Пример #1
0
        /// <summary>
        /// Get's the client area of a given window handle.
        /// </summary>
        public static Bitmap GetWindowImage(IntPtr hwnd)
        {
            //Variable to keep the handle to bitmap.
            IntPtr hBitmap;

            //Here the passed handle to the window and pass it to the varaible used for this function
            IntPtr hDC = PlatformInvokeUSER32.GetDC(hwnd);

            //Here we make a compatible device context in memory for screen
            //device context.
            IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

            Size s      = WC.winGetClientSize(hwnd);
            int  width  = s.Width;
            int  height = s.Height;

            //We create a compatible bitmap of the screen size and using
            //the screen device context.
            hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap
                          (hDC, width, height);

            //As hBitmap is IntPtr, we cannot check it against null.
            //For this purpose, IntPtr.Zero is used.
            if (hBitmap != IntPtr.Zero)
            {
                //Here we select the compatible bitmap in the memeory device
                //context and keep the refrence to the old bitmap.
                IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject
                                  (hMemDC, hBitmap);
                //We copy the Bitmap to the memory device context.
                PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, width, height, hDC,
                                           0, 0, PlatformInvokeGDI32.SRCCOPY);
                //We select the old bitmap back to the memory device context.
                PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
                //We delete the memory device context.
                PlatformInvokeGDI32.DeleteDC(hMemDC);
                //We release the screen device context.
                PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.
                                               GetDesktopWindow(), hDC);
                //Image is created by Image bitmap handle and stored in
                //local variable.
                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
                //Release the memory to avoid memory leaks.
                PlatformInvokeGDI32.DeleteObject(hBitmap);
                //This statement runs the garbage collector manually.
                GC.Collect();
                //Return the bitmap
                return(bmp);
            }
            //If hBitmap is null, retun null.
            return(null);
        }
Пример #2
0
        public static Bitmap GetWindowImage(IntPtr hwnd, int x, int y, int width, int height)
        {
            IntPtr dC  = PlatformInvokeUSER32.GetDC(hwnd);
            IntPtr hdc = PlatformInvokeGDI32.CreateCompatibleDC(dC);
            IntPtr bmp = PlatformInvokeGDI32.CreateCompatibleBitmap(dC, width, height);

            if (bmp != IntPtr.Zero)
            {
                IntPtr ptr4 = PlatformInvokeGDI32.SelectObject(hdc, bmp);
                PlatformInvokeGDI32.BitBlt(hdc, 0, 0, width, height, dC, x, y, 0xcc0020);
                PlatformInvokeGDI32.SelectObject(hdc, ptr4);
                PlatformInvokeGDI32.DeleteDC(hdc);
                PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), dC);
                Bitmap bitmap = Image.FromHbitmap(bmp);
                PlatformInvokeGDI32.DeleteObject(bmp);
                GC.Collect();
                return(bitmap);
            }
            return(null);
        }
Пример #3
0
        public static Bitmap GetDesktopImage()
        {
            SIZE   size;
            IntPtr dC  = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
            IntPtr hdc = PlatformInvokeGDI32.CreateCompatibleDC(dC);

            size.cx = PlatformInvokeUSER32.GetSystemMetrics(0);
            size.cy = PlatformInvokeUSER32.GetSystemMetrics(1);
            IntPtr bmp = PlatformInvokeGDI32.CreateCompatibleBitmap(dC, size.cx, size.cy);

            if (bmp != IntPtr.Zero)
            {
                IntPtr ptr4 = PlatformInvokeGDI32.SelectObject(hdc, bmp);
                PlatformInvokeGDI32.BitBlt(hdc, 0, 0, size.cx, size.cy, dC, 0, 0, 0xcc0020);
                PlatformInvokeGDI32.SelectObject(hdc, ptr4);
                PlatformInvokeGDI32.DeleteDC(hdc);
                PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), dC);
                Bitmap bitmap = Image.FromHbitmap(bmp);
                PlatformInvokeGDI32.DeleteObject(bmp);
                GC.Collect();
                return(bitmap);
            }
            return(null);
        }
Пример #4
0
        public static Bitmap GetDesktopImage()
        {
            //In size variable we shall keep the size of the screen.
            SIZE size;

            //Variable to keep the handle to bitmap.
            IntPtr hBitmap;

            //Here we get the handle to the desktop device context.
            IntPtr hDC = PlatformInvokeUSER32.GetDC
                             (PlatformInvokeUSER32.GetDesktopWindow());

            //Here we make a compatible device context in memory for screen
            //device context.
            IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

            //We pass SM_CXSCREEN constant to GetSystemMetrics to get the
            //X coordinates of the screen.
            size.cx = PlatformInvokeUSER32.GetSystemMetrics
                          (PlatformInvokeUSER32.SM_CXSCREEN);

            //We pass SM_CYSCREEN constant to GetSystemMetrics to get the
            //Y coordinates of the screen.
            size.cy = PlatformInvokeUSER32.GetSystemMetrics
                          (PlatformInvokeUSER32.SM_CYSCREEN);

            //We create a compatible bitmap of the screen size and using
            //the screen device context.
            hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap
                          (hDC, size.cx, size.cy);

            //As hBitmap is IntPtr, we cannot check it against null.
            //For this purpose, IntPtr.Zero is used.
            if (hBitmap != IntPtr.Zero)
            {
                //Here we select the compatible bitmap in the memeory device
                //context and keep the refrence to the old bitmap.
                IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject
                                  (hMemDC, hBitmap);
                //We copy the Bitmap to the memory device context.
                PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC,
                                           0, 0, PlatformInvokeGDI32.SRCCOPY);
                //We select the old bitmap back to the memory device context.
                PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
                //We delete the memory device context.
                PlatformInvokeGDI32.DeleteDC(hMemDC);
                //We release the screen device context.
                PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.
                                               GetDesktopWindow(), hDC);
                //Image is created by Image bitmap handle and stored in
                //local variable.
                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
                //Release the memory to avoid memory leaks.
                PlatformInvokeGDI32.DeleteObject(hBitmap);
                //This statement runs the garbage collector manually.
                GC.Collect();
                //Return the bitmap
                return(bmp);
            }
            //If hBitmap is null, retun null.
            return(null);
        }