.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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定