【发布时间】:2011-04-29 00:37:44
【问题描述】:
我得到以下代码的通用 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
【问题讨论】: