Convert Image to binary format?


Are you looking for sample code to convert image to binary format in VB.NET? Here is an example.

This code used to convert image into binary format

for ex

dim img as image =image.fromfile("C:\test.jpeg")
dim value as string =imageconversion(img)

							

Public Function ImageConversion(ByVal image As System.Drawing.Image) As String
If image Is Nothing Then Return ""
Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif)
Dim value As String = ""
For intCnt As Integer = 0 To memoryStream.ToArray.Length - 1
value = value & memoryStream.ToArray(intCnt) & ","
Next
Return value
End Function


Comments

Author: m dev10 Jun 2010 Member Level: Bronze   Points : 2

Many thanks for the good code. I faced a small issue, because i needed a byte array, then i do not know how to code a function which sends an array, then i slightly changed this, can u add this also so that others may get benefit from this.

Dim Img As Image = Image.FromFile(OpenFileDialog1.FileName)
Dim SomeBytAry() As Byte
Call ImageConversion(Img, SomeBytAry)

ImageDataSet.ImageTable.AddImageTableRow(SlNoCnt, SomeBytAry)

Public Sub ImageConversion(ByVal image As System.Drawing.Image, ByRef BytAry() As Byte)

Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
ReDim Preserve BytAry(memoryStream.ToArray.Length - 1)
For intCnt As Integer = 0 To memoryStream.ToArray.Length - 1
BytAry(intCnt) = memoryStream.ToArray(intCnt)
Next

End Sub

Thanks once again.

Author: Asif Huddani11 Jun 2010 Member Level: Gold   Points : 1

Check this article for C# code

http://www.dotnetspider.com/resources/38378-Byte-Array-Image-Vice-versa.aspx

Author: leo fucc04 Jul 2011 Member Level: Bronze   Points : 1

If I may...
I give you a faster way..


Private Sub ImageConversion(ByVal image As System.Drawing.Image, ByRef BytAry() As Byte)

Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)

BytAry = memoryStream.ToArray()

End Sub




:)

Guest Author: Gary22 Jul 2012

how do i reverse this procedure to put the saved binary back to the image



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: