public static System.Windows.Media.Imaging.WriteableBitmap CaptureWindow(IntPtr hwnd) { if (!User32DllMethodsInvoker.IsWindow(hwnd)) { return(null); } // このプロセスでの処理中に他プロセスのターゲット ウィンドウ ハンドルが無効化されることは十分にありうる。 // 例外を投げたりしないで、各 API の戻り値を随時チェックしていくほうが無難。 // アサーションも失敗させない。 var winRect = new Win32Commons.RECT(); bool retval = User32DllMethodsInvoker.GetWindowRect(hwnd, ref winRect); if (!retval || winRect.Width <= 0 || winRect.Height <= 0) { return(null); } using (var gdipBitmap = CaptureWindow(hwnd, winRect.Width, winRect.Height)) { System.Diagnostics.Debug.Assert(gdipBitmap != null); System.Diagnostics.Debug.Assert(gdipBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb); // TODO: DPI はどうする? 高 DPI 設定の場合でも論理ピクセルではなく実ピクセル単位で画像データを取得するべき。 var wicBitmap = new System.Windows.Media.Imaging.WriteableBitmap(winRect.Width, winRect.Height, MyDeviceHelper.DefaultDpi, MyDeviceHelper.DefaultDpi, System.Windows.Media.PixelFormats.Pbgra32, null); CopyGdipBitmapToWicBitmap(gdipBitmap, wicBitmap); return(wicBitmap); } }
public static extern bool SystemParametersInfo(SystemParametersInfoType uiAction, UInt32 uiParam, ref Win32Commons.RECT pvParam, UInt32 fWinIni);
public static extern bool GetClientRect(IntPtr hwnd, ref Win32Commons.RECT lpRect);