您的位置:首页 > 编程语言 > C#

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!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