Esempio n. 1
0
        /// <summary>
        ///     Creates a screenshot from a hidden window.
        /// </summary>
        /// <param name="windowHandle">
        ///     The handle of the window.
        /// </param>
        /// <returns>
        ///     A <see cref="Bitmap"/> of the window.
        /// </returns>
        public static Bitmap CreateFromHidden(IntPtr windowHandle)
        {
            Bitmap bmpScreen = null;

            try {
                Rectangle r;
                using (Graphics windowGraphic = Graphics.FromHdc(new IntPtr(NativeWin32.GetWindowDC(windowHandle)))) {
                    r = Rectangle.Round(windowGraphic.VisibleClipBounds);
                }

                bmpScreen = new Bitmap(r.Width, r.Height);
                using (Graphics g = Graphics.FromImage(bmpScreen)) {
                    IntPtr hdc = g.GetHdc();
                    try {
                        NativeWin32.PrintWindow(windowHandle, hdc, 0);
                    } finally {
                        g.ReleaseHdc(hdc);
                    }
                }
            } catch {
                if (bmpScreen != null)
                {
                    bmpScreen.Dispose();
                }
            }

            return(bmpScreen);
        }