C#基于.net CF 2.0实现手机抓屏幕
2008-04-29 09:42
239 查看
我博客的第一篇文章就是.net cf 1.x实现手机屏幕的抓取,大概有190左右的访问量吧。现在是时候介绍一下.net cf 2.0下如何进行手机屏幕抓取了,.net cf 2.0中microsoft封装了ImageFormat 类,这就解决了保存图片的问题,其实在.net cf 1.x实现抓屏也就是解决一个图像保存问题!抓屏的核心技术并没有多大区别,都是Invoke windows 的GDI API函数,也都是调用这些函数而已!下面是完整的抓屏代码:
//CaptureScreen.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace DeviceAnywherePhone
{
/// <summary>
/// This class shall keep all the functionality for capturing
/// the desktop.
/// </summary>
public class CaptureScreen
{
#region Public Class Functions
public static Bitmap GetDesktopImage()
{
SIZE size;
IntPtr hBitmap;
IntPtr hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);
size.cx = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);
size.cy = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);
hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (hBitmap!=IntPtr.Zero)
{
IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject(hMemDC, hBitmap);
PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY);
PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
PlatformInvokeGDI32.DeleteDC(hMemDC);
PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
PlatformInvokeGDI32.DeleteObject(hBitmap);
GC.Collect();
return bmp;
}
return null;
}
public static MemoryStream GetDesktopImageToStream(System.Drawing.Imaging.ImageFormat imageFormat)
{
MemoryStream ms = new MemoryStream();
Bitmap bmp = GetDesktopImage();
if (bmp != null)
{
bmp.Save(ms, imageFormat);
}
else
{
ms = null;
}
return ms;
}
public static byte[] GetDesktopImageToBuffer(System.Drawing.Imaging.ImageFormat imageFormat)
{
byte[] buffer = null;
MemoryStream ms = GetDesktopImageToStream(imageFormat);
if (ms != null)
{
//buffer = new byte[ms.Length];
buffer = ms.GetBuffer();
}
return buffer;
}
public static bool GetDesktopImageToFile(string fileName, ImageFormat imageFormat)
{
Bitmap bmp = GetDesktopImage();
if (bmp != null)
{
bmp.Save(fileName, imageFormat);
return true;
}
else
{
return false;
}
}
#endregion
}
}
PlatFormInvokeGDI32.cs
using System;
using System.Runtime.InteropServices;
namespace DeviceAnywherePhone
{
/// <summary>
/// This class shall keep the GDI32 APIs being used in our program.
/// </summary>
public class PlatformInvokeGDI32
{
#region Class Variables
public const int SRCCOPY = 13369376;
#endregion
#region Class Functions
[DllImport("coredll.dll", EntryPoint = "DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("coredll.dll", EntryPoint = "DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("coredll.dll", EntryPoint = "BitBlt")]
public static extern bool BitBlt(IntPtr hdcDest,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);
[DllImport("coredll.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("coredll.dll", EntryPoint = "CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("coredll.dll", EntryPoint = "SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);
#endregion
#region Public Constructor
public PlatformInvokeGDI32()
{
//
// TODO: Add constructor logic here
//
}
#endregion
}
}
PlatformInvokeUSER32.cs
using System;
using System.Runtime.InteropServices;
namespace DeviceAnywherePhone
{
/// <summary>
/// This class shall keep the User32 APIs being used in
/// our program.
/// </summary>
public class PlatformInvokeUSER32
{
#region Class Variables
public const int SM_CXSCREEN=0;
public const int SM_CYSCREEN=1;
#endregion
#region Class Functions
[DllImport("coredll.dll", EntryPoint="GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("coredll.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);
[DllImport("coredll.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("coredll.dll", EntryPoint = "GetWindowDC")]
public static extern IntPtr GetWindowDC(Int32 ptr);
[DllImport("coredll.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);
#endregion
#region Public Constructor
public PlatformInvokeUSER32()
{
//
// TODO: Add constructor logic here
//
}
#endregion
}
//This structure shall be used to keep the size of the screen.
public struct SIZE
{
public int cx;
public int cy;
}
}
三个文件,至于如何调用就不用我给出例子了吧,Good Luck!!
相关文章推荐
- windows mobile 5.0 下截获短信(C#基于.net CF 2.0)
- 基于.NET 2.0的GIS开源项目SharpMap分析手记(七):GDAL C#接口库的编译与SharpMap(Change Set 21021)对影像的支持研究
- 基于.Net(C#开发)平台的三层框架架构软件的设计与实现
- 基于.Net平台应用程序唯一运行实例C#代码实现
- 基于.NET 2.0的GIS开源项目SharpMap分析手记(十四):ASP.NET2.0实现无刷新客户端回调的Callback机制及例子代码下载
- 基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速开发框架设计NBear V1.8.1 [开源]
- ZedGraph:一个同时支持WinForm和WebForm的开源图表控件(基于LGPL协议,.NET 2.0 C#源代码)
- 基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速开发框架设计NBear V3.3.9 Stable [开源]
- 基于PLC-C#串口通讯,温度检测和转速监控的c#/.Net实现。
- C#基于.net CF 2.0实现手机抓屏幕
- C# 继承.Net中提供的WebBrowser 2.0控件以实现IE的超级拖放
- .net 2.0 C#实现压缩/解压功能实现
- 基于.Net(C#开发)平台的三层框架架构软件的设计与实现
- 使用.net 2.0(c#)来实现一般的FTP功能
- (C#)通过多线程为基于 .NET 的应用程序实现响应迅速的用户
- 基于C#实现的屏幕指定区域截屏代码
- 基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速开发框架设计NBear V3.3.9 Stable [开源]
- (C#)通过多线程为基于 .NET 的应用程序实现响应迅速的用户
- Teddy's Knowledge Base--基于.Net 2.0 (C# 2.0, ASP.NET 2.0)的快速开发框架设计NBear V2.0.0 [开源]
- 基于.Net平台应用程序唯一运行实例C#代码实现