locked
how to convert jpg format into binary format using VB code RRS feed

  • Question

  •  

    Dear All

     

    I wish if anyone can help me with how to convert  jpg format into binary format using VB code ?

     

    Thanks

    Mohamd

    Thursday, February 28, 2008 12:42 AM

Answers

  • Hi Mohamd,

     

    You can convert an image object between Image and Byte Array by means of MemoryStream object like this:

    Code Block

    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("E:\VBproject\1.jpg")

     

            'Image to byte[]

            Dim imgMemoryStream As MemoryStream = New MemoryStream()

            Dim imgByteArray As Byte() = Nothing

            myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)

            imgByteArray = imgMemoryStream.GetBuffer()

     

            'Byte[] to image

            imgMemoryStream = New IO.MemoryStream(imgByteArray)

            myImage = Drawing.Image.FromStream(imgMemoryStream)

     

            'Show it and check if the image is accordant with E:\VBproject\1.jpg.

            PictureBox1.Image = myImage  

     

        End Sub

    End Class

    Trackback: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2324862&SiteID=1

     

     

    For Visual Basic 6 questions, please read...

     

    Visual Basic 6 and Earlier forum

    http://www.vbforums.com/forumdisplay.php?f=1

     

     

    Regards,

    Martin

    Monday, March 3, 2008 5:40 AM

All replies

  • Hi Mohamd,

     

    You can convert an image object between Image and Byte Array by means of MemoryStream object like this:

    Code Block

    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("E:\VBproject\1.jpg")

     

            'Image to byte[]

            Dim imgMemoryStream As MemoryStream = New MemoryStream()

            Dim imgByteArray As Byte() = Nothing

            myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)

            imgByteArray = imgMemoryStream.GetBuffer()

     

            'Byte[] to image

            imgMemoryStream = New IO.MemoryStream(imgByteArray)

            myImage = Drawing.Image.FromStream(imgMemoryStream)

     

            'Show it and check if the image is accordant with E:\VBproject\1.jpg.

            PictureBox1.Image = myImage  

     

        End Sub

    End Class

    Trackback: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2324862&SiteID=1

     

     

    For Visual Basic 6 questions, please read...

     

    Visual Basic 6 and Earlier forum

    http://www.vbforums.com/forumdisplay.php?f=1

     

     

    Regards,

    Martin

    Monday, March 3, 2008 5:40 AM
  • Does this work in visual basic 6?  Right now, I can only get Dim imgMemoryStream as stream.

    If yes, what reference do you need to add?

     

    Dim imgMemoryStream As MemoryStream

     

    Thanks.

     

    Saturday, April 12, 2008 1:45 AM
  • No, este codigo es para Vb.Net

    Friday, March 26, 2010 8:59 PM