다음 코드로 일반 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
확인했는데 코드가 실제로 메모리가 누출되었습니다. 이제 수정되었습니다. CreateBitmapSourceFromHBitmap은 여전히 주기적으로 실패합니다. Try/Catch 블록을 구현하여 제안한대로 정상적으로 실패합니다. 도와 주셔서 감사합니다. – PeterM
@PeterM CreateBitmap 작업 만 다시 시도하면 성공합니까? 아니면 전체 루틴을 다시 실행해야합니까? –