首页 > 开发 > 综合 > 正文

C#截屏

2024-07-21 02:25:53
字体:
来源:转载
供稿:网友

using system;
using system.runtime.interopservices;
using system.drawing;
using system.drawing.imaging;

namespace csharpchat
{
 /// <summary>
 /// screencameraclass 的摘要说明。
 /// </summary>
 public class screencameraclass
 {
  public screencameraclass()
  {
   //
   // todo: 在此处添加构造函数逻辑
   //
  }
  /**//// <summary>
  /// creates an image object containing a screen shot of the entire desktop?
  /// </summary>
  /// <returns></returns>
  public image capturescreen()
  {
   return capturewindow( user32.getdesktopwindow() );
  }
 
  /**//// <summary>
  /// creates an image object containing a screen shot of a specific window?
  /// </summary>
  /// <param name="handle">the handle to the window. (in windows forms, this is obtained by the handle property)</param>
  /// <returns></returns>
  public image capturewindow(intptr handle)
  {
   // get te hdc of the target window
   intptr hdcsrc = user32.getwindowdc(handle);
   // get the size
   user32.rect windowrect = new user32.rect();
   user32.getwindowrect(handle,ref windowrect);
   int width = windowrect.right - windowrect.left;
   int height = windowrect.bottom - windowrect.top;
   // create a device context we can copy to
   intptr hdcdest = gdi32.createcompatibledc(hdcsrc);
   // create a bitmap we can copy it to,
   // using getdevicecaps to get the width/height
   intptr hbitmap = gdi32.createcompatiblebitmap(hdcsrc,width,height);
   // select the bitmap object
   intptr hold = gdi32.selectobject(hdcdest,hbitmap);
   // bitblt over
   gdi32.bitblt(hdcdest,0,0,width,height,hdcsrc,0,0,gdi32.srccopy);
   // restore selection
   gdi32.selectobject(hdcdest,hold);
   // clean up
   gdi32.deletedc(hdcdest);
   user32.releasedc(handle,hdcsrc);

   // get a .net image object for it
   image img = image.fromhbitmap(hbitmap);
   // free up the bitmap object
   gdi32.deleteobject(hbitmap);

   return img;
  }

  /**//// <summary>
  /// captures a screen shot of a specific window, and saves it to a file?
  /// </summary>
  /// <param name="handle"></param>
  /// <param name="filename"></param>
  /// <param name="format"></param>
  public void capturewindowtofile(intptr handle, string filename, imageformat format)
  {
   image img = capturewindow(handle);
   img.save(filename,format);
  }

  /**//// <summary>
  /// captures a screen shot of the entire desktop, and saves it to a file?
  /// </summary>
  /// <param name="filename"></param>
  /// <param name="format"></param>
  public void capturescreentofile(string filename, imageformat format)
  {
   image img = capturescreen();
   img.save(filename,format);
  }
     
  /**//// <summary>
  /// helper class containing gdi32 api functions
  /// </summary>
  private class gdi32
  {
           
   public const int srccopy = 0x00cc0020; // bitblt dwrop parameter

   [dllimport("gdi32.dll")]
   public static extern bool bitblt(intptr hobject,int nxdest,int nydest,
    int nwidth,int nheight,intptr hobjectsource,
    int nxsrc,int nysrc,int dwrop);
   [dllimport("gdi32.dll")]
   public static extern intptr createcompatiblebitmap(intptr hdc,int nwidth,
    int nheight);
   [dllimport("gdi32.dll")]
   public static extern intptr createcompatibledc(intptr hdc);
   [dllimport("gdi32.dll")]
   public static extern bool deletedc(intptr hdc);
   [dllimport("gdi32.dll")]
   public static extern bool deleteobject(intptr hobject);
   [dllimport("gdi32.dll")]
   public static extern intptr selectobject(intptr hdc,intptr hobject);
  }
 
  /**//// <summary>
  /// helper class containing user32 api functions
  /// </summary>
  private class user32
  {
   [structlayout(layoutkind.sequential)]
    public struct rect
   {
    public int left;
    public int top;
    public int right;
    public int bottom;
   }

   [dllimport("user32.dll")]
   public static extern intptr getdesktopwindow();
   [dllimport("user32.dll")]
   public static extern intptr getwindowdc(intptr hwnd);
   [dllimport("user32.dll")]
   public static extern intptr releasedc(intptr hwnd,intptr hdc);
   [dllimport("user32.dll")]
   public static extern intptr getwindowrect(intptr hwnd,ref rect rect);

  }
 }
}
 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表