.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

相关文章

Python如何实现高效的文件/目录比较

《Python如何实现高效的文件/目录比较》在系统维护、数据同步或版本控制场景中,我们经常需要比较两个目录的差异,本文将分享一下如何用Python实现高效的文件/目录比较,并灵活处理排除规则,希望对大... 目录案例一:基础目录比较与排除实现案例二:高性能大文件比较案例三:跨平台路径处理案例四:可视化差异报

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

Python脚本轻松实现检测麦克风功能

《Python脚本轻松实现检测麦克风功能》在进行音频处理或开发需要使用麦克风的应用程序时,确保麦克风功能正常是非常重要的,本文将介绍一个简单的Python脚本,能够帮助我们检测本地麦克风的功能,需要的... 目录轻松检测麦克风功能脚本介绍一、python环境准备二、代码解析三、使用方法四、知识扩展轻松检测麦

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Java高效实现Word转PDF的完整指南

《Java高效实现Word转PDF的完整指南》这篇文章主要为大家详细介绍了如何用Spire.DocforJava库实现Word到PDF文档的快速转换,并解析其转换选项的灵活配置技巧,希望对大家有所帮助... 目录方法一:三步实现核心功能方法二:高级选项配置性能优化建议方法补充ASPose 实现方案Libre

Go中select多路复用的实现示例

《Go中select多路复用的实现示例》Go的select用于多通道通信,实现多路复用,支持随机选择、超时控制及非阻塞操作,建议合理使用以避免协程泄漏和死循环,感兴趣的可以了解一下... 目录一、什么是select基本语法:二、select 使用示例示例1:监听多个通道输入三、select的特性四、使用se

Java 中编码与解码的具体实现方法

《Java中编码与解码的具体实现方法》在Java中,字符编码与解码是处理数据的重要组成部分,正确的编码和解码可以确保字符数据在存储、传输、读取时不会出现乱码,本文将详细介绍Java中字符编码与解码的... 目录Java 中编码与解码的实现详解1. 什么是字符编码与解码?1.1 字符编码(Encoding)1

Python Flask实现定时任务的不同方法详解

《PythonFlask实现定时任务的不同方法详解》在Flask中实现定时任务,最常用的方法是使用APScheduler库,本文将提供一个完整的解决方案,有需要的小伙伴可以跟随小编一起学习一下... 目录完js整实现方案代码解释1. 依赖安装2. 核心组件3. 任务类型4. 任务管理5. 持久化存储生产环境

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点