Convert a Bitmap to a Byte Array?

G

Guest

Greetings,
I think I know the answer to this question already but I thought it would be
worth while to ask it anyways. I need to convert a bitmap object to a byte
array...is there anyway to do this? Is there any easy way to save a bitmap to
a file?

I'm taking byte array converting it to a bitmap, cropping it and then trying
to save it to a file. I am able to convert the byte array to a bitmap and
crop it but I can't seem to find any easy ways to save it to a file. The most
logical answer seems to be to convert the bitmap back to a byte array and to
save it to a file via the fileStream class.

Thanks
 
H

Harry Simpson

Image is the link i think....

Here's a code snippet grabbing an Image and saving - seems like you could
plug in to the bitmap vs the image into the byte() cast.....

Dim stream As New MemoryStream()

Dim connection As New
SqlConnection(ConfigurationSettings.AppSettings.Get("ConnectionString"))

Try

connection.Open()

Dim strSQL As String = "select ImgField from Images where ID ='" & PictureID
& "'"

Dim command As New SqlCommand(strSQL, connection)

Dim image As Byte() = CType(command.ExecuteScalar(), Byte())

stream.Write(image, 0, image.Length)

SaveStreamtoFile(stream, FilePath)

Return True

Catch

Return False

Finally

connection.Close()

stream.Close()

End Try



"Convert a Bitmap to a Byte Array?" <Convert a Bitmap to a Byte
[email protected]> wrote in message
news:[email protected]...
 
G

Guest

Harry,
Thanks for your help, but I get the following error when I try to cast a
bitmap to a byte array.


Dim image() as byte = ctype(bmpVAR, byte())

error:
value of type "system.drawing.bitmap" can not be converted to "1-dimsional
array of byte"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top