none
Retrieve IMAGE to PictureBox from Access Database

    Question

  • The following code was used to display the images from the ACCESS database to the picture box

    PROBLEM

    IMAGE.FromStream(ms)    EXPRESSION DOES NOT PRODUCE A VALUE

        Dim i As Integer
            i = DataGridView1.CurrentRow.Index
            'Image
            Dim bytes As [Byte]() = (DataGridView1.Item(6, i).Value)
            Dim ms As New MemoryStream(bytes)
            PictureBox1.Image = IMAGE.FromStream(ms)
    

    Thursday, February 8, 2018 3:15 PM

Answers

  • See my MSDN code sample on working with images 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 8, 2018 3:51 PM
    Moderator
  • Hi monemas,

    This code retrieves the image from the Test5 table in the database into  a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Imageproperty of the PictureBox control.

     If TextBox1.Text = "" Then
                MessageBox.Show("Please fill the photo name")
            Else
                Using conn As New OleDbConnection(str)
                    conn.Open()
                    Dim sql As String = "select photo from Test5 where Name=@Name"
                    Using cmd As New OleDbCommand(sql, conn)
                        cmd.Parameters.AddWithValue("@Name", TextBox1.Text)
                        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                        If Not imageData Is Nothing Then
                            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                                ms.Write(imageData, 0, imageData.Length)
                                PictureBox1.Image = Image.FromStream(ms, True)
                            End Using
                        End If
                    End Using
                    conn.Close()
                End Using
            End If

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor LigthertMVP Friday, February 9, 2018 9:18 AM
    • Marked as answer by monemas Friday, February 9, 2018 11:04 PM
    Friday, February 9, 2018 4:21 AM
    Moderator
  • Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

            If TextBox1.Text = "" Then
                MessageBox.Show("Please fill the ID")
            Else
                Using conn As New OleDbConnection(conString)
                    conn.Open()
                    Dim sql As String = "select pic from picture where ids=@ids"
                    Using cmd As New OleDbCommand(sql, conn)
                        cmd.Parameters.AddWithValue("@ids", TextBox1.Text)
                        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                        If Not imageData Is Nothing Then
                            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                                ms.Write(imageData, 0, imageData.Length)
                                Picture.Image = Image.FromStream(ms, True)
                            End Using
                        End If
                    End Using
                    conn.Close()
                End Using
            End If

    unable to search picture from ms access data base pz help me

    Is there reason why you are posting to a question which was answered months ago? We like to keep current when providing information and when providing information be more details and real to life which your code sample is not, no user is every going to enter an id, instead they are presented with valid selections.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by monemas Saturday, July 7, 2018 1:27 PM
    Thursday, June 7, 2018 10:32 AM
    Moderator

