Jollyyy100 0 Junior Poster in Training

Hi, i want to know if its possible to bring upon many pictures in a single picturebox and navigating it. So far i have done the coding but when i run the program i cant see the picture being displayed following are my codes, and please help if theres any error or if u got a better solution:

Imports System.Data.OleDb
Imports System.IO

Public Class StaffForm1
    Dim conn As New OleDb.OleDbConnection
    Dim DA As OleDb.OleDbDataAdapter
    Dim DS As New DataSet
    Dim counter As Integer

    Private Sub StaffForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\User\Desktop\Payroll_System\PDatabase.mdb"

        DA = New OleDb.OleDbDataAdapter("Select * from Pictures", conn)

        DA.Fill(DS)

        counter = 0

        Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))

        If counter < Me.DS.Tables(0).Rows.Count - 1 Then

            counter += 1

        End If
    End Sub
    Private Function GetImageFromByteArray(ByVal picData As Byte()) As Image
        If picData Is Nothing Then
            Return Nothing
        End If

        ' is this is an embedded object? 
        Dim bmData As Integer = IIf((picData(0) = 21 AndAlso picData(1) = 28), 78, 0)

        ' load the picture 
        Dim img As Image = Nothing
        Try
            Dim ms As New IO.MemoryStream(picData, bmData, picData.Length - bmData)
            img = Image.FromStream(ms)
        Catch
        End Try

        ' return what we got 
        Return img

    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))

        If counter > 0 Then

            counter -= 1

        End If

    End Sub

End Class

Dani AI

Generated

— common causes when an image does not appear after loading bytes from a database: the query returned no rows or the wrong column, the field is DBNull, the stored Access OLE object contains an extra header, or the MemoryStream/Image lifetime is mishandled. Quick checks: confirm the DataSet actually has rows, inspect the field type and length with the debugger (or Debug.WriteLine(row("YourColumn").GetType())), and guard against DBNull before trying to convert to an Image.

If the image bytes include an OLE wrapper (typical with Access OLE Object fields), a robust approach is to scan the byte array for common image signatures (JPEG, PNG, GIF) and create the Image from the first matching signature. The snippet below shows a safe VB.NET helper that finds a signature, builds an Image from that offset and returns a copy so the underlying MemoryStream can be disposed immediately.

' Returns an Image or Nothing. Searches for JPEG/PNG/GIF headers before loading.
Private Function ImageFromBytes(ByVal data As Byte()) As Image
    If data Is Nothing OrElse data.Length = 0 Then Return Nothing

    Dim patterns As New List(Of Byte()) From {
        New Byte() {&HFF, &HD8},                         ' JPEG SOI
        New Byte() {&H89, &H50, &H4E, &H47},             ' PNG signature
        System.Text.Encoding.ASCII.GetBytes("GIF")       ' GIF
    }

    Dim offset As Integer = -1
    For Each p In patterns
        offset = IndexOfPattern(data, p)
        If offset >= 0 Then Exit For
    Next
    If offset < 0 Then offset = 0

    Using ms As New IO.MemoryStream(data, offset, data.Length - offset)
        Using tmp As Image = Image.FromStream(ms)
            Return New Bitmap(tmp)   ' detach image from stream
        End Using
    End Using
End Function

Private Function IndexOfPattern(source As Byte(), pattern As Byte()) As Integer
    For i = 0 To source.Length - pattern.Length
        Dim ok = True
        For j = 0 To pattern.Length - 1
            If source(i + j) <> pattern(j) Then ok = False : Exit For
        Next
        If ok Then Return i
    Next
    Return -1
End Function

Additional tips: always check IsNull before casting to Byte(), dispose the previous PictureBox image to avoid memory leaks, and clamp your navigation index to [0, count-1] so you never request a missing row. For long-term reliability, store a file path or a raw binary (without OLE wrapper) rather than an Access OLE Object. See the .NET docs for Image.FromStream for details: Image.FromStream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.