通用 GDI+错误

发布于 2024-11-03 16:21:08 字数 909 浏览 0 评论 0原文

我得到以下代码的通用 GDI 错误。通常它会很好地遵守并执行,但有时会失败并出现通用 GDI+ 错误。有什么办法可以解决这个问题,或者有办法在不使用互操作的情况下截取屏幕截图吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2024-11-10 16:21:08

这可能有帮助,也可能没有帮助,但据我所知,您几乎没有清理此方法中分配的任何资源。即使这没有帮助,最好的做法也是正确的做法是清理一次性对象并且不依赖 GC。

也就是说,.NET 框架中的“经典”图像支持(即 System.Drawing.Image)主要是对本机 GDI/GDI+ 库的包装,并且容易泄漏非托管资源。

我建议将 imggfx 对象包装在 using 块中,并显式删除 hObject处理对 DeleteObject 的互操作调用,包含在 try/finally 块中,以防万一CreateBitmapSourceFromHBitmap 失败。

...为什么框架在 Image 类上提供了 GetHbitmap 方法,但没有提供无需互操作代码即可删除它的方法,这是一个谜。查看 GetHbitmap 上的 MSDN 文档以获取更多详细信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文