代码之家  ›  专栏  ›  技术社区  ›  Sophart

如何解决GDI+中发生的一般错误。在VB中。网

  •  0
  • Sophart  · 技术社区  · 8 年前

    当类中的image字段属性设置为图片框中数据库中相同图像的image raw格式时,它总是引发此异常。相反,如果图片框中的图像已更新为我从本地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
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Sophart    8 年前

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

    为了解决这个问题,我声明了一个byte()类型的变量来存储数据库中的temp image 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