/// <summary>
        /// Captures a screenshot of the specified window at the specified
        /// bitmap size. <para/>NOTE: This method will not accurately capture controls
        /// that are hidden or obstructed (partially or completely) by another control (e.g. hidden tabs,
        /// or MDI child windows that are obstructed by other child windows/forms).
        /// </summary>
        /// <param name="hwnd">The window handle.</param>
        /// <param name="bitmapSize">The requested bitmap size.</param>
        /// <returns>A screen capture of the window.</returns>
        public static Bitmap GrabWindowBitmap(IntPtr hwnd, System.Drawing.Size bitmapSize)
        {
            if (bitmapSize.Height <= 0 || bitmapSize.Width <= 0)
            {
                return(null);
            }

            IntPtr   windowDC = IntPtr.Zero;
            IntPtr   targetDC = IntPtr.Zero;
            Graphics targetGr = null;

            try
            {
                System.Drawing.Size realWindowSize;
                TabbedThumbnailNativeMethods.GetClientSize(hwnd, out realWindowSize);

                if (realWindowSize == System.Drawing.Size.Empty)
                {
                    realWindowSize = new System.Drawing.Size(200, 200);
                }

                windowDC = TabbedThumbnailNativeMethods.GetWindowDC(hwnd);

                Bitmap targetBitmap = null;

                if (bitmapSize == System.Drawing.Size.Empty)
                {
                    targetBitmap = new Bitmap(realWindowSize.Width, realWindowSize.Height);
                }
                else
                {
                    targetBitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height);
                }

                targetGr = Graphics.FromImage(targetBitmap);

                targetDC = targetGr.GetHdc();
                uint operation = 0x00CC0020 /*SRCCOPY*/;

                System.Drawing.Size ncArea = WindowUtilities.GetNonClientArea(hwnd);

                bool success = TabbedThumbnailNativeMethods.StretchBlt(targetDC, 0, 0, targetBitmap.Width, targetBitmap.Height,
                                                                       windowDC, ncArea.Width, ncArea.Height, realWindowSize.Width, realWindowSize.Height, operation);
                return(targetBitmap);
            }
            finally
            {
                if (windowDC != IntPtr.Zero)
                {
                    TabbedThumbnailNativeMethods.ReleaseDC(hwnd, windowDC);
                }
                if (targetGr != null && targetDC != IntPtr.Zero)
                {
                    targetGr.ReleaseHdc(targetDC);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Captures a screenshot of the specified window at the specified
        /// bitmap size. <para/>NOTE: This method will not accurately capture controls
        /// that are hidden or obstructed (partially or completely) by another control (e.g. hidden tabs,
        /// or MDI child windows that are obstructed by other child windows/forms).
        /// </summary>
        /// <param name="windowHandle">The window handle.</param>
        /// <param name="bitmapSize">The requested bitmap size.</param>
        /// <returns>A screen capture of the window.</returns>
        public static Bitmap GrabWindowBitmap(IntPtr windowHandle, System.Drawing.Size bitmapSize)
        {
            if (bitmapSize.Height <= 0 || bitmapSize.Width <= 0)
            {
                return(null);
            }

            IntPtr windowDC = IntPtr.Zero;

            try
            {
                windowDC = TabbedThumbnailNativeMethods.GetWindowDC(windowHandle);

                System.Drawing.Size realWindowSize;
                TabbedThumbnailNativeMethods.GetClientSize(windowHandle, out realWindowSize);

                if (realWindowSize == System.Drawing.Size.Empty)
                {
                    realWindowSize = new System.Drawing.Size(200, 200);
                }

                System.Drawing.Size size = (bitmapSize == System.Drawing.Size.Empty) ?
                                           realWindowSize : bitmapSize;

                Bitmap targetBitmap = null;
                try
                {
                    targetBitmap = new Bitmap(size.Width, size.Height);

                    using (Graphics targetGr = Graphics.FromImage(targetBitmap))
                    {
                        IntPtr targetDC  = targetGr.GetHdc();
                        uint   operation = 0x00CC0020 /*SRCCOPY*/;

                        System.Drawing.Size ncArea = WindowUtilities.GetNonClientArea(windowHandle);

                        bool success = TabbedThumbnailNativeMethods.StretchBlt(
                            targetDC, 0, 0, targetBitmap.Width, targetBitmap.Height,
                            windowDC, ncArea.Width, ncArea.Height, realWindowSize.Width,
                            realWindowSize.Height, operation);

                        targetGr.ReleaseHdc(targetDC);

                        if (!success)
                        {
                            return(null);
                        }

                        return(targetBitmap);
                    }
                }
                catch
                {
                    if (targetBitmap != null)
                    {
                        targetBitmap.Dispose();
                    }
                    throw;
                }
            }
            finally
            {
                if (windowDC != IntPtr.Zero)
                {
                    TabbedThumbnailNativeMethods.ReleaseDC(windowHandle, windowDC);
                }
            }
        }