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"
HanneSThEGreaT at 2007-11-10 3:09:08 >
# 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
satanorz at 2007-11-10 3:10:03 >
# 3 Re: GDI+ Error!
Hi!
If you don't come right, can you zip & upload the project
I tried on XP.
In the meantime, I'll try to see if I can find another way / the problem
HanneSThEGreaT at 2007-11-10 3:11:02 >
# 4 Re: GDI+ Error!
No way hannes, i also tried to update .Net 2.0 framework because i read googling that maybe a bug that was fixed on the update.. but no way.

I've uploaded the small program, tell me if it fails to you or if you encounter the bug.

Thanks in advance!
satanorz at 2007-11-10 3:12:07 >
# 5 Re: GDI+ Error!
OK, have a look here :
http://www.vbdotnetheaven.com/UploadFile/mgold/FlashCardsLanguageProgram04072005091936AM/FlashCardsLanguageProgram.aspx

it may be helpful..
HanneSThEGreaT at 2007-11-10 3:13:06 >
# 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
satanorz at 2007-11-10 3:14:05 >
# 7 Re: GDI+ Error!
You're welcome :) I'm glad you came right, well done! :thumb:
I'm still puzzled as to why your code in post #1 gave me different results. :confused:
HanneSThEGreaT at 2007-11-10 3:15:14 >