.NET Compact Framework 1.0 下实现hbitmap,以及用hbitmap创建hdc(c#)

2023-10-28 07:48

本文主要是介绍.NET Compact Framework 1.0 下实现hbitmap,以及用hbitmap创建hdc(c#),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

.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>
    /// Summary description for CaptureScreen.
    /// </summary>
    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!

 

 

http://blog.csdn.net/wellwelcome/archive/2006/11/01/1360128.aspx

这篇关于.NET Compact Framework 1.0 下实现hbitmap,以及用hbitmap创建hdc(c#)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/291753

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

pytorch自动求梯度autograd的实现

《pytorch自动求梯度autograd的实现》autograd是一个自动微分引擎,它可以自动计算张量的梯度,本文主要介绍了pytorch自动求梯度autograd的实现,具有一定的参考价值,感兴趣... autograd是pytorch构建神经网络的核心。在 PyTorch 中,结合以下代码例子,当你

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的