我试图通过进程名抢窗口和重点,然后取它的截图。 它完美的作品,除非我有开放的TeamViewer(甚至没有在使用的TeamViewer到屏幕分享,就在TeamViewer是运行)
当TeamViewer是运行窗口不集中或推到前台,并RECT这截图是非常小(33x21),而正常情况下这将是1600×900。
这是有问题的代码:
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);