How to convert System.Drawing.Image to a byte array?

Hi, I am trying to convert an image to an array of bytes in order to pass it to sql as byte (). I try to use Image Converter, but it continues to fail
Dim converter As New ImageConverter nRow.Signature = converter.ConvertTo(imgSignature, TypeOf(Byte()) 

The error that I keep getting is a byte, this is an expression like

+10
image bytearray
source share
2 answers

The VB.NET TypeOf statement does not do what you think it does. Somewhat confusing, possibly due to the typeof C # operator. The equivalent of VB.NET is the GetType () function. This works great:

 Dim converter As New ImageConverter nRow.Signature = converter.ConvertTo(imgSignature, GetType(Byte())) 

The type converter uses a MemoryStream to perform the conversion using the PNG image format.

+17
source share

You can use MemoryStream . By storing the image in a MemoryStream , you can get an array of bytes of data from the image:

 Dim ms = new MemoryStream() imgSegnature.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) ' Use appropriate format here Dim bytes = ms.ToArray() 
+17
source share

All Articles