Пример #1
0
        public static Bitmap GetDesktop()
        {
            IntPtr dC             = WindowsAPI.GetDC(WindowsAPI.GetDesktopWindow());
            IntPtr intPtr         = WindowsAPI.CreateCompatibleDC(dC);
            int    systemMetrics  = WindowsAPI.GetSystemMetrics(0);
            int    systemMetrics2 = WindowsAPI.GetSystemMetrics(1);
            IntPtr intPtr2        = WindowsAPI.CreateCompatibleBitmap(dC, systemMetrics, systemMetrics2);
            Bitmap result;

            if (intPtr2 != IntPtr.Zero)
            {
                IntPtr hgdiobjBm = WindowsAPI.SelectObject(intPtr, intPtr2);
                WindowsAPI.BitBlt(intPtr, 0, 0, systemMetrics, systemMetrics2, dC, 0, 0, 13369376);
                WindowsAPI.SelectObject(intPtr, hgdiobjBm);
                WindowsAPI.DeleteDC(intPtr);
                WindowsAPI.ReleaseDC(WindowsAPI.GetDesktopWindow(), dC);
                Bitmap bitmap = Image.FromHbitmap(intPtr2);
                WindowsAPI.DeleteObject(intPtr2);
                GC.Collect();
                result = bitmap;
            }
            else
            {
                result = null;
            }
            return(result);
        }