【问题标题】:Save multiple images to database. Write exception for empty picture boxes将多个图像保存到数据库。空图片框写入异常
【发布时间】:2015-01-12 05:41:07
【问题描述】:

我的表单上有 4 个图片框,用于存储图像并发送到我的数据库:我通过表格适配器来完成。图像通过此函数转换为流:

公共函数 ConvertImage(ByVal myImage As Image) As Byte()

'store image in memory before converting and
'create memory stream, save image in proper format
Dim mStream As New MemoryStream
myImage.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg)

'convert new stream into bytes and indicate size
Dim myBytes(mStream.Length - 1) As Byte
mStream.Position = 0

mStream.Read(myBytes, 0, mStream.Length)

Return myBytes

结束函数

我的问题是当我的一个或多个图片框没有图像时,如果一个或多个图片框是空的,我如何在程序上指示,然后将图像设置为无。这是我现在的程序的简化版本

    Private Sub btnSaveCategory_Click(sender As Object, e As EventArgs) Handles btnSaveCategory.Click

        Dim picImage As Image = picBox1.Image
        Dim picImage 2 As Image = picBox2.Image
        Dim picImage 3 As Image = picBox3.Image
        Dim picImage 4 As Image = picBox4.Image

    PRODUCT_CATEGORYTableAdapter.InsertNewCategory(txtCategoryName.Text, ConvertImage(picImage),Image(picImage2), Image(picImage3), Image(picImage4))

End Sub

如何检查一个空的图片框并将其设置为空,这样我的程序就不会因为一个或多个框上不存在图像而出错?

谢谢。

【问题讨论】:

    标签: vb.net image


    【解决方案1】:

    试试这个

    If pictureBox.Image is Nothing Then
        //do stuff here
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      相关资源
      最近更新 更多