我想对Windows 7 64位中的进程窗口进行截屏,问题是我总是在下面这行代码中遇到错误:
var bmp = new Bitmap (width, height, PixelFormat.Format32bppArgb);
当出现“无效参数”时,我使用throw来查看错误,发现宽度和高度始终为0。
之前在32位系统中可以正常工作,但是在64位系统中不再有效。
代码如下:
public void CaptureApplication()
{
string procName = "firefox";
var proc = Process.GetProcessesByName(procName)[0];
var rect = new User32.Rect();
User32.GetWindowRect(proc.MainWindowHandle, ref rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bmp);
graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
bmp.Save("c:\\tmp\\test.png", ImageFormat.Png);
}
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
}
我该如何修复这个错误?
GetWindowRect
的返回值——如果它返回IntPtr.Zero
,则在该步骤中出现了错误。 - C.Evenhuis