今天无聊翻代码,翻出来一个以前写的C#截屏的函数...拿出来和大家共享一下.
这段代码是参照网上一段截屏的C++代码改写的.只不过把API都声明了一下而已.
声明的各API也附后.以供参照.如有问题欢迎指出.(Appledotnet@hotmail.com)

Code
1
/**////
2
/// 截取部分屏幕
3
///
4
/// 左上角
5
/// 右下角
6
/// 是否全屏幕
7
/// 返回值Bitmap
8
public static Bitmap GetPartScreen(Point P1,Point P2,bool Full)
9
{
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", null, null, 0);//创建DC句柄
17
hmemdc = CreateCompatibleDC(hscrdc);//创建一个内存DC
18
xscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.HORZRES);//获取屏幕宽度
19
yscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.VERTRES);//获取屏幕高度
20
if(Full)//如果是截取整个屏幕
21
{
22
nx = 0;
23
ny = 0;
24
nx2 = xscrn;
25
ny2 = yscrn;
26
}
27
else
28
{
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, 0, 0, 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
);
这段代码是参照网上一段截屏的C++代码改写的.只不过把API都声明了一下而已.
声明的各API也附后.以供参照.如有问题欢迎指出.(Appledotnet@hotmail.com)
1

/**//// 2
/// 截取部分屏幕3
/// 4
/// 左上角5
/// 右下角6
/// 是否全屏幕7
/// 返回值Bitmap8
public static Bitmap GetPartScreen(Point P1,Point P2,bool Full)9

{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", null, null, 0);//创建DC句柄17
hmemdc = CreateCompatibleDC(hscrdc);//创建一个内存DC18
xscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.HORZRES);//获取屏幕宽度19
yscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.VERTRES);//获取屏幕高度20
if(Full)//如果是截取整个屏幕21

{22
nx = 0;23
ny = 0;24
nx2 = xscrn;25
ny2 = yscrn;26
}27
else28

{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, 0, 0, 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返回Bitmap48
}49
50
51
52
所用到的API声明:53
[DllImport("gdi32.dll")]54
public static extern IntPtr CreateDC(55
string lpszDriver, // driver name56
string lpszDevice, // device name57
string lpszOutput, // not used; should be NULL58
Int64 lpInitData // optional printer data59
);60
[DllImport("gdi32.dll")]61
public static extern IntPtr CreateCompatibleDC(62
IntPtr hdc // handle to DC63
);64
[DllImport("gdi32.dll")]65
public static extern int GetDeviceCaps(66
IntPtr hdc, // handle to DC67
GetDeviceCapsIndex nIndex // index of capability68
);69
[DllImport("gdi32.dll")]70
public static extern IntPtr CreateCompatibleBitmap(71
IntPtr hdc, // handle to DC72
int nWidth, // width of bitmap, in pixels73
int nHeight // height of bitmap, in pixels74
);75
[DllImport("gdi32.dll")]76
public static extern IntPtr SelectObject(77
IntPtr hdc, // handle to DC78
IntPtr hgdiobj // handle to object79
);80
[DllImport("gdi32.dll")]81
public static extern int BitBlt(82
IntPtr hdcDest, // handle to destination DC83
int nXDest, // x-coord of destination upper-left corner84
int nYDest, // y-coord of destination upper-left corner85
int nWidth, // width of destination rectangle86
int nHeight, // height of destination rectangle87
IntPtr hdcSrc, // handle to source DC88
int nXSrc, // x-coordinate of source upper-left corner89
int nYSrc, // y-coordinate of source upper-left corner90
UInt32 dwRop // raster operation code91
);92
[DllImport("gdi32.dll")]93
public static extern int DeleteDC(94
IntPtr hdc // handle to DC95
);
浙公网安备 33010602011771号