C#截取屏幕↑ 选择自 AppleDotnet 的 Blog
2008-06-12 10:53
246 查看
今天无聊翻代码,翻出来一个以前写的C#截屏的函数...拿出来和大家共享一下.
这段代码是参照网上一段截屏的C++代码改写的.只不过把API都声明了一下而已.
声明的各API也附后.以供参照.如有问题欢迎指出.(Appledotnet@hotmail.com)
///
/// 截取部分屏幕
///
///
左上角
///
右下角
///
是否全屏幕
/// 返回值Bitmap
public static Bitmap GetPartScreen(Point P1,Point P2,bool Full)
{
IntPtr hscrdc,hmemdc;
IntPtr hbitmap,holdbitmap;
int nx,ny,nx2,ny2;
nx=ny=nx2=ny2=0;
int nwidth, nheight;
int xscrn, yscrn;
hscrdc = CreateDC("DISPLAY", null, null, 0);//创建DC句柄
hmemdc = CreateCompatibleDC(hscrdc);//创建一个内存DC
xscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.HORZRES);//获取屏幕宽度
yscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.VERTRES);//获取屏幕高度
if(Full)//如果是截取整个屏幕
{
nx = 0;
ny = 0;
nx2 = xscrn;
ny2 = yscrn;
}
else
{
nx = P1.X;
ny = P1.Y;
nx2 =P2.X;
ny2 =P2.Y;
//检查数值合法性
if(nx<0)nx = 0;
if(ny<0)ny = 0;
if(nx2>xscrn)nx2 = xscrn;
if(ny2>yscrn)ny2 = yscrn;
}
nwidth = nx2 - nx;//截取范围的宽度
nheight = ny2 - ny;//截取范围的高度
hbitmap = CreateCompatibleBitmap(hscrdc, nwidth, nheight);//从内存DC复制到hbitmap句柄
holdbitmap = SelectObject(hmemdc, hbitmap);
BitBlt(hmemdc, 0, 0, nwidth, nheight,hscrdc, nx, ny,(UInt32)0xcc0020);
hbitmap = SelectObject(hmemdc, holdbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return Bitmap.FromHbitmap(hbitmap);//用Bitmap.FromHbitmap从hbitmap返回Bitmap
}
所用到的API声明:
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // driver name
string lpszDevice, // device name
string lpszOutput, // not used; should be NULL
Int64 lpInitData // optional printer data
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
);
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
GetDeviceCapsIndex nIndex // index of capability
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
UInt32 dwRop // raster operation code
);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(
IntPtr hdc // handle to DC
);
这段代码是参照网上一段截屏的C++代码改写的.只不过把API都声明了一下而已.
声明的各API也附后.以供参照.如有问题欢迎指出.(Appledotnet@hotmail.com)
///
/// 截取部分屏幕
///
///
左上角
///
右下角
///
是否全屏幕
/// 返回值Bitmap
public static Bitmap GetPartScreen(Point P1,Point P2,bool Full)
{
IntPtr hscrdc,hmemdc;
IntPtr hbitmap,holdbitmap;
int nx,ny,nx2,ny2;
nx=ny=nx2=ny2=0;
int nwidth, nheight;
int xscrn, yscrn;
hscrdc = CreateDC("DISPLAY", null, null, 0);//创建DC句柄
hmemdc = CreateCompatibleDC(hscrdc);//创建一个内存DC
xscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.HORZRES);//获取屏幕宽度
yscrn = GetDeviceCaps(hscrdc, GetDeviceCapsIndex.VERTRES);//获取屏幕高度
if(Full)//如果是截取整个屏幕
{
nx = 0;
ny = 0;
nx2 = xscrn;
ny2 = yscrn;
}
else
{
nx = P1.X;
ny = P1.Y;
nx2 =P2.X;
ny2 =P2.Y;
//检查数值合法性
if(nx<0)nx = 0;
if(ny<0)ny = 0;
if(nx2>xscrn)nx2 = xscrn;
if(ny2>yscrn)ny2 = yscrn;
}
nwidth = nx2 - nx;//截取范围的宽度
nheight = ny2 - ny;//截取范围的高度
hbitmap = CreateCompatibleBitmap(hscrdc, nwidth, nheight);//从内存DC复制到hbitmap句柄
holdbitmap = SelectObject(hmemdc, hbitmap);
BitBlt(hmemdc, 0, 0, nwidth, nheight,hscrdc, nx, ny,(UInt32)0xcc0020);
hbitmap = SelectObject(hmemdc, holdbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return Bitmap.FromHbitmap(hbitmap);//用Bitmap.FromHbitmap从hbitmap返回Bitmap
}
所用到的API声明:
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // driver name
string lpszDevice, // device name
string lpszOutput, // not used; should be NULL
Int64 lpInitData // optional printer data
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
);
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
GetDeviceCapsIndex nIndex // index of capability
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
UInt32 dwRop // raster operation code
);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(
IntPtr hdc // handle to DC
);
相关文章推荐
- Win32类型和.net类型的对应表 选择自 AppleDotnet 的 Blog
- C#中使用DotNetZip选择指定文件并解压
- 从MP3中提取歌曲信息(C#) 选择自 ufoer23 的 Blog
- 用C#实现计算机屏幕截取
- C# 2.0 杂项技术,以及C#语言的未来发展(选择自 shoutor 的 Blog )
- C#截取屏幕
- 在C#.net中如何操作XML 选择自 WeekZero 的 Blog
- WEB编程开发常用的代码 ( 选择自 AppleBBS 的 Blog)
- C#实现截取当前屏幕的方法
- SQLServer和Oracle常用函数对比 选择自 AppleBBS 的 Blog
- C# 模拟PrintScreen 和 Alt+PrintScreen截取屏幕图片
- c#重点知识详解(四) 选择自 masterall 的 Blog
- C# 2.0 泛型编程 选择自 shoutor 的 Blog
- C#截取屏幕↑
- C++程序员快速学习C#--- 选择自 hsuyuan 的 Blog
- 用.net自带的类发送E_Mail(ASP.NET+C#) 选择自 laihua 的 Blog
- C#执行存储过程的简化 选择自 spanzhang 的 Blog
- c#重点知识详解(三) 选择自 masterall 的 Blog
- ASP构造大数据量的分页SQL语句 选择自 AppleBBS 的 Blog
- 用纯C#实现截取屏幕指定区域