All replies

  • Monemas,

    An Access database image can be from the Ole......... (I forget forever that Olename) format, Paul knows it exact. 

    It has an offset this is a part of code from our website, the tip is to old to show.

    Dim ms As New System.IO.MemoryStream
    Dim bm As Bitmap
    Dim arData() As Byte = dr.Item("Picture")
    ms.Write(arData, 78, arData.Length - 78)
    bm = New Bitmap(ms)
    PictureBox1.Image = bm
    End Sub 
    

     



    Success Cor

    Thursday, February 8, 2018 3:49 PM
  • See my MSDN code sample on working with images 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 8, 2018 3:51 PM
    Moderator
  • Below is a simple example - one row, one column - it can be expanded for multiple rows and columns. If the image is stored as an OLE Object then the OLE header will need to be removed as Cor mentioned.

            Dim RetVal As Long
            Dim FieldLen As Int32
    
            Dim AccessConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=C:\Users\...\Documents\My Database\Access\db1 XP.accdb")
            Dim AccessCommand As New OleDbCommand("SELECT BLOB FROM BlobTable WHERE ID = @ID", AccessConnection)
            AccessCommand.Parameters.AddWithValue("ID", 7)
            AccessConnection.Open()
            Dim AccessDataReader As OleDbDataReader = AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
            AccessDataReader.Read()
            FieldLen = AccessDataReader.Item(0).Length
            Dim PictureByteArray(FieldLen - 1) As Byte
            Dim startIndex As Integer = 0
            RetVal = AccessDataReader.GetBytes(0, startIndex, PictureByteArray, 0, PictureByteArray.Length)
            Dim BLOBDataStream As New MemoryStream(PictureByteArray)
    
            Dim ImageColumn As New DataGridViewImageColumn
    
            ImageGrid.Columns.Add(ImageColumn)
            ImageColumn.Name = "Image"
            ImageColumn.HeaderText = "Image"
            ImageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
            ImageGrid.Columns("Image").DataGridView.AutoGenerateColumns = False
            ImageGrid.Columns("Image").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            ImageGrid.Rows(0).Cells("Image").Value = Image.FromStream(BLOBDataStream)
    
            AccessDataReader.Close()
            AccessConnection.Close()


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, February 8, 2018 4:44 PM
  • Hi monemas,

    This code retrieves the image from the Test5 table in the database into  a Byte array and then into a MemoryStream object, and then loads the MemoryStream into the Imageproperty of the PictureBox control.

     If TextBox1.Text = "" Then
                MessageBox.Show("Please fill the photo name")
            Else
                Using conn As New OleDbConnection(str)
                    conn.Open()
                    Dim sql As String = "select photo from Test5 where Name=@Name"
                    Using cmd As New OleDbCommand(sql, conn)
                        cmd.Parameters.AddWithValue("@Name", TextBox1.Text)
                        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                        If Not imageData Is Nothing Then
                            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                                ms.Write(imageData, 0, imageData.Length)
                                PictureBox1.Image = Image.FromStream(ms, True)
                            End Using
                        End If
                    End Using
                    conn.Close()
                End Using
            End If

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor LigthertMVP Friday, February 9, 2018 9:18 AM
    • Marked as answer by monemas Friday, February 9, 2018 11:04 PM
    Friday, February 9, 2018 4:21 AM
    Moderator
  • Cherry, 

    Extend next time the test on the returned scalar object, if a Null column is allowed in SQL server there is not returned Nothing but DBNull.Value

     If Not imageData Is Nothing AndAlso Not imageobj Is DBNull.Value Then
    Nothing is in fact only with an empty database


    Success Cor


    Friday, February 9, 2018 9:19 AM
  • Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

            If TextBox1.Text = "" Then
                MessageBox.Show("Please fill the ID")
            Else
                Using conn As New OleDbConnection(conString)
                    conn.Open()
                    Dim sql As String = "select pic from picture where ids=@ids"
                    Using cmd As New OleDbCommand(sql, conn)
                        cmd.Parameters.AddWithValue("@ids", TextBox1.Text)
                        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                        If Not imageData Is Nothing Then
                            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                                ms.Write(imageData, 0, imageData.Length)
                                Picture.Image = Image.FromStream(ms, True)
                            End Using
                        End If
                    End Using
                    conn.Close()
                End Using
            End If

    unable to search picture from ms access data base pz help me

    Thursday, June 7, 2018 10:05 AM
  • Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

            If TextBox1.Text = "" Then
                MessageBox.Show("Please fill the ID")
            Else
                Using conn As New OleDbConnection(conString)
                    conn.Open()
                    Dim sql As String = "select pic from picture where ids=@ids"
                    Using cmd As New OleDbCommand(sql, conn)
                        cmd.Parameters.AddWithValue("@ids", TextBox1.Text)
                        Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
                        If Not imageData Is Nothing Then
                            Using ms As New MemoryStream(imageData, 0, imageData.Length)
                                ms.Write(imageData, 0, imageData.Length)
                                Picture.Image = Image.FromStream(ms, True)
                            End Using
                        End If
                    End Using
                    conn.Close()
                End Using
            End If

    unable to search picture from ms access data base pz help me

    Is there reason why you are posting to a question which was answered months ago? We like to keep current when providing information and when providing information be more details and real to life which your code sample is not, no user is every going to enter an id, instead they are presented with valid selections.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by monemas Saturday, July 7, 2018 1:27 PM
    Thursday, June 7, 2018 10:32 AM
    Moderator