プロセス名でウィンドウを取得してフォーカスし、スクリーンショットを撮ろうとしています。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);