C#基于.net CF 2.0实现手机抓屏幕

我博客的第一篇文章就是.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!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值