-1

I've got a c# application which saves and displays records, When saving a record with an image using the picture box it seems as though the image has saved just fine but in the database all the images seem to have the same tag

[BLOB - 13B]

when saving the exact same image into the database manually using the dbms the image tag will read

[BLOB - 2.4KB]

with this tag I can use my image display method and it will be fine

The image is being converted or something when being saved but I cant seem to understand what is the problem.

here is the code for selecting and saving the image

MemoryStream ms = new MemoryStream();
pictureBox.Image.Save(ms, pictureBox.Image.RawFormat);
byte[] img = ms.ToArray();

because of the way this code changes the image, when retrieving it, I receive an error which says "parameter is invalid" using c# and a mysqldatabase, although the database type is irrelevant.

Anyone understand what is going wrong?

minglee
  • 53
  • 1
  • 7

2 Answers2

0

It sounds like you're saving a byte array of your image to your database and need to know how to convert that byte array back into an image so that it can be displayed. You can do that with a function like this:

    public Image ConvertByteArrayToImage(byte[] byteArray)
    {
        MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length);
        ms.Write(byteArray, 0, byteArray.Length);
        Image image = Image.FromStream(ms, true);
        return image;
    }

UPDATE: After reading it over again. I don't think I answered the actual question you were asking. It sounds like your not converting your image into a byte array. I would try taking the image from your picturebox and putting it into an Image object. Then converting that Image object into a byte array. Rather than saving picturebox.Image.RawFormat

Samuel David
  • 75
  • 12
  • I am already doing this using the following code byte[] image = GetImage(); MemoryStream stream = new MemoryStream(image); pictureBoxEdit.Image = Image.FromStream(stream); But it still wont work when retrieving the images which have the 13b blob tag only works with images that I entered through the dbms The GetImage() method is predefined and returns the image as a byte array – minglee Mar 24 '16 at 19:19
  • I read your update and it sounds like it makes sense, I will try it out. Thanks – minglee Mar 24 '16 at 19:23
  • I don't know the exact syntax off the top of my head but it would probably go something like: Image picture = new Image(picturebox.Image) – Samuel David Mar 24 '16 at 19:27
  • I tried out what you suggested and entering the image into the database now gives me [BLOB - 21B] so there is some difference, but I am still unable to retrieve the image. Would I still have to convert the image to a byte array and back when retrieving it? – minglee Mar 24 '16 at 19:38
  • Yes. You have to convert the Image object into a byte[] and save the byte[] to your database. Then you convert that byte[] back into an Image object using the function above. – Samuel David Mar 24 '16 at 19:43
  • Still getting the same error using the code where I save the picture box image into an image object. It seems to save slightly differently into the database but still not how the original image should be saved. perhaps it is losing bytes? or is that normal – minglee Mar 24 '16 at 20:02
0

For any future readers.

I figured out my own problem after some research, turns out that when I was concatenating the SQL string and placing the byte array into it, the byte array would be changed to follow the string parameters. Hence the blob in the database would say [Blob 13B] only rather than the 2.7KB it should have been

To avoid this, you have to use a "parameterized" SQL query and not a standard concatenated one.

see Why my BLOB field is still 13B - what I am doing wrong?

and SQL Insert Query Using C#

Community
  • 1
  • 1
minglee
  • 53
  • 1
  • 7