Exemple #1
0
        private static Bitmap MakeSnapshot(IntPtr AppWndHandle, bool IsClientWnd, Win32API.WindowShowStyle nCmdShow)
        {
            if (AppWndHandle == IntPtr.Zero || !Win32API.IsWindow(AppWndHandle) || !Win32API.IsWindowVisible(AppWndHandle))
            {
                return(null);
            }
            if (Win32API.IsIconic(AppWndHandle))
            {
                Win32API.ShowWindow(AppWndHandle, nCmdShow);//show it
            }
            if (!Win32API.SetForegroundWindow(AppWndHandle))
            {
                return(null);   //can't bring it to front
            }
            Thread.Sleep(1000); //give it some time to redraw
            RECT appRect;
            bool res = IsClientWnd ? Win32API.GetClientRect(AppWndHandle, out appRect) : Win32API.GetWindowRect(AppWndHandle, out appRect);

            if (!res || appRect.Height == 0 || appRect.Width == 0)
            {
                return(null);//some hidden window
            }
            if (IsClientWnd)
            {
                System.Drawing.Point lt = new System.Drawing.Point(appRect.Left, appRect.Top);
                System.Drawing.Point rb = new System.Drawing.Point(appRect.Right, appRect.Bottom);
                Win32API.ClientToScreen(AppWndHandle, ref lt);
                Win32API.ClientToScreen(AppWndHandle, ref rb);
                appRect.Left   = lt.X;
                appRect.Top    = lt.Y;
                appRect.Right  = rb.X;
                appRect.Bottom = rb.Y;
            }
            IntPtr DesktopHandle = Win32API.GetDesktopWindow();
            RECT   desktopRect;

            Win32API.GetWindowRect(DesktopHandle, out desktopRect);
            RECT visibleRect;

            if (!Win32API.IntersectRect(out visibleRect, ref desktopRect, ref appRect))
            {
                visibleRect = appRect;
            }
            if (Win32API.IsRectEmpty(ref visibleRect))
            {
                return(null);
            }

            int    Width   = visibleRect.Width;
            int    Height  = visibleRect.Height;
            IntPtr hdcTo   = IntPtr.Zero;
            IntPtr hdcFrom = IntPtr.Zero;
            IntPtr hBitmap = IntPtr.Zero;

            try
            {
                Bitmap clsRet = null;

                hdcFrom = IsClientWnd ? Win32API.GetDC(AppWndHandle) : Win32API.GetWindowDC(AppWndHandle);

                hdcTo   = Win32API.CreateCompatibleDC(hdcFrom);
                hBitmap = Win32API.CreateCompatibleBitmap(hdcFrom, Width, Height);

                if (hBitmap != IntPtr.Zero)
                {
                    int    x            = appRect.Left < 0 ? -appRect.Left : 0;
                    int    y            = appRect.Top < 0 ? -appRect.Top : 0;
                    IntPtr hLocalBitmap = Win32API.SelectObject(hdcTo, hBitmap);
                    Win32API.BitBlt(hdcTo, 0, 0, Width, Height, hdcFrom, x, y, Win32API.SRCCOPY);
                    Win32API.SelectObject(hdcTo, hLocalBitmap);
                    clsRet = Image.FromHbitmap(hBitmap);
                }
                return(clsRet);
            }
            finally
            {
                if (hdcFrom != IntPtr.Zero)
                {
                    Win32API.ReleaseDC(AppWndHandle, hdcFrom);
                }
                if (hdcTo != IntPtr.Zero)
                {
                    Win32API.DeleteDC(hdcTo);
                }
                if (hBitmap != IntPtr.Zero)
                {
                    Win32API.DeleteObject(hBitmap);
                }
            }
        }
Exemple #2
0
        public static Bitmap GetWindowSnapshot(IntPtr AppWndHandle, bool IsClientWnd, Win32API.WindowShowStyle nCmdShow)
        {
            if (AppWndHandle == IntPtr.Zero || !Win32API.IsWindow(AppWndHandle) ||
                !Win32API.IsWindowVisible(AppWndHandle))
            {
                return(null);
            }
            if (Win32API.IsIconic(AppWndHandle))
            {
                Win32API.ShowWindow(AppWndHandle, nCmdShow);                //show it
            }
            if (!Win32API.SetForegroundWindow(AppWndHandle))
            {
                return(null);                    //can't bring it to front
            }
            System.Threading.Thread.Sleep(1000); //give it some time to redraw
            RECT appRect;
            bool res = IsClientWnd ? Win32API.GetClientRect
                           (AppWndHandle, out appRect) : Win32API.GetWindowRect
                           (AppWndHandle, out appRect);

            if (!res || appRect.Height == 0 || appRect.Width == 0)
            {
                return(null);               //some hidden window
            }
            // calculate the app rectangle
            if (IsClientWnd)
            {
                Point lt = new Point(appRect.Left, appRect.Top);
                Point rb = new Point(appRect.Right, appRect.Bottom);
                Win32API.ClientToScreen(AppWndHandle, ref lt);
                Win32API.ClientToScreen(AppWndHandle, ref rb);
                appRect.Left   = lt.X;
                appRect.Top    = lt.Y;
                appRect.Right  = rb.X;
                appRect.Bottom = rb.Y;
            }
            //Intersect with the Desktop rectangle and get what's visible
            IntPtr DesktopHandle = Win32API.GetDesktopWindow();
            RECT   desktopRect;

            Win32API.GetWindowRect(DesktopHandle, out desktopRect);
            RECT visibleRect;

            if (!Win32API.IntersectRect
                    (out visibleRect, ref desktopRect, ref appRect))
            {
                visibleRect = appRect;
            }
            if (Win32API.IsRectEmpty(ref visibleRect))
            {
                return(null);
            }

            int    Width   = visibleRect.Width;
            int    Height  = visibleRect.Height;
            IntPtr hdcTo   = IntPtr.Zero;
            IntPtr hdcFrom = IntPtr.Zero;
            IntPtr hBitmap = IntPtr.Zero;

            try
            {
                Bitmap clsRet = null;

                // get device context of the window...
                hdcFrom = IsClientWnd ? Win32API.GetDC(AppWndHandle) :
                          Win32API.GetWindowDC(AppWndHandle);

                // create dc that we can draw to...
                hdcTo   = Win32API.CreateCompatibleDC(hdcFrom);
                hBitmap = Win32API.CreateCompatibleBitmap(hdcFrom, Width, Height);

                //  validate
                if (hBitmap != IntPtr.Zero)
                {
                    // adjust and copy
                    int    x            = appRect.Left < 0 ? -appRect.Left : 0;
                    int    y            = appRect.Top < 0 ? -appRect.Top : 0;
                    IntPtr hLocalBitmap = Win32API.SelectObject(hdcTo, hBitmap);
                    Win32API.BitBlt(hdcTo, 0, 0, Width, Height,
                                    hdcFrom, x, y, Win32API.SRCCOPY);
                    Win32API.SelectObject(hdcTo, hLocalBitmap);
                    //  create bitmap for window image...
                    clsRet = System.Drawing.Image.FromHbitmap(hBitmap);
                }
                return(clsRet);
            }
            finally
            {
                //  release the unmanaged resources
                if (hdcFrom != IntPtr.Zero)
                {
                    Win32API.ReleaseDC(AppWndHandle, hdcFrom);
                }
                if (hdcTo != IntPtr.Zero)
                {
                    Win32API.DeleteDC(hdcTo);
                }
                if (hBitmap != IntPtr.Zero)
                {
                    Win32API.DeleteObject(hBitmap);
                }
            }
        }