none
convert image to byte array

    Question

  • Hello,

     

    I'm trying to convert a image into a byte array for use in a msacces db.

    THis is the code I use but I can;\'t get the byte array?

    Imports System.IO
    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        StreamFile("C:\Users\magistics\Pictures\838.jpg")
      End Sub
    
      Private Function StreamFile(ByVal filename As String) As Byte()
        filename = "C:\Users\magistics\Pictures\838.jpg"
        Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    
        ' Create a byte array of file stream length 
        Dim ImageData As Byte() = New Byte(fs.Length - 1) {}
    
        'Read block of bytes from stream into the byte array 
        fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))
    
        'Close the File Stream 
        fs.Close()
        'return the byte data 
    
        Return ImageData
        TextBox1.Text = ImageData.ToString
      End Function

    with kind regards,

     

    rocky

    Thursday, June 10, 2010 9:28 AM

Answers

  • Does this sample on our website helps you,

    It uses the binary reader

    http://www.vb-tips.com/Image2ByteArray.aspx


    Success
    Cor
    Thursday, June 10, 2010 9:34 AM
  • Hi rocky,

    to get it as a string there aren`t that much possibilities. You can use the code below to convert the bytearray to a base64 string. Usually you wouldn`t store the image inside a database,but a link to the file inside some filesystem. If you need to save it to a database then a binary blob field would be the best option.


    'Visual Basic 2008 - .net 3.5 - Any CPU
    Private Sub ShowTheBase64()
        TextBox1.Text = Convert.ToBase64String(StreamFile("C:\Users\magistics\Pictures\838.jpg"))
      End Sub
      Private Function StreamFile(ByVal filename As String) As Byte()
        filename = "C:\Users\magistics\Pictures\838.jpg"
        Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    
        ' Create a byte array of file stream length 
        Dim ImageData As Byte() = New Byte(fs.Length - 1) {}
    
        'Read block of bytes from stream into the byte array 
        fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))
    
        'Close the File Stream 
        fs.Close()
        'return the byte data 
    
        Return ImageData
    
      End Function

     


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Friday, June 11, 2010 11:53 AM
  • Take a look at 'My.Computer.FileSystem.ReadAllBytes' -- just a single, simple method call!
    Herfried K. Wagner [MVP]
    Friday, June 11, 2010 11:55 PM
  • Hi rocky,

    i have tested your code and it work like expected. I guess you think it isn`t because you won`t see anything inside your textbox.

    This is because after the Return ImageData no instruction will be executed anymore ( TextBox1.Text ...). Also you wouldn`t be satisfied if you could see the ImageData.ToString because it results in the text : System.Byte[]

     


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Thursday, June 10, 2010 9:57 AM
  • MemoryStreams and Byte() are identical.  Try this code:

    Imports System.IO
    Imports System.Drawing.Imaging
    Public Class Form1
      
    Public Sub New()
        InitializeComponent()
        
    AddHandler Open.Click, AddressOf Open_Click
        MS.Items.Add(Open)
        PB.Dock = DockStyle.Fill
        MS.Parent = 
    Me
        PB.Parent = Me
      End Sub
      Dim MS As New MenuStrip
      
    Dim Open As New ToolStripButton("Open")
      
    Dim PB As New PictureBox
      
    Private Sub Open_Click(ByVal sender As ObjectByVal e As EventArgs)
        
    Using OFD As New OpenFileDialog
          
    Dim B(), B1() As Byte
          If OFD.ShowDialog = DialogResult.OK Then
            B = File.ReadAllBytes(OFD.FileName)
            
    Using MS As New MemoryStream(B)
              PB.Image = Image.FromStream(MS)
              B1 = MS.ToArray
            
    End Using
          End If
          Dim I As Integer
          For I = 0 To B.Length - 1
            
    If B(I) <> B1(I) Then Exit For
          Next
          MessageBox.Show(If(I < B.Length, "Byte arrays differ.""Byte arrays are identical"))
        
    End Using
      End Sub
    End
     Class

    Thursday, June 10, 2010 10:17 AM
  • If you want the output in text, then try to use base64 converter to convert the byte to string

    This will convert the byte to string
     Dim ImageData() As Byte
     Dim output As String = Convert.ToBase64String(ImageData)

     Now, to convert the string back to byte
    ImageData = Convert.FromBase64String(output)

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, June 11, 2010 11:46 AM

All replies

  • Does this sample on our website helps you,

    It uses the binary reader

    http://www.vb-tips.com/Image2ByteArray.aspx


    Success
    Cor
    Thursday, June 10, 2010 9:34 AM
  • Hi rocky,

    i have tested your code and it work like expected. I guess you think it isn`t because you won`t see anything inside your textbox.

    This is because after the Return ImageData no instruction will be executed anymore ( TextBox1.Text ...). Also you wouldn`t be satisfied if you could see the ImageData.ToString because it results in the text : System.Byte[]

     


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Thursday, June 10, 2010 9:57 AM
  • MemoryStreams and Byte() are identical.  Try this code:

    Imports System.IO
    Imports System.Drawing.Imaging
    Public Class Form1
      
    Public Sub New()
        InitializeComponent()
        
    AddHandler Open.Click, AddressOf Open_Click
        MS.Items.Add(Open)
        PB.Dock = DockStyle.Fill
        MS.Parent = 
    Me
        PB.Parent = Me
      End Sub
      Dim MS As New MenuStrip
      
    Dim Open As New ToolStripButton("Open")
      
    Dim PB As New PictureBox
      
    Private Sub Open_Click(ByVal sender As ObjectByVal e As EventArgs)
        
    Using OFD As New OpenFileDialog
          
    Dim B(), B1() As Byte
          If OFD.ShowDialog = DialogResult.OK Then
            B = File.ReadAllBytes(OFD.FileName)
            
    Using MS As New MemoryStream(B)
              PB.Image = Image.FromStream(MS)
              B1 = MS.ToArray
            
    End Using
          End If
          Dim I As Integer
          For I = 0 To B.Length - 1
            
    If B(I) <> B1(I) Then Exit For
          Next
          MessageBox.Show(If(I < B.Length, "Byte arrays differ.""Byte arrays are identical"))
        
    End Using
      End Sub
    End
     Class

    Thursday, June 10, 2010 10:17 AM
  • Hello Heslacher,

    You're right about this, I thought it was not working, but how to solve my prob.

    I think I need the output iin text? I need it in a database, the component I use converts the image from bytearray into inmage and shows the image.

     

    Regards,

     

    rocky

    Friday, June 11, 2010 11:08 AM
  • If you want the output in text, then try to use base64 converter to convert the byte to string

    This will convert the byte to string
     Dim ImageData() As Byte
     Dim output As String = Convert.ToBase64String(ImageData)

     Now, to convert the string back to byte
    ImageData = Convert.FromBase64String(output)

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, June 11, 2010 11:46 AM
  • Hi rocky,

    to get it as a string there aren`t that much possibilities. You can use the code below to convert the bytearray to a base64 string. Usually you wouldn`t store the image inside a database,but a link to the file inside some filesystem. If you need to save it to a database then a binary blob field would be the best option.


    'Visual Basic 2008 - .net 3.5 - Any CPU
    Private Sub ShowTheBase64()
        TextBox1.Text = Convert.ToBase64String(StreamFile("C:\Users\magistics\Pictures\838.jpg"))
      End Sub
      Private Function StreamFile(ByVal filename As String) As Byte()
        filename = "C:\Users\magistics\Pictures\838.jpg"
        Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    
        ' Create a byte array of file stream length 
        Dim ImageData As Byte() = New Byte(fs.Length - 1) {}
    
        'Read block of bytes from stream into the byte array 
        fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))
    
        'Close the File Stream 
        fs.Close()
        'return the byte data 
    
        Return ImageData
    
      End Function

     


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Friday, June 11, 2010 11:53 AM
  • Take a look at 'My.Computer.FileSystem.ReadAllBytes' -- just a single, simple method call!
    Herfried K. Wagner [MVP]
    Friday, June 11, 2010 11:55 PM