⌘BABAKOV.NET Let's code!
100% [light] [ru]

Making Screenshots in C# under .NET CF (from archives)

Publication from archives, might be useful...
Once I encountered the need to take screenshots from a PDA, and I couldn't find any suitable program. Specifically, a simple, small one that doesn't install anything, but works. I decided to write it, spent two hours digging the internet about how to do it - it turned out not to be so simple, only through API on mobile devices.

So, without further ado, here is the code:

//Подключаемые библиотеки...
        [DllImport("coredll.dll", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr GetDesctopWindow();

        [DllImport("coredll.dll", EntryPoint = "GetDC")]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("coredll.dll", EntryPoint = "ReleaseDC")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("coredll.dll")]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
        const int SRCCOPY = 0x00CC0020;



//Сам код...

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\\\myPic.bmp";
            //Сохраняем изображение.
            Rectangle rect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            Graphics g = Graphics.FromImage(bmp);
            IntPtr hwnd = GetDesctopWindow();
            IntPtr hdc = GetDC(hwnd);
            BitBlt(g.GetHdc(), 0, 0, rect.Width, rect.Height, hdc, rect.Left, rect.Top, SRCCOPY);
            bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
            ReleaseDC(hwnd, hdc);
            bmp.Dispose();
            g.Dispose();

P.S. Requires .NET CF 3.5

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.