Exemplo n.º 1
0
        public static Image GrabScreen(IntPtr hWnd, Point location, Size size)
        {
            Image    myImage           = new Bitmap(size.Width, size.Height);
            Graphics g                 = Graphics.FromImage(myImage);
            IntPtr   destDeviceContext = g.GetHdc();
            IntPtr   srcDeviceContext  = Win32API.GetWindowDC(hWnd);

            Win32API.BitBlt(destDeviceContext, 0, 0, size.Width, size.Height, srcDeviceContext, location.X, location.Y, Win32API.SRCCOPY);
            Win32API.ReleaseDC(hWnd, srcDeviceContext);
            g.ReleaseHdc(destDeviceContext);

            return(myImage);
        }
Exemplo n.º 2
0
        private Image GetScreenShot()
        {
            IntPtr windowHandle = Win32API.GetDesktopWindow();
            RECT   desktopRect;

            Win32API.GetWindowRect(windowHandle, out desktopRect);

            Image    myImage           = new Bitmap(desktopRect.Width, desktopRect.Height);
            Graphics g                 = Graphics.FromImage(myImage);
            IntPtr   destDeviceContext = g.GetHdc();
            IntPtr   srcDeviceContext  = Win32API.GetWindowDC(windowHandle);

            Win32API.BitBlt(destDeviceContext, 0, 0, desktopRect.Width, desktopRect.Height, srcDeviceContext, 0, 0, Win32API.SRCCOPY);
            Win32API.ReleaseDC(windowHandle, srcDeviceContext);
            g.ReleaseHdc(destDeviceContext);
            return(myImage);
        }
Exemplo n.º 3
0
        public static Bitmap CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            var hdcSrc = Win32API.GetWindowDC(handle);

            // get the size
            var windowRect = new RECT();

            Win32API.GetWindowRect(handle, out windowRect);

            int width  = (int)Math.Round((windowRect.Right - windowRect.Left) / Helper.Rate);
            int height = (int)Math.Round((windowRect.Bottom - windowRect.Top) / Helper.Rate);

            // create a device context we can copy to
            var hdcDest = Win32API.CreateCompatibleDC(hdcSrc);

            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            var hBitmap = Win32API.CreateCompatibleBitmap(hdcSrc, width, height);

            // select the bitmap object
            var hOld = Win32API.SelectObject(hdcDest, hBitmap);

            // bitblt over
            Win32API.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);

            // restore selection
            Win32API.SelectObject(hdcDest, hOld);

            // clean up
            Win32API.DeleteDC(hdcDest);
            Win32API.ReleaseDC(handle, hdcSrc);

            // get a .NET image object for it
            Bitmap img = System.Drawing.Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            Win32API.DeleteObject(hBitmap);

            return(img);
        }
Exemplo n.º 4
0
        /* Image CaptureWindow(IntPtr handle)
         * return an image of expected handle.
         */
        public static Image CaptureWindow(IntPtr handle)
        {
            try
            {
                if (handle == IntPtr.Zero)
                {
                    throw new Exception("Handle can not be 0.");
                }

                // get the size
                Win32API.Rect windowRect = new Win32API.Rect();
                Win32API.GetWindowRect(handle, ref windowRect);
                int width  = windowRect.right - windowRect.left;
                int height = windowRect.bottom - windowRect.top;

                if (width > 0 && height > 0)
                {
                    bool printed = false;

                    //firstly, try PrintWindow to get the image of the window.
                    Bitmap bm = new Bitmap(width, height);
                    using (Graphics g = Graphics.FromImage(bm))
                    {
                        System.IntPtr bmDC = g.GetHdc();
                        printed = Win32API.PrintWindow(handle, bmDC, 0);
                        g.ReleaseHdc(bmDC);
                    }

                    if (printed)
                    {
                        return(bm);
                    }
                    else
                    {
                        //not printed, try other way.

                        // get te hDC of the target window
                        IntPtr hdcSrc = Win32API.GetWindowDC(handle);

                        // create a device context we can copy to
                        IntPtr hdcDest = Win32API.CreateCompatibleDC(hdcSrc);
                        // create a bitmap we can copy it to,
                        IntPtr hBitmap = Win32API.CreateCompatibleBitmap(hdcSrc, width, height);
                        // select the bitmap object
                        IntPtr hOld = Win32API.SelectObject(hdcDest, hBitmap);
                        // bitblt over
                        Win32API.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Win32API.SRCCOPY);
                        // restore selection
                        Win32API.SelectObject(hdcDest, hOld);
                        // clean up
                        Win32API.DeleteDC(hdcDest);
                        Win32API.ReleaseDC(handle, hdcSrc);

                        // get a .NET image object for it
                        Image img = Image.FromHbitmap(hBitmap);
                        // free up the Bitmap object
                        Win32API.DeleteObject(hBitmap);

                        return(img);
                    }
                }
                else
                {
                    throw new Exception("Can not get size infomation of the window.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }