예제 #1
0
        private static Bitmap GetWindowBitmapBitBltOnDesktop(Rectangle rectangle)
        {
            Bitmap bitmap;

            NativeMethodsEx.RECT rEct = new NativeMethodsEx.RECT()
            {
                Left   = rectangle.Left,
                Top    = rectangle.Top,
                Right  = rectangle.Right,
                Bottom = rectangle.Bottom
            };
            NativeMethodsEx.RECT rECT1 = rEct;
            if (rECT1.Height == 0 || rECT1.Width == 0)
            {
                return(null);
            }
            IntPtr zero   = IntPtr.Zero;
            IntPtr dC     = IntPtr.Zero;
            IntPtr intPtr = IntPtr.Zero;

            try
            {
                Bitmap bitmap1 = null;
                dC     = NativeMethodsEx.User32.GetDC(IntPtr.Zero);
                zero   = NativeMethodsEx.Gdi32.CreateCompatibleDC(dC);
                intPtr = NativeMethodsEx.Gdi32.CreateCompatibleBitmap(dC, rECT1.Width, rECT1.Height);
                if (intPtr != IntPtr.Zero)
                {
                    IntPtr intPtr1 = NativeMethodsEx.Gdi32.SelectObject(zero, intPtr);
                    NativeMethodsEx.Gdi32.BitBlt(zero, 0, 0, rECT1.Width, rECT1.Height, dC, rECT1.Left, rECT1.Top, 80478240);
                    NativeMethodsEx.Gdi32.SelectObject(zero, intPtr1);
                    bitmap1 = Image.FromHbitmap(intPtr);
                }
                bitmap = bitmap1;
            }
            finally
            {
                if (dC != IntPtr.Zero)
                {
                    NativeMethodsEx.User32.ReleaseDC(IntPtr.Zero, dC);
                }
                if (zero != IntPtr.Zero)
                {
                    NativeMethodsEx.Gdi32.DeleteDC(zero);
                }
                if (intPtr != IntPtr.Zero)
                {
                    NativeMethodsEx.Gdi32.DeleteObject(intPtr);
                }
            }
            return(bitmap);
        }
예제 #2
0
        private static Bitmap GetWindowBitmap(Rectangle rectangle, IntPtr handle)
        {
            NativeMethodsEx.RECT rECT;
            Bitmap bitmap;
            IntPtr dC     = NativeMethodsEx.User32.GetDC(handle);
            IntPtr intPtr = handle;
            bool   flag   = false;

            //Rectangle rectangle = this.Rectangle;
            NativeMethodsEx.RECT rECT1 = new NativeMethodsEx.RECT
            {
                Left   = rectangle.Left,
                Top    = rectangle.Top,
                Right  = rectangle.Right,
                Bottom = rectangle.Bottom
            };
            NativeMethodsEx.RECT x = rECT1;
            if (x.Height == 0 || x.Width == 0)
            {
                return(null);
            }
            if (flag)
            {
                Point point  = new Point(x.Left, x.Top);
                Point point1 = new Point(x.Right, x.Bottom);
                NativeMethodsEx.User32.ClientToScreen(intPtr, ref point);
                NativeMethodsEx.User32.ClientToScreen(intPtr, ref point1);
                x.Left   = point.X;
                x.Top    = point.Y;
                x.Right  = point1.X;
                x.Bottom = point1.Y;
            }
            IntPtr desktopWindow = NativeMethodsEx.User32.GetDesktopWindow();

            NativeMethodsEx.RECT rECT2 = new NativeMethodsEx.RECT();
            NativeMethodsEx.User32.GetWindowRect(desktopWindow, ref rECT2);
            if (!NativeMethodsEx.User32.IntersectRect(out rECT, ref rECT2, ref x))
            {
                rECT = x;
            }
            if (NativeMethodsEx.User32.IsRectEmpty(ref rECT))
            {
                return(null);
            }
            int    width    = rECT.Width;
            int    height   = rECT.Height;
            IntPtr zero     = IntPtr.Zero;
            IntPtr windowDC = IntPtr.Zero;
            IntPtr zero1    = IntPtr.Zero;

            try
            {
                Bitmap bitmap1 = null;
                windowDC = NativeMethodsEx.User32.GetWindowDC(intPtr);
                zero     = NativeMethodsEx.Gdi32.CreateCompatibleDC(windowDC);
                zero1    = NativeMethodsEx.Gdi32.CreateCompatibleBitmap(windowDC, width, height);
                if (zero1 != IntPtr.Zero)
                {
                    int    num     = 0;
                    int    num1    = 0;
                    IntPtr intPtr1 = NativeMethodsEx.Gdi32.SelectObject(zero, zero1);
                    NativeMethodsEx.Gdi32.BitBlt(zero, 0, 0, width, height, windowDC, num, num1, 80478240);
                    NativeMethodsEx.Gdi32.SelectObject(zero, intPtr1);
                    bitmap1 = Image.FromHbitmap(zero1);
                }
                bitmap = bitmap1;
            }
            finally
            {
                if (windowDC != IntPtr.Zero)
                {
                    NativeMethodsEx.User32.ReleaseDC(intPtr, windowDC);
                }
                if (zero != IntPtr.Zero)
                {
                    NativeMethodsEx.Gdi32.DeleteDC(zero);
                }
                if (zero1 != IntPtr.Zero)
                {
                    NativeMethodsEx.Gdi32.DeleteObject(zero1);
                }
            }
            return(bitmap);
        }