Example #1
0
        private static void getDisplayStatus()
        {
            bool   fOn;
            IntPtr hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());

            if (GetDevicePowerState(hDC, out fOn))
            {
                Console.WriteLine("Monitor is Off.");
            }
            else
            {
                Console.WriteLine("Monitor is on.");
            }
        }
            public static Bitmap GetDesktopImage()
            {
                SIZE   size;
                IntPtr hBitmap;
                IntPtr hDC    = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
                IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

                size.cx = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);
                size.cy = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);
                hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);

                if (hBitmap != IntPtr.Zero)
                {
                    IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, hBitmap);
                    PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY);
                    PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
                    PlatformInvokeGDI32.DeleteDC(hMemDC);
                    PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC);
                    Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
                    PlatformInvokeGDI32.DeleteObject(hBitmap);
                    return(bmp);
                }
                return(null);
            }