How to convert image object to byte array vb.net

Stevan
Stevan
Member
310 Points
20 Posts

I'm working on vb.net. I wan to convert image object to byte array.  Can anybody suggest how I can convert an image to a byte array and vice versa?
If anybody has some code samples to help me out, that would be great.

Views: 10814
Total Answered: 2
Total Marked As Answer: 0
Posted On: 16-Mar-2015 14:45

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Editor
1382 Points
14 Posts
         

Use following code to convert image object to byte array in vb.net

Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte()
Dim _tempByte() As Byte = Nothing
If String.IsNullOrEmpty(ImageFilePath) = True Then
Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath")
Return Nothing
End If
Try
Dim _fileInfo As New IO.FileInfo(ImageFilePath)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim _BinaryReader As New IO.BinaryReader(_FStream)
_tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes))
_fileInfo = Nothing
_NumBytes = 0
_FStream.Close()
_FStream.Dispose()
_BinaryReader.Close()
Return _tempByte
Catch ex As Exception
Return Nothing
End Try
End Function
Posted On: 16-Mar-2015 15:49
Rashmi
Rashmi
Member
820 Points
17 Posts
         

You can use following:

'Read Image File into Image object.
Dim img As Image = Image.FromFile("C:\image2.jpg")

'ImageConverter Class convert Image object to Byte array.
Dim bytes As Byte() = CType((New ImageConverter()).ConvertTo(img, GetType(Byte())), Byte())
Posted On: 23-Feb-2019 03:16
 Log In to Chat