• 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

        }
    }
  • 相关阅读:
    UVA307Sticksdfs+剪枝
    UVA 10382 Watering Grass 贪心+区间覆盖问题
    H265网页视频播放器播放H265编码录像视频文件如何减缓卡顿情况?
    RTMP/RTSP/GB28181协议视频平台授权页面配置完成后,控制台出现报错如何解决?
    通过私有化部署自建一套视频流媒体服务器平台,如何解决视频播放延时卡顿问题?
    RTSP/RTMP/HTTP/HLS协议视频流媒体播放器EasyPlayerRTSP安卓版本,切换视频流黑屏怎么办?
    【解决方案】设备众多/品牌不一/协议复杂的需求下如何搭建智慧工地视频集中管控平台,实现建筑工程新形态?
    RTMP/RTSP/GB28181协议视频平台授权页面开发中数字框输入字母BUG解决方法
    国标GB28181流媒体协议EasyGBS视频平台集成第三方平台播放器,显示logo水印如何去除?
    通过私有化部署自建视频流媒体服务器,不同视频流延时说明
  • 原文地址:https://www.cnblogs.com/hust_wsh/p/2041040.html
Copyright © 2020-2023  润新知