.NET Compact Framework 1.0 下实现抓屏

.NET Compact Framework 1.x中实现屏幕抓取有些难度,其实还是.net cf 1.x的支持库不够强大,微软在.net cf2.0中已经弥补了此处的不足。但是为什么还非要实现.net cf1.x的屏幕抓取呢?因为目前使用windows mobile 2003的用户还是大多数,即使目前刷了windows moblie 5的玩家,其系统也并未集成.net cf2.0。这就需要自己安装 .net cf2.0,这对一般用户有一定的门槛(我会在以后的文章中介绍如何安装.net cf2.0),高级用户即使安装也是有顾虑的,.net cf2.0会占用大约10M的手机空间。所以,在这里我给出一个.net cf1.x下抓屏的实例。

#region Using directives

using System;
using System.Runtime.InteropServices;
using System.IO;
#endregion

namespace CatchScreen
{
    ///


    /// Summary description for CaptureScreen.
    ///

    public class CaptureScreen
    {
        public CaptureScreen()
        {

        }

        public class CaptureScreenApplication
        {
            private const int sizeBFH = 14;
            private const int imgHeight = 220;
            private const int imgWidth = 176;
            private const int SRCCOPY = 0xCC0020;

            public static void CaptureScreen()
            {
                //IntPtr h1 = GDIApi.FindWindow("", "Form1");
                IntPtr h1 = GDIApi.GetDesktopWindow();

                IntPtr dc1 = GDIApi.GetDC(IntPtr.Zero);
                IntPtr InMemoryDC = GDIApi.CreateCompatibleDC(dc1);

                IntPtr hBitmap;
                IntPtr ppvBits;

                GDIApi.BITMAPINFO bmi = new GDIApi.BITMAPINFO();
                bmi.biSize = Marshal.SizeOf(bmi);
                bmi.biBitCount = 24;
                bmi.biPlanes = 1;
                bmi.biWidth = imgWidth;
                bmi.biHeight = imgHeight;
                bmi.biXPelsPerMeter = 0xb12;
                bmi.biYPelsPerMeter = 0xb12;
                bmi.biSizeImage = bmi.biWidth * bmi.biHeight * bmi.biBitCount / 8;
                bmi.biClrUsed = 0;
                bmi.biClrImportant = 0;
                bmi.biCompression = 0;

                hBitmap = GDIApi.CreateDIBSection(new IntPtr(0), bmi, 0, out ppvBits, new IntPtr(0), 0);
                IntPtr OldBitmap = GDIApi.SelectObject(InMemoryDC, hBitmap);

                IntPtr dc2 = GDIApi.GetWindowDC(h1);
                GDIApi.StretchBlt(InMemoryDC, 0, 0, imgWidth, imgHeight, dc2, 0, 0, imgWidth, imgHeight, SRCCOPY);
                byte[] rawData = new byte[bmi.biSizeImage];
                Marshal.Copy(ppvBits, rawData, 0, bmi.biSizeImage);
                GDIApi.DeleteDC(InMemoryDC);

                GDIApi.ReleaseDC(IntPtr.Zero, dc1);
                GDIApi.ReleaseDC(h1, dc2);

                byte[] bitmap = CreateBitmap(imgWidth, imgHeight, rawData, bmi);

                FileStream fs = new FileStream(@"/out1.bmp", FileMode.Create, FileAccess.Write);
                fs.Write(bitmap, 0, bitmap.Length);
                fs.Close();
            }

            public static byte[] CreateBitmap(int width, int height, byte[] bitmapData, GDIApi.BITMAPINFO bi)
            {
                if ((width & 1) == 1)
                    throw new ArgumentException("Width must be an even number");

                int nSize = sizeBFH + Marshal.SizeOf(typeof(GDIApi.BITMAPINFO)) + (width << 3) * height;
                byte[] data = new byte[nSize];
                byte[] bfh = new byte[sizeBFH];
                BitConverter.GetBytes((short)0x4d42).CopyTo(data, 0);
                BitConverter.GetBytes(nSize).CopyTo(data, 2);
                int bfhOffBits = (int)(sizeBFH + Marshal.SizeOf(typeof(GDIApi.BITMAPINFO)));
                BitConverter.GetBytes(bfhOffBits).CopyTo(data, 10);

                byte[] hdr = GetBytes(bi);

                Buffer.BlockCopy(hdr, 0, data, sizeBFH, hdr.Length);
                Buffer.BlockCopy(bitmapData, 0, data, (int)bfhOffBits, Math.Min(bitmapData.Length, bi.biSizeImage));

                return data;
            }

