Пример #1
0
        public static Bitmap GetScreenShot(IntPtr windowHandle)
        {
            WindowsFunctions.GetWindowRect(new HandleRef(null, windowHandle), out var rc);

            var    bmp    = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top, PixelFormat.Format32bppArgb);
            var    gfxBmp = Graphics.FromImage(bmp);
            IntPtr hdcBitmap;

            try
            {
                hdcBitmap = gfxBmp.GetHdc();
            }
            catch
            {
                return(null);
            }

            var succeeded = WindowsFunctions.PrintWindow(windowHandle, hdcBitmap, 0x02); /// flag for hardware acceleration window.

            gfxBmp.ReleaseHdc(hdcBitmap);

            if (!succeeded)
            {
                gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
            }

            var hRgn = WindowsFunctions.CreateRectRgn(0, 0, 0, 0);

            WindowsFunctions.GetWindowRgn(windowHandle, hRgn);

            var region = Region.FromHrgn(hRgn); //err here once

            if (!region.IsEmpty(gfxBmp))
            {
                gfxBmp.ExcludeClip(region);
                gfxBmp.Clear(Color.Transparent);
            }

            gfxBmp.Dispose();
            return(bmp);
        }