Пример #1
0
        /// <summary>
        /// 可以截取非前端窗体的截图,但是非GDI的程序是无法截图的比如DirectX
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static Bitmap GetWindowCapture(IntPtr hWnd)
        {
            IntPtr hscrdc = WinApi.GetWindowDC(hWnd);
            var windowRect = new WinApi.RECT();
            WinApi.GetWindowRect(hWnd, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            IntPtr hbitmap = WinApi.CreateCompatibleBitmap(hscrdc, width, height);
            IntPtr hmemdc = WinApi.CreateCompatibleDC(hscrdc);
            WinApi.SelectObject(hmemdc, hbitmap);
            WinApi.PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
            WinApi.DeleteDC(hscrdc);//删除用过的对象
            WinApi.DeleteDC(hmemdc);//删除用过的对象
            return bmp;
        }
Пример #2
0
 /// <summary>  
 /// 指定窗口截图   可以截取GDI或者非GDI图形 只不过,非前端窗体图形不能截获...
 /// </summary>  
 /// <param name="handle">窗口句柄. (在windows应用程序中, 从Handle属性获得)</param>  
 /// <returns></returns>  
 public Bitmap CaptureWindow(IntPtr handle)
 {
     IntPtr hdcSrc = WinApi.GetWindowDC(handle);
     WinApi.RECT windowRect = new WinApi.RECT();
     WinApi.GetWindowRect(handle, ref windowRect);
     int width = windowRect.right - windowRect.left;
     int height = windowRect.bottom - windowRect.top;
     IntPtr hdcDest = WinApi.CreateCompatibleDC(hdcSrc);
     IntPtr hBitmap = WinApi.CreateCompatibleBitmap(hdcSrc, width, height);
     IntPtr hOld = WinApi.SelectObject(hdcDest, hBitmap);
     WinApi.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, WinApi.SRCCOPY);
     //WinApi.SelectObject(hdcDest, hOld);
     WinApi.DeleteDC(hdcDest);
     WinApi.ReleaseDC(handle, hdcSrc);
     var img = Bitmap.FromHbitmap(hBitmap);
     WinApi.DeleteObject(hBitmap);
     return img;
 }