.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/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

C/C++和OpenCV实现调用摄像头

《C/C++和OpenCV实现调用摄像头》本文主要介绍了C/C++和OpenCV实现调用摄像头,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录准备工作1. 打开摄像头2. 读取视频帧3. 显示视频帧4. 释放资源5. 获取和设置摄像头属性

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

SpringBoot使用ffmpeg实现视频压缩

《SpringBoot使用ffmpeg实现视频压缩》FFmpeg是一个开源的跨平台多媒体处理工具集,用于录制,转换,编辑和流式传输音频和视频,本文将使用ffmpeg实现视频压缩功能,有需要的可以参考... 目录核心功能1.格式转换2.编解码3.音视频处理4.流媒体支持5.滤镜(Filter)安装配置linu

在Spring Boot中实现HTTPS加密通信及常见问题排查

《在SpringBoot中实现HTTPS加密通信及常见问题排查》HTTPS是HTTP的安全版本,通过SSL/TLS协议为通讯提供加密、身份验证和数据完整性保护,下面通过本文给大家介绍在SpringB... 目录一、HTTPS核心原理1.加密流程概述2.加密技术组合二、证书体系详解1、证书类型对比2. 证书获

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.