Ejemplo n.º 1
0
        /// <summary>
        ///     Creates a screenshot from a hidden window using a different method
        /// </summary>
        /// <example>
        ///     <code>
        /// <![CDATA[
        /// IntPtr hwnd = ... ;
        /// Bitmap capture = ScreenShot.CreateFromHidden(hwnd);
        /// ]]>
        /// </code>
        /// </example>
        /// <param name="windowHandle">Handle of window</param>
        /// <returns></returns>
        public static Bitmap CreateFromHidden2(IntPtr windowHandle)
        {
            Bitmap bmpScreen = null;

            try
            {
                Rectangle r;
                using (Graphics windowGraphic = Graphics.FromHdc(NativeMethods.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
                    {
                        NativeMethods.PrintWindow(windowHandle, hdc, 0);
                    }
                    finally
                    {
                        g.ReleaseHdc(hdc);
                    }
                }
            }
            catch
            {
                if (bmpScreen != null)
                {
                    bmpScreen.Dispose();
                }
            }
            return(bmpScreen);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// creates a screenshot from a hidden window
        /// </summary>
        /// <example>
        /// <code>
        /// <![CDATA[
        /// IntPtr hwnd = ... ;
        /// Bitmap capture = ScreenShot.CreateFromHidden(hwnd);
        /// ]]>
        /// </code>
        /// </example>
        /// <param name="WindowHandle">handle of window</param>
        /// <returns></returns>
        public static Bitmap CreateFromHidden(IntPtr WindowHandle)
        {
            Bitmap bmpScreen = null;

            try
            {
                Rectangle R = Rectangle.Empty;
                using (Graphics WindowGraphic = System.Drawing.Graphics.FromHdc(NativeMethods.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
                    {
                        NativeMethods.PrintWindow(WindowHandle, Hdc, (uint)0);
                    }
                    finally
                    {
                        g.ReleaseHdc(Hdc);
                    }
                }
            }
            catch
            {
                if (bmpScreen != null)
                {
                    bmpScreen.Dispose();
                }
            }
            return(bmpScreen);
        }