我有以下C#代码,我用它来捕获远程桌面(RDP)会话中的屏幕截图。它在会话处于活动状态时工作正常,但如果我最小化会话,则会因无效的句柄异常而失败。
有没有办法让这项工作成功,或者当会话最小化时屏幕基本上“消失了”?
string filename = @"C:\Snap.png";
Size bitmapSize = new Size( Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height );
using (Bitmap bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height, PixelFormat.Format24bppRgb))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen( // Exception thrown here
new Point(0, 0),
new Point(0, 0),
bitmapSize);
bitmap.Save(filename, ImageFormat.Png);
}