前阵子改了段c#截图功能的代码,现贴上来希望对大家有用
主文件
capturescreenform.cs
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
namespace capturescreen
{
/// <summary>
/// capturescreenform 的摘要说明。
/// </summary>
public class capturescreenform : system.windows.forms.form
{
private system.windows.forms.picturebox picbox;
private system.windows.forms.button exitbtn;
private system.windows.forms.button aboutbtn;
private system.windows.forms.button clearbtn;
private system.windows.forms.button savebtn;
private system.windows.forms.button fullscreenbtn;
private system.windows.forms.button mousecapturebtn;
public static image image = null ;
private system.windows.forms.panel panel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public capturescreenform()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.picbox = new system.windows.forms.picturebox();
this.fullscreenbtn = new system.windows.forms.button();
this.mousecapturebtn = new system.windows.forms.button();
this.clearbtn = new system.windows.forms.button();
this.savebtn = new system.windows.forms.button();
this.aboutbtn = new system.windows.forms.button();
this.exitbtn = new system.windows.forms.button();
this.panel1 = new system.windows.forms.panel();
this.panel1.suspendlayout();
this.suspendlayout();
//
// picbox
//
this.picbox.location = new system.drawing.point(0, 40);
this.picbox.name = "picbox";
this.picbox.size = new system.drawing.size(680, 312);
this.picbox.sizemode = system.windows.forms.pictureboxsizemode.autosize;
this.picbox.tabindex = 0;
this.picbox.tabstop = false;
//
// fullscreenbtn
//
this.fullscreenbtn.location = new system.drawing.point(0, 0);
this.fullscreenbtn.name = "fullscreenbtn";
this.fullscreenbtn.size = new system.drawing.size(96, 32);
this.fullscreenbtn.tabindex = 1;
this.fullscreenbtn.text = "全屏截取";
this.fullscreenbtn.click += new system.eventhandler(this.fullscreenbtn_click);
//
// mousecapturebtn
//
this.mousecapturebtn.location = new system.drawing.point(120, 0);
this.mousecapturebtn.name = "mousecapturebtn";
this.mousecapturebtn.size = new system.drawing.size(96, 32);
this.mousecapturebtn.tabindex = 2;
this.mousecapturebtn.text = "鼠标截取";
this.mousecapturebtn.click += new system.eventhandler(this.mousecapturebtn_click);
//
// clearbtn
//
this.clearbtn.location = new system.drawing.point(240, 0);
this.clearbtn.name = "clearbtn";
this.clearbtn.size = new system.drawing.size(88, 32);
this.clearbtn.tabindex = 3;
this.clearbtn.text = "清除截取图片";
this.clearbtn.click += new system.eventhandler(this.clearbtn_click);
//
// savebtn
//
this.savebtn.location = new system.drawing.point(352, 0);
this.savebtn.name = "savebtn";
this.savebtn.size = new system.drawing.size(96, 32);
this.savebtn.tabindex = 4;
this.savebtn.text = "保存截取图片";
this.savebtn.click += new system.eventhandler(this.savebtn_click);
//
// aboutbtn
//
this.aboutbtn.location = new system.drawing.point(472, 0);
this.aboutbtn.name = "aboutbtn";
this.aboutbtn.size = new system.drawing.size(96, 32);
this.aboutbtn.tabindex = 5;
this.aboutbtn.text = "关 于";
this.aboutbtn.click += new system.eventhandler(this.aboutbtn_click);
//
// exitbtn
//
this.exitbtn.location = new system.drawing.point(592, 0);
this.exitbtn.name = "exitbtn";
this.exitbtn.size = new system.drawing.size(88, 32);
this.exitbtn.tabindex = 6;
this.exitbtn.text = "退 出";
this.exitbtn.click += new system.eventhandler(this.exitbtn_click);
//
// panel1
//
this.panel1.controls.add(this.fullscreenbtn);
this.panel1.controls.add(this.mousecapturebtn);
this.panel1.controls.add(this.clearbtn);
this.panel1.controls.add(this.savebtn);
this.panel1.controls.add(this.aboutbtn);
this.panel1.controls.add(this.exitbtn);
this.panel1.location = new system.drawing.point(0, 0);
this.panel1.name = "panel1";
this.panel1.size = new system.drawing.size(680, 32);
this.panel1.tabindex = 7;
//
// capturescreenform
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.autoscroll = true;
this.clientsize = new system.drawing.size(696, 358);
this.controls.add(this.panel1);
this.controls.add(this.picbox);
this.name = "capturescreenform";
this.text = "截取屏幕";
this.panel1.resumelayout(false);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new capturescreenform());
}
protected override void onpaint(painteventargs e)
{
base.onpaint (e);
if(image != null)
{
this.picbox.image = image;
}
else
{
this.picbox.image = null;
this.picbox.refresh();
}
}
// 引用api
[system.runtime.interopservices.dllimportattribute("gdi32.dll")]
public static extern bool bitblt(
intptr hdcdest, //目标设备的句柄
int nxdest, // 目标对象的左上角的x坐标
int nydest, // 目标对象的左上角的x坐标
int nwidth, // 目标对象的矩形的宽度
int nheight, // 目标对象的矩形的长度
intptr hdcsrc, // 源设备的句柄
int nxsrc, // 源对象的左上角的x坐标
int nysrc, // 源对象的左上角的x坐标
system.int32 dwrop // 光栅的操作值
);
private void exitbtn_click(object sender, system.eventargs e)
{
application.exit();
}
private void aboutbtn_click(object sender, system.eventargs e)
{
messagebox.show("code by star in 2006-3-17,e_mail: [email protected] ,thank you to use this.");
}
private void clearbtn_click(object sender, system.eventargs e)
{
capturescreenform.image = null;
this.invalidate();
}
private void savebtn_click(object sender, system.eventargs e)
{
bool issave = true;
savefiledialog saveimagedialog = new savefiledialog();
saveimagedialog.title = "capture screen image save dialog";
saveimagedialog.filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
if(saveimagedialog.showdialog() == dialogresult.ok)
{
string filename = saveimagedialog.filename.tostring();
if(filename != "" && filename != null)
{
string fileextname = filename.substring(filename.lastindexof(".")+1).tostring();
system.drawing.imaging.imageformat imgformat = null;
if(fileextname!="")
{
switch(fileextname)
{
case "jpg":
imgformat = system.drawing.imaging.imageformat.jpeg;
break;
case "bmp":
imgformat = system.drawing.imaging.imageformat.bmp;
break;
case "gif":
imgformat = system.drawing.imaging.imageformat.gif;
break;
default:
messagebox.show("只能存取为: jpg,bmp,gif 格式");
issave = false;
break;
}
}
//默认保存为jpg格式
if(imgformat == null)
{
imgformat = system.drawing.imaging.imageformat.jpeg;
}
if(issave)
{
try
{
capturescreenform.image.save(filename,imgformat);
messagebox.show("图片已经成功保存!~~");
}
catch
{
messagebox.show("保存失败,你还没有截取过图片或已经清空图片!");
}
}
}
}
}
private void fullscreenbtn_click(object sender, system.eventargs e)
{
capturescreenform.image = windowfullscreen();
picbox.image = capturescreenform.image;
}
public static bitmap windowfullscreen()
{
//建立屏幕graphics
graphics grpscreen = graphics.fromhwnd(intptr.zero);
//根据屏幕大小建立位图
bitmap bitmap = new bitmap(screen.primaryscreen.bounds.width,screen.primaryscreen.bounds.height,grpscreen);
//建立位图相关graphics
graphics grpbitmap = graphics.fromimage(bitmap);
//建立屏幕上下文
intptr hdcscreen = grpscreen.gethdc();
//建立位图上下文
intptr hdcbitmap = grpbitmap.gethdc();
//将屏幕捕获保存在图位中
bitblt(hdcbitmap,0,0,bitmap.width,bitmap.height,hdcscreen,0,0,0x00cc0020);
//关闭位图句柄
grpbitmap.releasehdc(hdcbitmap);
//关闭屏幕句柄
grpscreen.releasehdc(hdcscreen);
//释放位图对像
grpbitmap.dispose();
//释放屏幕对像
grpscreen.dispose();
//返回捕获位图
return bitmap;
}
private void mousecapturebtn_click(object sender, system.eventargs e)
{
mousecaptureform mousecapture = new mousecaptureform();
mousecapture.show();
}
}
}
另一个截屏文件:
mousecaptureform.cs
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
namespace capturescreen
{
/// <summary>
/// mousecaptureform 的摘要说明。
/// </summary>
public class mousecaptureform : system.windows.forms.form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
//
//自定义变量
//
private point pot;
private rectangle area = rectangle.empty;
private image img;
private int index = -1;
private string noselectstring = "/n.请按下鼠标左键不放拖动选取截图区域/n/n .单击鼠标右键或按esc取消截图";
private string selectingstring = "/n.松开鼠标左键确定选取范围/n/n.按esc重新选择";
private string selectedstring = "/n.按鼠标左键调整选择范围/n/n .双击鼠标左键保存截图 /n/n.按鼠标右键重新选择 /n/n.esc键取消截图";
private system.windows.forms.label tiptext;
//
//系统生成定义
//
private system.componentmodel.container components = null;
public mousecaptureform()
{
this.bounds = system.windows.forms.screen.primaryscreen.bounds;
this.backgroundimage = capturescreenform.windowfullscreen();
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.tiptext = new system.windows.forms.label();
this.suspendlayout();
//
// tiptext
//
this.tiptext.backcolor = system.drawing.color.fromargb(((system.byte)(192)), ((system.byte)(192)), ((system.byte)(255)));
this.tiptext.forecolor = system.drawing.color.white;
this.tiptext.location = new system.drawing.point(10, 10);
this.tiptext.name = "tiptext";
this.tiptext.size = new system.drawing.size(166, 140);
this.tiptext.tabindex = 0;
this.tiptext.mousemove += new system.windows.forms.mouseeventhandler(this.tiptext_mousemove);
//
// mousecaptureform
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(456, 320);
this.controls.add(this.tiptext);
this.formborderstyle = system.windows.forms.formborderstyle.none;
this.name = "mousecaptureform";
this.showintaskbar = false;
this.startposition = system.windows.forms.formstartposition.windowsdefaultbounds;
this.text = "鼠标截屏";
this.topmost = true;
this.windowstate = system.windows.forms.formwindowstate.maximized;
this.keydown += new system.windows.forms.keyeventhandler(this.mousecaptureform_keydown);
this.load += new system.eventhandler(this.mousecaptureform_load);
this.doubleclick += new system.eventhandler(this.mousecaptureform_doubleclick);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 重载mousedown
/// </summary>
///
protected override void onmousedown(mouseeventargs e)
{
base.onmousedown (e);
{
if(this.area == rectangle.empty && e.button == mousebuttons.left)
{
this.tiptext.text = selectingstring;
this.area.location = new point(e.x,e.y);
}
this.pot = new point(e.x,e.y);
this.index = this.getselectedhandle(new point(e.x,e.y));
this.setcursor();
}
}
/// <summary>
/// 设置鼠标方案
/// </summary>
private void setcursor()
{
cursor cr = cursors.default;
if(index == 1 || index == 5)
{
cr = cursors.sizenwse;
}
else if(index == 2 || index == 6)
{
cr = cursors.sizens;
}
else if(index == 3 || index == 7)
{
cr = cursors.sizenesw;
}
else if(index == 4 || index == 8)
{
cr = cursors.sizewe;
}
else if(index == 0)
{
cr = cursors.sizeall;
}
cursor.current = cr;
}
protected override void onpaint(painteventargs e)
{
base.onpaint(e);
e.graphics.drawrectangle(new pen(this.forecolor),this.area);
for(int i = 1;i < 9;i++)
{
e.graphics.fillrectangle(new solidbrush(color.green),this.gethandlerect(i));
}
}
private rectangle gethandlerect(int index)
{
point point = gethandle(index);
return new rectangle(point.x - 3, point.y - 3, 6, 6);
}
private point gethandle(int index)
{
int x, y, xcenter, ycenter;
xcenter = area.x + area.width/2;
ycenter = area.y + area.height/2;
x = area.x;
y = area.y;
switch ( index )
{
case 1:
x = area.x;
y = area.y;
break;
case 2:
x = xcenter;
y = area.y;
break;
case 3:
x = area.right;
y = area.y;
break;
case 4:
x = area.right;
y = ycenter;
break;
case 5:
x = area.right;
y = area.bottom;
break;
case 6:
x = xcenter;
y = area.bottom;
break;
case 7:
x = area.x;
y = area.bottom;
break;
case 8:
x = area.x;
y = ycenter;
break;
}
return new point(x, y);
}
protected override void onmouseup(mouseeventargs e)
{
base.onmouseup (e);
int left = area.left;
int top = area.top;
int right = area.right;
int bottom = area.bottom;
area.x = math.min(left,right);
area.y = math.min(top,bottom);
area.width = math.abs(left-right);
area.height = math.abs(top-bottom);
this.tiptext.text = selectedstring;
if(e.button == mousebuttons.right)
{
if(this.area == rectangle.empty)
{
this.close();
}
else
{
this.area=rectangle.empty;
this.invalidate();
}
}
this.index = this.getselectedhandle(new point(e.x,e.y));
this.setcursor();
}
private int getselectedhandle(point p)
{
int index = -1;
for(int i = 1;i < 9;i++)
{
if(gethandlerect(i).contains(p))
{
index=i;
break;
}
}
if(this.area.contains(p))index = 0;
return index;
}
protected override void onmousemove(mouseeventargs e)
{
base.onmousemove (e);
if(this.capture)
{
this.movehandleto(new point(e.x,e.y));
this.invalidate();
}
else
{
this.index = this.getselectedhandle(new point(e.x,e.y));
this.setcursor();
}
}
private void movehandleto(point point)
{
int left = area.left;
int top = area.top;
int right = area.right;
int bottom = area.bottom;
switch ( index )
{
case 0:
area.x +=point.x - this.pot.x;
area.y += point.y - pot.y;
this.pot = point;
return;
case 1:
left = point.x;
top = point.y;
break;
case 2:
top = point.y;
break;
case 3:
right = point.x;
top = point.y;
break;
case 4:
right = point.x;
break;
case 5:
right = point.x;
bottom = point.y;
break;
case 6:
bottom = point.y;
break;
case 7:
left = point.x;
bottom = point.y;
break;
case 8:
left = point.x;
break;
}
this.pot = point;
area.x = left;
area.y = top;
area.width = right - left;
area.height = bottom - top;
}
private void mousecaptureform_doubleclick(object sender, system.eventargs e)
{
//修正截取图片过界提示内存不足bug
int left = area.left;
int right = area.right;
int top = area.top;
int bottom = area.bottom;
if(left < screen.primaryscreen.bounds.left)
{left = screen.primaryscreen.bounds.left; }
if(right > screen.primaryscreen.bounds.right )
{right = screen.primaryscreen.bounds.right;}
if(top < screen.primaryscreen.bounds.top)
{top = screen.primaryscreen.bounds.top;}
if(bottom > screen.primaryscreen.bounds.bottom)
{bottom = screen.primaryscreen.bounds.bottom;}
area.x = left;
area.y = top;
area.width = right - left;
area.height = bottom - top;
//截取选择区域图片
bitmap bm = new bitmap(this.backgroundimage);
this.img = bm.clone(this.area,system.drawing.imaging.pixelformat.format16bppargb1555);
capturescreenform.image = img;
capturescreenform.activeform.invalidate();
this.close();
}
private void mousecaptureform_keydown(object sender, system.windows.forms.keyeventargs e)
{
if(e.keydata == keys.escape && system.windows.forms.button.mousebuttons == mousebuttons.left)
{
this.area=rectangle.empty;
this.invalidate();
tiptext.text = noselectstring;
}
else if(e.keydata == keys.escape)
{this.close();}
}
private void tiptext_mousemove(object sender, system.windows.forms.mouseeventargs e)
{
if (tiptext.location.x == 10 && tiptext.location.y == 10)
{
point formloc = new point((screen.primaryscreen.bounds.width - 176),10) ;
tiptext.location = formloc;
}
else
{tiptext.location = new point(10,10);}
}
private void mousecaptureform_load(object sender, system.eventargs e)
{
tiptext.text = noselectstring;
}
}
}
新闻热点
疑难解答