C#截取屏幕【转】

  今天无聊翻代码,翻出来一个以前写的C#截屏的函数...拿出来和大家共享一下.
  这段代码是参照网上一段截屏的C++代码改写的.只不过把API都声明了一下而已.
  声明的各API也附后.以供参照.如有问题欢迎指出.(Appledotnet@hotmail.com)
  
  
ContractedBlock.gif ExpandedBlockStart.gif Code
 1ExpandedBlockStart.gifContractedBlock.gif  /**//// 
 2  /// 截取部分屏幕
 3  /// 
 4  /// 左上角
 5  /// 右下角
 6  /// 是否全屏幕
 7  /// 返回值Bitmap

 8  public static Bitmap GetPartScreen(Point P1,Point P2,bool Full)
 9ExpandedBlockStart.gifContractedBlock.gif  {
10   IntPtr hscrdc,hmemdc;
11   IntPtr hbitmap,holdbitmap;
12   int nx,ny,nx2,ny2;
13   nx=ny=nx2=ny2=0;
14   int nwidth, nheight;
15   int xscrn, yscrn;
16   hscrdc = CreateDC("DISPLAY"nullnull0);//创建DC句柄
17   hmemdc = CreateCompatibleDC(hscrdc);//创建一个内存DC
18   xscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.HORZRES);//获取屏幕宽度
19   yscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.VERTRES);//获取屏幕高度
20   if(Full)//如果是截取整个屏幕
21ExpandedSubBlockStart.gifContractedSubBlock.gif   {
22   nx = 0;
23   ny = 0;
24   nx2 = xscrn;
25   ny2 = yscrn;
26   }

27   else
28ExpandedSubBlockStart.gifContractedSubBlock.gif   {
29   nx = P1.X;
30   ny = P1.Y;
31   nx2 =P2.X;
32   ny2 =P2.Y;
33   //检查数值合法性
34   if(nx<0)nx = 0;
35   if(ny<0)ny = 0;
36   if(nx2>xscrn)nx2 = xscrn;
37   if(ny2>yscrn)ny2 = yscrn;
38   }

39   nwidth = nx2 - nx;//截取范围的宽度
40   nheight = ny2 - ny;//截取范围的高度
41   hbitmap = CreateCompatibleBitmap(hscrdc, nwidth, nheight);//从内存DC复制到hbitmap句柄
42   holdbitmap = SelectObject(hmemdc, hbitmap);
43   BitBlt(hmemdc, 00, nwidth, nheight,hscrdc, nx, ny,(UInt32)0xcc0020);
44   hbitmap = SelectObject(hmemdc, holdbitmap);
45   DeleteDC(hscrdc);//删除用过的对象
46   DeleteDC(hmemdc);//删除用过的对象
47   return Bitmap.FromHbitmap(hbitmap);//用Bitmap.FromHbitmap从hbitmap返回Bitmap
48  }

49   
50   
51   
52  所用到的API声明:
53  [DllImport("gdi32.dll")]
54  public static extern IntPtr CreateDC(
55  string lpszDriver, // driver name
56  string lpszDevice, // device name
57  string lpszOutput, // not used; should be NULL
58  Int64 lpInitData // optional printer data
59  );
60  [DllImport("gdi32.dll")]
61  public static extern IntPtr CreateCompatibleDC(
62  IntPtr hdc // handle to DC
63  );
64  [DllImport("gdi32.dll")]
65  public static extern int GetDeviceCaps(
66  IntPtr hdc, // handle to DC
67  GetDeviceCapsIndex nIndex // index of capability
68  );
69  [DllImport("gdi32.dll")]
70  public static extern IntPtr CreateCompatibleBitmap(
71  IntPtr hdc, // handle to DC
72  int nWidth, // width of bitmap, in pixels
73  int nHeight // height of bitmap, in pixels
74  );
75  [DllImport("gdi32.dll")]
76  public static extern IntPtr SelectObject(
77  IntPtr hdc, // handle to DC
78  IntPtr hgdiobj // handle to object
79  );
80  [DllImport("gdi32.dll")]
81  public static extern int BitBlt(
82  IntPtr hdcDest, // handle to destination DC
83  int nXDest, // x-coord of destination upper-left corner
84  int nYDest, // y-coord of destination upper-left corner
85  int nWidth, // width of destination rectangle
86  int nHeight, // height of destination rectangle
87  IntPtr hdcSrc, // handle to source DC
88  int nXSrc, // x-coordinate of source upper-left corner
89  int nYSrc, // y-coordinate of source upper-left corner
90  UInt32 dwRop // raster operation code
91  );
92  [DllImport("gdi32.dll")]
93  public static extern int DeleteDC(
94  IntPtr hdc // handle to DC
95  );
posted on 2008-03-21 17:09  Jrong 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jrong/archive/2008/03/21/1116762.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值