通用GDI +错误错误、GDI

2023-09-06 06:35:58 作者:有时间一起睡觉啊

我得到的一般GDI错误有以下code。通常它遵守和执行得很好,但有时会失败,一个普通的GDI +错误。是否有某种方式来解决这个问题,还是有办法采取的截图,而无需使用跨运?

 公共功能CopyImageFromScreen(地区Int32Rect)作为的BitmapSource

    昏暗的IMG作为新System.Drawing.Bitmap(region.Width,region.Height)
    昏暗的GFX作为System.Drawing.Graphics = System.Drawing.Graphics.FromImage(IMG)
    gfx.CopyFromScreen(region.X,region.Y,0,0,新System.Drawing.Size(region.Width,region.Height))
    img.LockBits(新System.Drawing.Rectangle(0,0,img.Width,img.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,img.PixelFormat)

    昏暗hObject作为IntPtr的= img.GetHbitmap'这行导致的错误

    昏暗WpfImage作为的BitmapSource = Interop.Imaging.CreateBitmapSourceFromHBitmap(hObject,IntPtr.Zero,_
        System.Windows.Int32Rect.Empty,System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())

    返回WpfImage

端功能
 

解决方案

这可能会或可能不会帮助,但不久我可以告诉你不清理任何分配这个方法的资源。即使这没有帮助,这是最好的实践和适当的清理处置的对象,并不能依靠GC。

C picturebox覆盖保存GDI 中发生一般性错误

这是说,经典形​​象支持(即System.Drawing.Image对象)在.NET框架是主要的包装在机GDI / GDI +库,而且容易泄漏非托管资源。

我所说的其实是包裹 IMG GFX 使用块,以及明确地删除 hObject 与互操作调用的 DeleteObject ,封闭在一个尝试,以防/ finally块 CreateBitmapSourceFromHBitmap 失败。

...为什么框架提供了GetHbitmap方法对图像类,而不是一种方法来删除它没有互操作code是一个谜。检查出GetHbitmap MSDN文档有关的更多细节。

I get as Generic GDI Error with the following code. Usually it complies and executes just fine but sometimes fails with a Generic GDI+ Error. Is there some way to solve the issue, or a way to take a screenshot without using inter-op?

Public Function CopyImageFromScreen(region as Int32Rect) As BitmapSource

    Dim img As New System.Drawing.Bitmap(region.Width, region.Height)
    Dim gfx As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(img)
    gfx.CopyFromScreen(region.X, region.Y, 0, 0, New System.Drawing.Size(region.Width, region.Height))
    img.LockBits(New System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, img.PixelFormat)

    Dim hObject As IntPtr = img.GetHbitmap 'This Line Causes the Error

    Dim WpfImage As BitmapSource = Interop.Imaging.CreateBitmapSourceFromHBitmap(hObject, IntPtr.Zero, _
        System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions())

    Return WpfImage

End Function

解决方案

This may or may not help, but near as I can tell you are not cleaning up any of the resources allocated in this method. Even if this doesn't help, it's best practice and proper to clean up disposable objects and to not rely on the GC.

That said, "classic" image support (i.e. System.Drawing.Image) in the .NET framework is mostly a wrapper over the native GDI/GDI+ libraries and are prone to leaking unmanaged resources.

What I suggest is wrapping the img and gfx objects in a using block, as well as explicitly deleting the hObject handle with an interop call to DeleteObject, enclosed in a try/finally block in case CreateBitmapSourceFromHBitmap fails.

...why the framework provides a GetHbitmap method on the Image class, but not a way to delete it without interop code is a mystery. Check out the MSDN docs on GetHbitmap for more details on that.