10

Hi I'm trying to convert image to byte array to pass it into sql as byte(). Im trying to use Image Converter but it keeps failing

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

the error I keep getting is byte is a type not expression

nikeee
  • 10,248
  • 7
  • 40
  • 66
PaShKa
  • 188
  • 1
  • 3
  • 13

2 Answers2

19

You can use a MemoryStream. By saving the image into a MemoryStream, you can get the byte array 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()
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
19

The VB.NET TypeOf operator doesn't do what you think it does. Somewhat confusing perhaps due to the C# typeof operator. The VB.NET equivalent is the GetType() function. This works fine:

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

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

Pierre-Loup Pagniez
  • 3,611
  • 3
  • 29
  • 29
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536