Example #1
0
        public static Bitmap GetDesktopImage()
        {
            IntPtr dC     = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
            IntPtr intPtr = PlatformInvokeGDI32.CreateCompatibleDC(dC);
            SIZE   sIZE;

            sIZE.cx = PlatformInvokeUSER32.GetSystemMetrics(0);
            sIZE.cy = PlatformInvokeUSER32.GetSystemMetrics(1);
            IntPtr intPtr2 = PlatformInvokeGDI32.CreateCompatibleBitmap(dC, sIZE.cx, sIZE.cy);
            Bitmap result;

            if (intPtr2 != IntPtr.Zero)
            {
                IntPtr bmp = PlatformInvokeGDI32.SelectObject(intPtr, intPtr2);
                PlatformInvokeGDI32.BitBlt(intPtr, 0, 0, sIZE.cx, sIZE.cy, dC, 0, 0, 13369376);
                PlatformInvokeGDI32.SelectObject(intPtr, bmp);
                PlatformInvokeGDI32.DeleteDC(intPtr);
                PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), dC);
                Bitmap bitmap = Image.FromHbitmap(intPtr2);
                PlatformInvokeGDI32.DeleteObject(intPtr2);
                GC.Collect();
                result = bitmap;
            }
            else
            {
                result = null;
            }
            return(result);
        }
        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 screen.
            size.cx = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);

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

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

            //As hBitmap is IntPtr we can not check it against null. For this purspose IntPtr.Zero is used.
            if (hBitmap != IntPtr.Zero)
            {
                //Here we select the compatible bitmap in memeory device context and keeps the refrence to 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 retunrn null.
            return(null);
        }