winforms 如何在检索图像时跳过数据库中的空位图值

w8ntj3qf  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(89)

我尝试设置flowlayoutpanel以显示来自access数据库www.example.com的图像VB.net,但是当它从image列读取空单元格时,它给我错误提示
指定的转换无效
图像数据是以字节为单位的我只是不知道当没有数据时如何正确地跳过一个单元格.如何正确地设置检查空字节的条件?
这是我代码

Private Sub loadData()
  cn.Open()
  cmd = New OleDbCommand("select [Image Name], [Item Name] from tbl_item_image", cn)

  dr = cmd.ExecuteReader
  While dr.Read
    Dim len As Long = dr.GetBytes(0, 0, Nothing, 0, 0)
    Dim array(CInt(len)) As Byte
    dr.GetBytes(0, 0, array, 0, CInt(len))

    pic = New PictureBox
    pic.Width = 100
    pic.Height = 100
    pic.BackgroundImageLayout = ImageLayout.Stretch
           
    mybtn = New Button
    mybtn.Width = 100
    mybtn.Height = 30

    AddHandler mybtn.Click, AddressOf OnButton_Click
    AddHandler mybtn.MouseHover, AddressOf OnButton_MouseHove

    Dim ms As New System.IO.MemoryStream(array)
    Dim bitmap As New System.Drawing.Bitmap(ms)

    pic.BackgroundImage = bitmap  
    mybtn.Text = dr.Item("Item Name").ToString
    FlowLayoutPanel1.Controls.Add(pic)
    pic.Controls.Add(mybtn)
  End While
  dr.Close()
  cn.Close()
End Sub

我试过设置一个If语句来检查null或空值,但我真的不知道如何正确地做。

kpbwa7wx

kpbwa7wx1#

试试这个

If Not Convert.IsDBNull(your image data) Then

        Dim ms As New MemoryStream(CType(your image data, Byte()))

        pic.Image = New Bitmap(Image.FromStream(ms))

        ms.Dispose()

    Else

        pic.Image = Nothing

    End If

相关问题