[英]VB.Net Exception Unhandled: System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+
[英]generic error occurred in gdi+ vb.net
完全希望它不会重复出现的问题,因为我进行过多搜索未找到任何答案。 我将图像保存到数据库的代码是
Private Function ConvertImageToRGBFormat(ByVal imgName As String) As Image
Dim img As Image
img = New Bitmap(imgName)
If Not img.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb Then
Dim temp As Bitmap = New Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
Dim g As Graphics = Graphics.FromImage(temp)
g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
g.Dispose()
Return temp
End If
Return img
End Function
Private Sub btnTravel_Info_Room_Image_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTravel_Info_Room_Image.Click
Dim dlg As New OpenFileDialog
If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim bt As Byte()
Dim myImage As Image
myImage = ConvertImageToRGBFormat(dlg.FileName)
Using stream = New MemoryStream()
myImage.Save(stream, Drawing.Imaging.ImageFormat.Png)
bt = stream.ToArray()
myImage = New Bitmap(Image.FromStream(stream, True))
End Using
picTravel_Info_Hotel.Tag = bt
picTravel_Info_Hotel.BackgroundImage = myImage
End If
End Sub
和用于从数据库到图片框获取图像的代码是,这里我得到了错误
Dim BArray As Byte() = CType(dt.Rows(0)("Room_Image"), Byte())
Using MS As New System.IO.MemoryStream(BArray)
picTravel_Info_Hotel.Tag = BArray
MS.Position = 0
picTravel_Info_Hotel.BackgroundImage = New Bitmap(Image.FromStream(MS, True))
MS.Dispose()
End Using
当我从数据库中获取图像时,我在某些PC上的错误并非在每台PC上都出现了“ gdi +中发生通用错误 ”。 提前致谢 :)
这段代码中有几个错误会导致该异常。 如果在If()语句中缺少对img对象的Dispose()调用,则会造成麻烦。 它将使文件处于锁定状态的时间无法预测,尝试覆盖文件将导致此异常。
但是主要的错误是:
Using MS As New System.IO.MemoryStream(BArray)
Using语句不正确,它使流不可访问。 调用FromStream()方法后 ,Image类可以访问流。 以典型的.NET惰性方式,仅在需要时执行代码。 这并非始终如一地完成,仅会延迟访问某些类型的图像。 如果这样做,当发现该流不再可用并通过GenericException报告它时,它将遭受心脏病发作。 您必须保持流可用,直到无法再使用该图像为止。 请注意,您在多个地方都犯了这个错误。 删除使用修复,不要打扰它。
查看此链接http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html
问题是将临时文件写入磁盘时权限不足? 我看到ConvertImageToRGBFormat,这是否会将图像保存到磁盘,以便完成转换? 保存到数据库之前,请确保包含图像的文件夹具有写权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.