            private static byte[] GetBytes(object o)
            {
                int size = Marshal.SizeOf(o.GetType());
                IntPtr p = GDIApi.LocalAlloc(GDIApi.GPTR, size);
                Marshal.StructureToPtr(o, p, false);
                byte[] ret = new byte[size];
                Marshal.Copy(p, ret, 0, size);
                GDIApi.LocalFree(p);
                return ret;
            }
        }


        public class GDIApi
        {
            public const int GPTR = 0x40;

            public struct BITMAPFILEHEADER
            {
                public Int16 bfType;
                public Int32 bfSize;
                public Int16 bfReserved1;
                public Int16 bfReserved2;
                public Int32 bfOffBits;
            }

            public class BITMAPINFO
            {
                public Int32 biSize;
                public Int32 biWidth;
                public Int32 biHeight;
                public Int16 biPlanes;
                public Int16 biBitCount;
                public Int32 biCompression;
                public Int32 biSizeImage;
                public Int32 biXPelsPerMeter;
                public Int32 biYPelsPerMeter;
                public Int32 biClrUsed;
                public Int32 biClrImportant;
            };

            [DllImport("coredll.dll")]
            public static extern uint GetLastError();
            [DllImport("coredll.dll")]
            public static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFO pbmi, uint iUsage, out IntPtr
                                                         ppvBits, IntPtr hSection, uint dwOffset);
            [DllImport("coredll.dll")]
            public static extern uint GetSystemPaletteEntries(IntPtr hdc, uint iStartIndexx,
            uint nEntries, IntPtr lppe);

            [DllImport("coredll.dll")]
            public static extern int GetObject(IntPtr hgdiobj, int cBuffer, IntPtr lp);

            [DllImport("coredll.dll")]
            public static extern IntPtr DeleteDC(IntPtr hdc);

            [DllImport("coredll.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

            [DllImport("coredll.dll")]
            public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

            [DllImport("coredll.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

            [DllImport("coredll.dll")]
            public static extern IntPtr FindWindow(string s1, string s2);

            [DllImport("coredll.dll")]
            public static extern int GetWindowText(IntPtr handle, char[] c, int len);

            [DllImport("coredll.dll")]
            public static extern IntPtr GetWindow(IntPtr handle, int cmd);

            [DllImportAttribute("coredll.dll")]
            public static extern IntPtr GetDC(IntPtr hWnd);

 

            [DllImportAttribute("coredll.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hWnd);

            [DllImportAttribute("coredll.dll")]
            public static extern int GetDeviceCaps(IntPtr hWnd, int nIndex);

            [DllImportAttribute("coredll.dll")]
            public static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref int[] lpRect, uint uFormat);

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

            [DllImportAttribute("coredll.dll")]
            public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, uint uFlags);

            [DllImportAttribute("coredll.dll")]
            public static extern bool StretchBlt(
                IntPtr hdcDest, // handle to destination DC
                int nXDest, // x-coord of destination upper-left corner
                int nYDest, // y-coord of destination upper-left corner
                int nWidth, // width of destination rectangle
                int nHeight, // height of destination rectangle, negative to switch upside down
                IntPtr hdcSrc, // handle to source DC
                int nXSrc, // x-coordinate of source upper-left corner
                int nYSrc, // y-coordinate of source upper-left corner
                int nWidthSrc,
                int nHeightSrc,
                uint dwRop // raster operation code
            );

            [DllImport("coredll.dll")]
            public static extern IntPtr GetDesktopWindow();

            [DllImport("coredll.dll")]
            public static extern IntPtr LocalAlloc(uint flags, int cb);
            [DllImport("coredll.dll")]
            public static extern IntPtr LocalFree(IntPtr hMem);
        }

    }
}
 有过c#开发经验的朋友会很容看懂以上的代码,在win32下实现抓屏是很简单的,因为我们可以用ImageFormat类来保存抓取的文件。其实,.net cf 1.x下实现抓屏并不在于抓屏本身难以实现,无非都是调用windows 的api函数,其难点在于如何保存抓取的文件。我是在opennetcf的论坛中找到了一段代码,然后经过自己的修改调试才完成的抓屏功能。这个类并不完善,一些参数我都写死了,有兴趣的朋友可以自己完善一下。Good luck!God bless you!

你可能感兴趣的:(.NET,Compact,Framework,1.0,.net,byte,windows,class,string,struct)