Пример #1
0
        public static Bitmap GetWindowBitmap(IntPtr hwnd, Rect rect)
        {
            var hdc     = Win32GDIWindow.BeginPaint(hwnd, out LPPAINTSTRUCT lps);
            var hdcMem  = Win32GDI.CreateCompatibleDC(hdc);
            var hbitmap = Win32GDI.CreateCompatibleBitmap(hdc, rect.Width, rect.Height);

            Win32GDIWindow.ReleaseDC(hwnd, hdc);
            Win32GDI.SelectObject(hdcMem, hbitmap);
            Win32GDIWindow.PrintWindow(hwnd, hdcMem, 0);
            var bmp = Image.FromHbitmap(hbitmap);

            Win32GDI.DeleteDC(hdcMem);
            Win32GDIWindow.DeleteObject(hbitmap);
            lps.rcPaint = new Rect {
                Top = 0, Left = 0, Bottom = rect.Height, Right = rect.Width
            };
            Win32GDIWindow.EndPaint(hwnd, ref lps);
            return(bmp);
        }
Пример #2
0
        public static Bitmap GetWindowBitmap(IntPtr hwnd)
        {
            var hdc = Win32GDIWindow.BeginPaint(hwnd, out LPPAINTSTRUCT lps);
            var rec = new Rect();

            Win32Hand.GetWindowRect(hwnd, out rec);
            // IntPtr hdc = WindowExternal.GetDC(hwnd);
            var hdcMem  = Win32GDI.CreateCompatibleDC(hdc);
            var hbitmap = Win32GDI.CreateCompatibleBitmap(hdc, rec.Width, rec.Height);

            Win32GDIWindow.ReleaseDC(hwnd, hdc);
            Win32GDI.SelectObject(hdcMem, hbitmap);
            //可能造成窗体刷新失败
            Win32GDIWindow.PrintWindow(hwnd, hdcMem, 0);
            //开启dwm后可以BitBlt获得离屏表面
            //GDIExternal.BitBlt(hdcMem, 0, 0, rec.Width, rec.Height,
            //    hdc, 0, 0, TernaryRasterOperations.SRCCOPY);
            var bmp = Image.FromHbitmap(hbitmap);

            Win32GDI.DeleteDC(hdcMem);
            Win32GDIWindow.DeleteObject(hbitmap);
            lps.rcPaint = new Rect {
                Top = 0, Left = 0, Bottom = rec.Height, Right = rec.Width
            };
            Win32GDIWindow.EndPaint(hwnd, ref lps);

            #region 预处理

            //IntPtr hdcMem;
            //IntPtr ip = InnerPaintWindowsCapture(hwnd,out hdcMem);
            //Bitmap bmp = Image.FromHbitmap(ip);
            //WindowExternal.DeleteObject(ip);
            //GDIExternal.DeleteDC(hdcMem);

            #endregion

            //if (collect.Count<50)
            //{
            //    collect.Add(bmp);
            //}
            return(bmp);
        }