【问题标题】:How could I resolve A generic error occurred in GDI+. in VB.Net?如何解决 GDI+ 中出现一般错误。在 VB.Net 中?
【发布时间】:2018-04-11 09:49:48
【问题描述】:

当类中的图像字段属性设置为图片框中我的数据库中相同图像的图像原始格式时,它总是抛出此异常。相比之下,如果图片框中的图像已使用我从本地 PC 目录中选择的图像进行了更新,则更新功能可以正常工作。

下面是我的代码:

Try
   With mEmployee
     If Miscellaneous.GetImageName(ofdPhoto).ToLower = "No_Photo.jpg".ToLower Then
        .Image = Nothing
     Else
         Dim stream As New MemoryStream
         pbImage.Image.Save(stream, pbImage.Image.RawFormat)
         .Image = stream.GetBuffer()
     End If
  End With
Catch ex As Exception
   MessageBox.Show(ex.Message)
End Try

【问题讨论】:

  • 您肯定会假设 dbase 中的数据不是使用标准文件格式之一的正确编码图像。例如,存储在 Access 数据库中的图像并不罕见。代码 sn-p 似乎不相关,它对 dbase 没有任何作用。
  • 感谢您的评论。此代码与使用 mEmployee.Image = stream.GetBuffer() 行更新数据库表的数据设置器相关。但是此时,每次我不选择要更新的新图片时,它都会抛出一个异常。

标签: vb.net picturebox memorystream


【解决方案1】:

现在一切都解决了。此 GDI+ 通用错误是由图片框本身引起的。实际上,当我将记录与数据库中的图像绑定并更新该记录而不更新 PictureBox 中的图像时,图像字段已使用框中的旧 byte() 数据设置,从而导致错误。

为了解决这个问题,我声明了一个 byte() 类型变量来存储数据库中的临时图像 byte() 数据,并且在更新时,如果图像没有被更改,它将使用来自该变量的数据进行设置. 这是我解决所有问题的代码:

Try
            If Miscellaneous.GetImageName(ofdPhoto).ToLower = "No_Photo.jpg".ToLower Then
                .Image = Nothing
            Else
                If isImageChanged = True Then
                    Dim stream As New MemoryStream
                    pbImage.Image.Save(stream, pbImage.Image.RawFormat)
                    .Image = stream.GetBuffer()
                    isImageChanged = False
                ElseIf isRemoveImage = True Then
                    .Image = Nothing
                    isRemoveImage = False
                Else
                    .Image = tempImage
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多