当前位置: 编程技术>.net/c#/asp.net
C#实现类似qq的屏幕截图程序
来源: 互联网 发布时间:2014-10-13
本文导语: 因为近来想写个类似于远程桌面监控的程序,该程序中要用到屏幕捕捉.为实现该程序的一部分功能,做了个小DEMO.程序很简单,用到的技术也不多,只能实现类似qq的截图功能(方法虽然很笨)程序流程如下:1.截取整个屏幕并保存2...
因为近来想写个类似于远程桌面监控的程序,该程序中要用到屏幕捕捉.为实现该程序的一部分功能,做了个小DEMO.程序很简单,用到的技术也不多,只能实现类似qq的截图功能(方法虽然很笨)
程序流程如下:
1.截取整个屏幕并保存
2.新开一个全屏窗口,将保存的屏幕作为背景
3.鼠标拖动改变截取范围,右键取消
4.双击截取,保存在粘贴板,全屏窗口关闭
好了,下面的是代码部分
首先新建一个项目ScreenCutter(VS2005),将窗体名改为MainForm,再新建一个窗体ScreenBody.
添加一个按钮btnCutter到ScreenCutter并添加按钮事件:
利用Graphics的CopyFromScreen函数获取当前屏幕.
好了,现在按下按钮全屏窗口就会出来了.
下面讲全屏窗口ScreenBody,首先设置窗体的FormBorderStyle为None,然后声明以下变量
之后就是窗体的鼠标函数了,里面很多代码都没有作出整理,看了一下,整理后的代码应该会更少更精简的
private void ScreenBody_DoubleClick(object sender, EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Left &&Rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
{
//保存的时候有很多种方法的......我这里只用了这种
Image memory = new Bitmap(Rect.Width, Rect.Height);
Graphics g = 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, MouseEventArgs e)
{
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, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDowned = false;
RectReady = true;
}
}
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
{
if (RectReady == false)
{
if (isDowned == true)
{
Image New = 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, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
MainPainter = this.CreateGraphics();
pen = new Pen(Brushes.Blue);
isDowned = false;
baseImage = this.BackgroundImage;
Rect = new Rectangle();
RectReady = false;
}
辅助函数
本来应该写更多的辅助函数的,将窗体响应函数里面的代码放到里面来,不过本人很懒,就这样将就了.呵呵
private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
{
int width = 0;
int heigth = 0;
if (Mouse_y
c#通过委托delegate与Dictionary实现action选择器代码举例
C#实现获取枚举中元素个数的方法
C#实现自定义双击事件
C#键盘输入回车键实现点击按钮效果的方法
C#实现获取一年中是第几个星期的方法
C#实现Datatable排序的方法
C#实现装箱与拆箱操作简单实例
解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法
C#下实现创建和删除目录的实例代码
使用C#实现在屏幕上画图效果的代码实例
C#实现过滤html标签并保留a标签的方法
c#实现TextBox只允许输入数字
C# Winform 整个窗口拖动的实现代码
c# ListView实现双击Item事件的变通方法
C#实现随鼠标移动窗体实例
C#中的FileUpload 选择后的预览效果具体实现
C# 窗体隐藏及任务管理器中禁止关闭的实现代码
C#的锯齿数组以及C++实现代码
C#格式化文件大小的实现代码
C#怎样才能实现窗体最小化到托盘呢?
程序流程如下:
1.截取整个屏幕并保存
2.新开一个全屏窗口,将保存的屏幕作为背景
3.鼠标拖动改变截取范围,右键取消
4.双击截取,保存在粘贴板,全屏窗口关闭
好了,下面的是代码部分
首先新建一个项目ScreenCutter(VS2005),将窗体名改为MainForm,再新建一个窗体ScreenBody.
添加一个按钮btnCutter到ScreenCutter并添加按钮事件:
private void btnCutter_Click(object sender, EventArgs e)
{
Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
ScreenBody body = new ScreenBody();
body.BackgroundImage = img;
body.Show();
}
Screen.AllScreens[0]是获取当前所有设备窗口的第一个,我这里只有一个显示器,当然我就是第一个.{
Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
ScreenBody body = new ScreenBody();
body.BackgroundImage = img;
body.Show();
}
利用Graphics的CopyFromScreen函数获取当前屏幕.
好了,现在按下按钮全屏窗口就会出来了.
下面讲全屏窗口ScreenBody,首先设置窗体的FormBorderStyle为None,然后声明以下变量
private Graphics MainPainter; //主画笔
private Pen pen; //就是笔咯
private bool isDowned; //判断鼠标是否按下
private bool RectReady; //矩形是否绘制完成
private Image baseImage; //基本图形(原来的画面)
private Rectangle Rect; //就是要保存的矩形
private Point downPoint; //鼠标按下的点
int tmpx;
int tmpy;
private Pen pen; //就是笔咯
private bool isDowned; //判断鼠标是否按下
private bool RectReady; //矩形是否绘制完成
private Image baseImage; //基本图形(原来的画面)
private Rectangle Rect; //就是要保存的矩形
private Point downPoint; //鼠标按下的点
int tmpx;
int tmpy;
之后就是窗体的鼠标函数了,里面很多代码都没有作出整理,看了一下,整理后的代码应该会更少更精简的
private void ScreenBody_DoubleClick(object sender, EventArgs e)
{
if (((MouseEventArgs)e).Button == MouseButtons.Left &&Rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
{
//保存的时候有很多种方法的......我这里只用了这种
Image memory = new Bitmap(Rect.Width, Rect.Height);
Graphics g = 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, MouseEventArgs e)
{
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, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDowned = false;
RectReady = true;
}
}
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
{
if (RectReady == false)
{
if (isDowned == true)
{
Image New = 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, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
MainPainter = this.CreateGraphics();
pen = new Pen(Brushes.Blue);
isDowned = false;
baseImage = this.BackgroundImage;
Rect = new Rectangle();
RectReady = false;
}
辅助函数
本来应该写更多的辅助函数的,将窗体响应函数里面的代码放到里面来,不过本人很懒,就这样将就了.呵呵
private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
{
int width = 0;
int heigth = 0;
if (Mouse_y