C#实现类似qq的屏幕截图程序

本文介绍了一个简单的屏幕截图程序的设计与实现过程。通过使用C#语言,作者实现了包括全屏截图、区域选择及复制到剪贴板等功能。文章详细展示了如何使用Graphics的CopyFromScreen方法捕获屏幕,并通过鼠标操作实现截图区域的选择。
因为近来想写个类似于远程桌面监控的程序,该程序中要用到屏幕捕捉.为实现该程序的一部分功能,做了个小DEMO.程序很简单,用到的技术也不多,只能实现类似qq的截图功能(方法虽然很笨)
程序流程如下:

1.截取整个屏幕并保存
2.新开一个全屏窗口,将保存的屏幕作为背景
3.鼠标拖动改变截取范围,右键取消
4.双击截取,保存在粘贴板,全屏窗口关闭

好了,下面的是代码部分

首先新建一个项目ScreenCutter(VS2005),将窗体名改为MainForm,再新建一个窗体ScreenBody.
添加一个按钮btnCutter到ScreenCutter并添加按钮事件:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> private void btnCutter_Click( object sender,EventArgse)
{
Imageimg
= new Bitmap(Screen.AllScreens[ 0 ].Bounds.Width,Screen.AllScreens[ 0 ].Bounds.Height);
Graphicsg
= Graphics.FromImage(img);
g.CopyFromScreen(
new Point( 0 , 0 ), new Point( 0 , 0 ),Screen.AllScreens[ 0 ].Bounds.Size);
ScreenBodybody
= new ScreenBody();
body.BackgroundImage
= img;
body.Show();
}
Screen.AllScreens[0]是获取当前所有设备窗口的第一个,我这里只有一个显示器,当然我就是第一个.
利用Graphics的CopyFromScreen函数获取当前屏幕.

好了,现在按下按钮全屏窗口就会出来了.

下面讲全屏窗口ScreenBody,首先设置窗体的FormBorderStyle为None,然后声明以下变量
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> private GraphicsMainPainter; //主画笔
private Penpen; //就是笔咯
private bool isDowned; //判断鼠标是否按下
private bool RectReady; //矩形是否绘制完成
private ImagebaseImage; //基本图形(原来的画面)
private RectangleRect; //就是要保存的矩形
private PointdownPoint; //鼠标按下的点
int tmpx;
int tmpy;

之后就是窗体的鼠标函数了,里面很多代码都没有作出整理,看了一下,整理后的代码应该会更少更精简的

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->

private void ScreenBody_DoubleClick( object sender,EventArgse)
{
if (((MouseEventArgs)e).Button == MouseButtons.Left && Rect.Contains(((MouseEventArgs)e).X,((MouseEventArgs)e).Y))
{
//保存的时候有很多种方法的......我这里只用了这种
Imagememory
= new Bitmap(Rect.Width,Rect.Height);
Graphicsg
= Graphics.FromImage(memory);
g.CopyFromScreen(Rect.X
+ 1 ,Rect.Y + 1 , 0 , 0 ,Rect.Size);
Clipboard.SetImage(memory);
this .Close();
}
}

private void ScreenBody_MouseDown( object sender,MouseEventArgse)
{
if (e.Button == MouseButtons.Left)
{
isDowned
= true ;

if (RectReady == false )
{
Rect.X
= e.X;
Rect.Y
= e.Y;
downPoint
= new Point(e.X,e.Y);
}
if (RectReady == true )
{
tmpx
= e.X;
tmpy
= e.Y;
}

}
if (e.Button == MouseButtons.Right)
{
if (RectReady != true )
{
this .Close();
return ;
}
MainPainter.
DrawImage(baseImage, 0 , 0 );
RectReady
= false ;
}

}

private void ScreenBody_MouseUp( object sender,MouseEventArgse)
{
if (e.Button == MouseButtons.Left)
{
isDowned
= false ;
RectReady
= true ;
}
}

private void ScreenBody_MouseMove( object sender,MouseEventArgse)
{

if (RectReady == false )
{
if (isDowned == true )
{
ImageNew
= DrawScreen((Image)baseImage.Clone(),e.X,e.Y);
MainPainter.DrawImage(New,
0 , 0 );
New.Dispose();
}
}
if (RectReady == true )
{
if (Rect.Contains(e.X,e.Y))
{
// this.Cursor=Cursors.Hand;
if (isDowned == true )
{
// 和上一次的位置比较获取偏移量
Rect.X = Rect.X + e.X - tmpx;
Rect.Y
= Rect.Y + e.Y - tmpy;
// 记录现在的位置
tmpx = e.X;
tmpy
= e.Y;
MoveRect((Image)baseImage.Clone(),Rect);
}
}

}

}

private void ScreenBody_Load( object sender,EventArgse)
{
this .WindowState = FormWindowState.Maximized;
MainPainter
= this .CreateGraphics();
pen
= new Pen(Brushes.Blue);
isDowned
= false ;
baseImage
= this .BackgroundImage;
Rect
= new Rectangle();
RectReady
= false ;
}


辅助函数
本来应该写更多的辅助函数的,将窗体响应函数里面的代码放到里面来,不过本人很懒,就这样将就了.呵呵

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
private void DrawRect(GraphicsPainter, int Mouse_x, int Mouse_y)
{
int width = 0 ;
int heigth = 0 ;
if (Mouse_y < Rect.Y)
{
Rect.Y
= Mouse_y;
heigth
= downPoint.Y - Mouse_y;
}
else
{
heigth
= Mouse_y - downPoint.Y;
}
if (Mouse_x < Rect.X)
{
Rect.X
= Mouse_x;
width
= downPoint.X - Mouse_x;
}
else
{
width
= Mouse_x - downPoint.X;
}
Rect.Size
= new Size(width,heigth);
Painter.DrawRectangle(pen,Rect);
}

private ImageDrawScreen(Imageback, int Mouse_x, int Mouse_y)
{
GraphicsPainter
= Graphics.FromImage(back);
DrawRect(Painter,Mouse_x,Mouse_y);
return back;
}
private void MoveRect(Imageimage,RectangleRect)
{
GraphicsPainter
= Graphics.FromImage(image);
Painter.DrawRectangle(pen,Rect.X,Rect.Y,Rect.Width,Rect.Height);
DrawRects(Painter);
MainPainter.DrawImage(image,
0 , 0 );
image.Dispose();
}


到这里,代码就算是写完了,运行



截取结果,这里截取的边界没有控制好,所以还有边界可以见到,稍微设置一下就可以了



好了,这个东东就这样搞完了,接下来要做利用钩子的了....希望能够快点完成,累呀~~~~

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值