1

I don't know what's wrong with my code in retrieving image in db. I insert image without using image path file cause the image provide by the cam.

Here's my code in inserting image in db

Image img = pictureBox1.Image;
MemoryStream memStream = new MemoryStream();
img.Save(memStream, ImageFormat.Bmp);
byte[] imageBt = memStream.ToArray();
query = "";
query += "INSERT INTO table(picture) VALUES ('" + imageBt + "')";
cmd = new MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.close();

Here's my code in retrieving image in db

query = "";
query += "SELECT picture FROM table WHERE id = '1'";
cmd = new MySqlCommand(query, con);
con.Open();
MemoryStream ms = new MemoryStream();
byte[] image = (byte[])cmd.ExecuteScalar();
ms.Write(image, 0, image.Length);
con.Close();
Bitmap bmp = new Bitmap(ms)
pictureBox1.Image = bmp; //Still get the Error here parameter is not valid

Is there anywrong process in saving image in database. Btw my image type in db is Blob. I don't know why it doesn't work in retrieving image it always thrown error. Thanks

6
  • i also try this one but i doesn't work also dr = cmd.ExecuteReader(); if (dr.Read()) { byte[] data = (byte[])dr["picture"]; MemoryStream ms = new MemoryStream(data); pictureBox1.Image = Image.FromStream(ms); } Commented Jul 5, 2016 at 15:58
  • 1
    Looks like you are trying to insert text. Use parameters to avoid sql injection and formatting issues. Commented Jul 5, 2016 at 16:01
  • You should start using SQL parameters immediately. Commented Jul 5, 2016 at 16:01
  • 1
    How can you append the byte[] imageBt variable to a string? query += "INSERT INTO table(picture) VALUES ('" + imageBt + "')"; I think you want to insert a binary blob or something in hex, but not like this. Check what exact query is executed above, wouldn't wonder if its ".. VALUES (System.Byte[]) Commented Jul 5, 2016 at 16:02
  • Any time you have a question about a “thrown error” please include the error message. Commented Jul 5, 2016 at 16:30

1 Answer 1

2

When reading, you haven't rewound the stream. If you Write, you must set ms.Position = 0; afterwards. Or simpler: create the stream from the data, then you don't need to:

byte[] image = ...
var ms = new MemoryStream(image);

When writing, you seem to have injected the data directly. That... almost certainly won't work - in fact, you're probably writing System.Byte[] to the command. Ideally, use a parameter:

query = "INSERT INTO table(picture) VALUES (@blob)";
cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("blob", imageBt);
cmd.ExecuteNonQuery();

(the exact syntax may change between RDBMS implementations)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for answering this question really aprreciated it :). May i ask if do i need to change the way how i insert image in db? Thanks
@issei-kun yes; the code in the question doesn't insert the image into the DB. IIt inserts the string "System.Byte[]" every single time. I'm Guessing that isn't what you intended.
Yeah but how will i insert the image in db and retrieve it. It seems my pic in db when i try to retrieve it always throw error parameter is not valid. How will i fix it or what is the proper way of inserting image in db with blob type and retrieve it in picture box.
@issei-kun I've already answered these questions. And if you are using the sql shown in your question to insert into the DB: no, your image is not in the DB. The literal string System.Byte[] is what you will have stored every time. Not the contents.
so much better if i will not convert my image into byte[] array? and retrieve it by creating stream?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.