locked
FAQ: How do I convert between an image object and Byte Array?

    Question

  • How do I convert between an image object and Byte Array?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 2:44 PM

Answers

  • You can convert between an image object and Byte Array by means of MemoryStream class.


    Imports System.IO
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myImage As Image = Image.FromFile("D:\1.jpg")
            Dim imgByteArray As Byte() = Nothing
    
            'Image to byte[]      
            Dim imgMemoryStream As MemoryStream = New MemoryStream()
            myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            imgByteArray = imgMemoryStream.GetBuffer()
    
            'Byte[] to image      
            imgMemoryStream = New MemoryStream(imgByteArray)
            myImage = Drawing.Image.FromStream(imgMemoryStream)
    
        End Sub
    End Class
    

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ 



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

     

    Friday, April 10, 2009 2:46 PM