GDI+ Error!
Hi to all!, i'm here another time :P
I'm using this function to convert an image into an array of bytes, so with this i can save it to image field on sql express 2k5.
Public Shared Function Image2Bytes(ByVal img As Image) As Byte()
Dim sTemp As String = Path.GetTempFileName()
Dim fs As New FileStream(sTemp, FileMode.OpenOrCreate, FileAccess.ReadWrite)
img.Save(fs, System.Drawing.Imaging.ImageFormat.Png)
fs.Position = 0
'
Dim imgLength As Integer = CInt(fs.Length)
Dim bytes(0 To imgLength - 1) As Byte
fs.Read(bytes, 0, imgLength)
fs.Close()
Return bytes
End Function
Ok, i pass me.picturebox.image as parameter and the function returns me an error on the img.save line saying "Generic error on GDI+"
I'm using vs 2005 on xp sp2
Anyone can help me? thx!
[950 byte] By [
satanorz] at [2007-11-20 9:19:41]

# 1 Re: GDI+ Error!
I tried running this procedure numurous times, 25, to be exact. Not once did it fail.
I even went to my Temp directory, and saw that the .tmp file was indeed created.
How big ( in size ) is the picture you are trying to "save"
# 2 Re: GDI+ Error!
Wow, this error is very crazy!, i've tested it with various images, formats as jpg, bmp, gif.. , sizes like 50x50@256 to 640x480@24b and always the same error at the same point.
I also try to put the temp filename manually to c:\lol.png, because i googled somewhere and says this problems maybe due to permissions problems.. but the error persists and the file is created empty.
Thanks for testing it a lot of times Hannes!, i'll check the code, if i found a solution it will be posted here :P
# 6 Re: GDI+ Error!
Many thanks Hannes!
I've tested many ways of doing the same thing, and this is the way that works to me.
I put here the two conversion functions working, hope it helps anyone!
This function returns the image of the filename in an array of bytes ( useful to upload images on SQL Server)
Public Function Image2Bytes(ByVal filename As String) As Byte()
Dim fs As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read)
' Read the Data into the Byte Array
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, CInt(fs.Length))
fs.Close()
Return MyData
End Function
And this makes useable the image field stored in the database
Public Function Bytes2Image(ByVal bytes() As Byte) As Image
If bytes Is Nothing Then Return Nothing
'
Dim ms As New MemoryStream(bytes)
Dim bm As Bitmap = Nothing
Try
bm = New Bitmap(ms)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.Message)
End Try
Return bm
End Function