Esempio n. 1
0
        /// <summary>
        /// THis function gets the screen shot of a window..
        /// </summary>
        /// <param name="windowHandle">This is the handle (in IntPtr form) of the window to get the screen shot of.</param>
        /// <returns>Returns a bitmap of the window.</returns>
        private static Bitmap GetWindowScreenShot(IntPtr windowHandle)
        {
            Rectangle windowRect = Rectangle.Empty;

            if (windowHandle == IntPtr.Zero)
            {
                // Desktop image, so set rectangle appropriately.
                windowRect = Screen.PrimaryScreen.Bounds;
            }
            else
            {
                // Check that window handle is valid and get the window size info.
                Win32APIDataTypes.RECT rect = new Win32APIDataTypes.RECT();
                if (!GetWindowRect(windowHandle, ref rect))
                {
                    // Throw exception if window handle is invalid.
                    throw new ArgumentException("The window handle is not valid.");
                }

                // Set up the window rectangle.
                windowRect = Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom);
            }

            // Create a new bitmap the size of the primary display device
            Bitmap bmpScreenShot = new Bitmap(windowRect.Width, windowRect.Height);

            // Get a Graphics object from the bitmap so we can draw on it
            Graphics gfx = Graphics.FromImage(bmpScreenShot);

            // Get a win32 DC (Device Context) for destination blitting
            IntPtr dcDestination = gfx.GetHdc();

            // Now get a source DC from the window handle
            IntPtr dcSource = GetWindowDC(windowHandle);

            // Blit from the desktop's DC to our bitmap's DC
            BitBlt(dcDestination, 0, 0, windowRect.Width, windowRect.Height, dcSource, 0, 0, SourceCopy);

            // Release GDI and GDI+ objects
            gfx.ReleaseHdc(dcDestination);
            ReleaseDC(windowHandle, dcSource);

            return(bmpScreenShot);
        }
Esempio n. 2
0
 private static extern bool GetWindowRect(IntPtr hWnd, ref Win32APIDataTypes.RECT lpRect);