WinCE 6.0截屏 C#源代码

本文参考整理了: http://d.download.csdn.net/down/3135218/chainway765 作者源代码,在此表示感谢
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CC
{
    class CaptureScreen
    {
        private static int picNumbers = 1;

        //截屏保存
        public static void SaveScreenToFile()
        {
            string pathName = @"\Storage Card\Images\Image";
            string picName = DateTime.Now.ToString("yyyyMMdd");
            string fileName = string.Empty;
            string pathMsg = @"\Storage Card\Images"; //数据保存路径
            if (!Directory.Exists(pathMsg))
            {

                Directory.CreateDirectory(pathMsg);
            }
            for (int i = picNumbers; i < 55555; i++)
            {
                fileName = pathName + picName + "_" + i + ".jpg";
                if (!File.Exists(fileName))
                {
                    picNumbers = i;
                    break;
                }
            }
            byte[] bitmapData = GetScreenBitmapArray();
            FileStream fs = new FileStream(fileName, FileMode.Create);
            fs.Write(bitmapData, 0, bitmapData.Length);
            fs.Flush();
            fs.Close();
            MessageBox.Show("截图成功!\r\n保存在存储卡Images目录下", "提示");
        }
       
        #region 私有方法
        private const int PelsPerMeter = 0xb12; // 72 dpi, 96 (0xec4) also possible
        private static byte[] GetControlBitmapArray(Control control)
        {
            control.Capture = true;
            IntPtr hWnd = GetCapture();
            control.Capture = false;
            IntPtr hDC = GetDC(hWnd);
            IntPtr hMemoryDC = CreateCompatibleDC(hDC);

            BITMAPINFOHEADER bih = new BITMAPINFOHEADER();
            bih.biSize = Marshal.SizeOf(bih);
            bih.biBitCount = 24;
            bih.biClrUsed = 0;
            bih.biClrImportant = 0;
            bih.biCompression = 0;
            bih.biHeight = control.Height;
            bih.biWidth = control.Width;
            bih.biPlanes = 1;
            int cb = (int)(bih.biHeight * bih.biWidth * bih.biBitCount / 8);
            bih.biSizeImage = cb;
            bih.biXPelsPerMeter = PelsPerMeter;
            bih.biYPelsPerMeter = PelsPerMeter;

            IntPtr pBits = IntPtr.Zero;
            IntPtr pBIH = LocalAlloc(GPTR, bih.biSize);
            Marshal.StructureToPtr(bih, pBIH, false);
            IntPtr hBitmap = CreateDIBSection(hDC, pBIH, 0, ref pBits, IntPtr.Zero, 0);

            BITMAPINFOHEADER bihMem = (BITMAPINFOHEADER)Marshal.PtrToStructure(pBIH, typeof(BITMAPINFOHEADER));
            IntPtr hPreviousBitmap = SelectObject(hMemoryDC, hBitmap);
            BitBlt(hMemoryDC, 0, 0, bih.biWidth, bih.biHeight, hDC, 0, 0, SRCCOPY);
            byte[] bits = new byte[cb];
            Marshal.Copy(pBits, bits, 0, cb);

            BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
            bfh.bfSize = (uint)cb + 0x36;
            bfh.bfType = 0x4d42;
            bfh.bfOffBits = 0x36;
            int headerSize = 14;
            byte[] header = new byte[headerSize];
            BitConverter.GetBytes(bfh.bfType).CopyTo(header, 0);
            BitConverter.GetBytes(bfh.bfSize).CopyTo(header, 2);
            BitConverter.GetBytes(bfh.bfOffBits).CopyTo(header, 10);
            byte[] data = new byte[cb + bfh.bfOffBits];
            header.CopyTo(data, 0);
            header = new byte[Marshal.SizeOf(bih)];
            IntPtr pHeader = LocalAlloc(GPTR, Marshal.SizeOf(bih));
            Marshal.StructureToPtr(bihMem, pHeader, false);
            Marshal.Copy(pHeader, header, 0, Marshal.SizeOf(bih));
            LocalFree(pHeader);
            header.CopyTo(data, headerSize);
            bits.CopyTo(data, (int)bfh.bfOffBits);

            DeleteObject(SelectObject(hMemoryDC, hPreviousBitmap));
            DeleteDC(hMemoryDC);
            ReleaseDC(hDC);

            return data;
        }

        private static byte[] GetScreenBitmapArray()
        {
            IntPtr hDC = GetDC(IntPtr.Zero);
            IntPtr hMemoryDC = CreateCompatibleDC(hDC);

            BITMAPINFOHEADER bih = new BITMAPINFOHEADER();
            bih.biSize = Marshal.SizeOf(bih);
            bih.biBitCount = 24;
            bih.biClrUsed = 0;
            bih.biClrImportant = 0;
            bih.biCompression = 0;
            bih.biHeight = Screen.PrimaryScreen.Bounds.Height;
            bih.biWidth = Screen.PrimaryScreen.Bounds.Width;
            bih.biPlanes = 1;
            int cb = (int)(bih.biHeight * bih.biWidth * bih.biBitCount / 8);
            bih.biSizeImage = cb;
            bih.biXPelsPerMeter = PelsPerMeter;
            bih.biYPelsPerMeter = PelsPerMeter;

            IntPtr pBits = IntPtr.Zero;
            IntPtr pBIH = LocalAlloc(GPTR, bih.biSize);
            Marshal.StructureToPtr(bih, pBIH, false);
            IntPtr hBitmap = CreateDIBSection(hDC, pBIH, 0, ref pBits, IntPtr.Zero, 0);

            BITMAPINFOHEADER bihMem = (BITMAPINFOHEADER)Marshal.PtrToStructure(pBIH, typeof(BITMAPINFOHEADER));
            IntPtr hPreviousBitmap = SelectObject(hMemoryDC, hBitmap);
            BitBlt(hMemoryDC, 0, 0, bih.biWidth, bih.biHeight, hDC, 0, 0, SRCCOPY);
            byte[] bits = new byte[cb];
            Marshal.Copy(pBits, bits, 0, cb);

            BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
            bfh.bfSize = (uint)cb + 0x36;
            bfh.bfType = 0x4d42;
            bfh.bfOffBits = 0x36;
            int headerSize = 14;
            byte[] header = new byte[headerSize];
            BitConverter.GetBytes(bfh.bfType).CopyTo(header, 0);
            BitConverter.GetBytes(bfh.bfSize).CopyTo(header, 2);
            BitConverter.GetBytes(bfh.bfOffBits).CopyTo(header, 10);
            byte[] data = new byte[cb + bfh.bfOffBits];
            header.CopyTo(data, 0);
            header = new byte[Marshal.SizeOf(bih)];
            IntPtr pHeader = LocalAlloc(GPTR, Marshal.SizeOf(bih));
            Marshal.StructureToPtr(bihMem, pHeader, false);
            Marshal.Copy(pHeader, header, 0, Marshal.SizeOf(bih));
            LocalFree(pHeader);
            header.CopyTo(data, headerSize);
            bits.CopyTo(data, (int)bfh.bfOffBits);

            DeleteObject(SelectObject(hMemoryDC, hPreviousBitmap));
            DeleteDC(hMemoryDC);
            ReleaseDC(hDC);

            return data;
        }
        #endregion
        #region 声明API
        private struct BITMAP
        {
            public int bmType;
            public int bmWidth;
            public int bmHeight;
            public int bmWidthBytes;
            public ushort bmPlanes;
            public ushort bmBitsPixel;
            public byte[] bmBits;
        }

        private struct BITMAPINFO
        {
            public BITMAPINFOHEADER bmiHeader;
            public RGBQUAD[] bmiColors;
        }

        private struct RGBQUAD
        {
            public byte rgbBlue;
            public byte rgbGreen;
            public byte rgbRed;
            public byte rgbReserved;
        }

        private struct BITMAPINFOHEADER
        {
            public int biSize;
            public int biWidth;
            public int biHeight;
            public ushort biPlanes;
            public ushort biBitCount;
            public int biCompression;
            public int biSizeImage;
            public int biXPelsPerMeter;
            public int biYPelsPerMeter;
            public uint biClrUsed;
            public uint biClrImportant;
        }

        private struct BITMAPFILEHEADER
        {
            public ushort bfType;
            public uint bfSize;
            public ushort bfReserved1;
            public ushort bfReserved2;
            public uint bfOffBits;
        }

        private const int GPTR = 0x40;
        private const int SRCCOPY = 0x00CC0020;

        [DllImport("coredll.dll")]
        private static extern IntPtr GetCapture();
        [DllImport("coredll.dll")]
        private static extern IntPtr LocalAlloc(uint flags, int cb);
        [DllImport("coredll.dll")]
        private static extern IntPtr LocalFree(IntPtr hMem);
        [DllImport("coredll.dll")]
        private static extern IntPtr CreateDIBSection(IntPtr hdc, IntPtr hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);
        [DllImport("coredll.dll")]
        private static extern IntPtr GetDC(IntPtr hWnd);
        [DllImport("coredll.dll")]
        private static extern void ReleaseDC(IntPtr hDC);
        [DllImport("coredll.dll")]
        private static extern void DeleteDC(IntPtr hDC);
        [DllImport("coredll.dll")]
        private static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
        [DllImport("coredll.dll")]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("coredll.dll")]
        private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObj);
        [DllImport("coredll.dll")]
        private static extern void DeleteObject(IntPtr hObj);
        #endregion

    }
}
posted on 2011-05-09 12:30 pojowsh 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/hust_wsh/archive/2011/05/09/2041040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值