通用 GDI+错误
我得到以下代码的通用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能有帮助,也可能没有帮助,但据我所知,您几乎没有清理此方法中分配的任何资源。即使这没有帮助,最好的做法也是正确的做法是清理一次性对象并且不依赖 GC。
也就是说,.NET 框架中的“经典”图像支持(即 System.Drawing.Image)主要是对本机 GDI/GDI+ 库的包装,并且容易泄漏非托管资源。
我建议将
img
和gfx
对象包装在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
andgfx
objects in ausing
block, as well as explicitly deleting thehObject
handle with an interop call to DeleteObject, enclosed in a try/finally block in caseCreateBitmapSourceFromHBitmap
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.