[RESOLVED] A generic error occurred in GDI+
i just encounterd an error gdi+ when saving same image back to database.
im using this code to get image from db
Code:
'Stream object containing the binary data
Dim ms As MemoryStream
ms = New MemoryStream(CType(Me.DataGridSearchView.SelectedCells(10).Value, Byte()))
frm.Picture1 = Image.FromStream(ms)
ms.Close()
frm.Picture1 is a property of image type from another class.
and this my code to update image back to db
Code:
If Not Me.PictureBox1.Image Is Nothing Then
Dim custPic as Byte() = Nothing
Dim ms As MemoryStream = New MemoryStream
Me.PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
custPic = ms.GetBuffer
ms.Close()
End If
there is no problem if i changed the image before saving it back to db,it gives error if you will not change the image before saving it back to database..
anyone have an idea..please share..
thank you very much in advance..
glen.a
Re: A generic error occurred in GDI+
This is just a guess, but sound like the image in the database becomes read-only when you read it into the Picture1 property. That might explain how you can save it OK when only you change the image. If so, how about replacing this
Code:
frm.Picture1 = Image.FromStream(ms)
by this?
Code:
frm.Picture1 = New Bitmap(Image.FromStream(ms))
Does it help?
BB
Re: A generic error occurred in GDI+
wow!..I think your guess was right..Thank you boops boops it works perfectly.
Can you explain what the cause of this problem?..
Re: A generic error occurred in GDI+
When you set the Image property of a PictureBox, and then try to save the same image you will get an exception. The source image is "in use", so the write attempt throws an error. Unfortunately, GDI+ errors are often extremely unhelpful: "Generic Error" and "Out of Memory" are typical.
I guessed something similar could be happening when you do frm.Picture1 = Image.FromStream(ms). The way to deal with that is to load a copy of the source image, instead of setting the Image property directly. For a PictureBox you can do that by using the Load method. The New Bitmap method I suggested is an alternative way to make a copy of the image.
BB
Re: A generic error occurred in GDI+
again,Thank you boops boops..i will not forget that..:)