我正在使用以下代码捕获屏幕并将其复制到BitmapSource中.每400ms通过DispatcherTimer连续调用该方法.首先,我在.NET Framework 3.5中使用了此代码,然后切换到Framework 4.0.当程序运行一段时间(假设15分钟)时,它会在GetHBitmap调用期间突然崩溃,出现"GDI +中的通用错误".
当我切换到.NET 4.0时,我不得不注释掉CloseHandle()调用,它引发了一个SEHException.也许这会导致问题,也许不会.
所以,这是我的代码.我希望有人能帮帮忙...
// The code is based on an example by Charles Petzold
// http://www.charlespetzold.com/pwcs/ReadingPixelsFromTheScreen.html
// Import external Win32 functions
// BitBlt is used for the bit by bit block copy of the screen content
[DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hdcDst, int xDst, int yDst, int cx, int cy,
IntPtr hdcSrc, int xSrc, int ySrc, uint ulRop);
// DeleteObject is used to delete the bitmap handle
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject); …Run Code Online (Sandbox Code Playgroud)