Tuesday, December 23, 2008

Image to Byte Array and Byte Array to Image

Level: Intermediate

Knowledge Required:
Image Class

Description:
Here are simple functions that convert an Image into Byte Array and Byte Array into Image. An image is required to be converted into bytes array when we save it in Database, hence on reading the bytes from database, it needs to be converted back into image.

Private Function BytesToImage(ByVal ImageBytes() As Byte) As Image
    Dim imgNew As Image
    Dim memImage As New System.IO.MemoryStream(ImageBytes)
    imgNew = Image.FromStream(memImage)
    Return imgNew
End Function

Private Function ImageToBytes(ByVal Image As Image) As Byte()
    Dim memImage As New System.IO.MemoryStream
    Dim bytImage() As Byte

    Image.Save(memImage, Image.RawFormat)
    bytImage = memImage.GetBuffer()

    Return bytImage
End Function

3 comments:

Anonymous said...

Thank you so much. It helps me very well.
Zanyar From Iran

Jesús said...

how I can do it in vb6???

Bambang T Lasiman said...

FromStream is not a member System.Web.UI.WebControls.Image

what the problem?