使用下一个和上一个按钮在图片框中显示图像

时间:2014-12-22 00:41:46

标签: vb.net picturebox

我现在正在显示来自picturebox的图像,但我的问题是使用下一个按钮和上一个按钮显示图像。

这是我的加载代码:

    Private Sub me_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    disconnect()
    connect()
    cmd = New Odbc.OdbcCommand("Select stud_pic from tablestudent order by section desc", con)
    dr = cmd.ExecuteReader
    If dr.HasRows Then
        dr.Read()
        Dim data As Byte() = DirectCast(dr("stud_pic"), Byte())
        Dim ms As New MemoryStream(data)
        PictureBox1.Image = Image.FromStream(ms)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If
End Sub

下一个和上一个按钮:

  Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
 'Do here
 End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub me_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    disconnect()
    connect()
    DA = New Odbc.OdbcDataAdapter("Select stud_pic from tablestudent order by section desc", con)
    DA.Fill(DS)

    counter = 0

    Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub

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

    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 MemoryStream(picData, bmData, picData.Length - bmData)
        img = Image.FromStream(ms)
    Catch
    End Try ' return what we got Return img
    Return img

End Function

Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.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 Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
    PictureBox1.Image = GetImageFromByteArray(DS.Tables(0).Rows.Item(counter).Item(0))
    If counter > 0 Then
        counter -= 1
    End If
End Sub