我试图通过进程名称抓取一个窗口并将其聚焦,然后对其进行截图。除非我打开 Teamviewer,否则它工作得很好(即使在使用 teamviewer 进行屏幕共享时,只是在 teamviewer 运行时)
当 teamviewer 运行时,窗口没有聚焦或被带到前台,它截取的矩形非常小(33x21),通常为 1600x900。
这是有问题的代码:
proc = Process.GetProcessesByName(procName)[0];
SetForegroundWindow(proc.MainWindowHandle);
ShowWindow(proc.MainWindowHandle, SW_RESTORE);
Rect rect = new Rect();
GetWindowRect(proc.MainWindowHandle, ref rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics.FromImage(bmp).CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
这是我获得这些功能的地方:
[DllImport("user32.dll")]
private static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);