码迷,mamicode.com
首页 > Windows程序 > 详细

用C#实现的小截屏软件

时间:2015-08-28 17:07:52      阅读:492      评论:0      收藏:0      [点我收藏+]

标签:

第一次写博客,文笔较差,将就看吧

日常生活中会经常使用到截屏功能,使用最多的无非就是Windows自带的截图工具、QQ截图和PrintScreen键,但要达到截图到word或保存到文件,需要鼠标选择多次。比如我们想截图并将图插入到Word中,不需要保存图片,我们希望直接点击截图按钮,选择截图区域,Ctrl+V简单三步就行了(感觉说了好多废话),切入正题。

1.基本功能

选择屏幕区域后提醒你保存所截的图片,或直接将所截图片放到剪切板里(以便用Ctrl+V粘贴)。

2.界面设计

界面很简单,无非就是可实现以上功能的两个按钮和其他文字,见图:

技术分享

界面的属性需要注意几个问题:

1)窗口设为固定大小,并禁用窗口最大化(因为我们不希望窗口大小会变)

技术分享

技术分享

2)窗口最好设为顶置

技术分享

3)把两个文字label和两个按钮都放到一个panel里,以便于后面程序对控件属性的操作

4)那么大的按钮,最好改变一下它的样式,还可以设置背景为gif动图

技术分享

3.功能实现

那么关键问题来了,怎么截图呢?见下图

技术分享

原理其实很简单,就是在点击按钮后,窗口变为全屏覆盖在屏幕最上方,并变为半透明,使你能看到窗口下面的屏幕内容,然后拖动鼠标(此时其实是在软件的主窗口上拖动,这样就方便程序捕捉鼠标坐标了),根据坐标在屏幕上绘制选框,接着松开鼠标后,根据鼠标落下和松开的坐标,截取屏幕,最后保存或复制到剪切板。

4.敲代码吧

  1: using System;
  2: using System.Windows.Forms;
  3: using System.Drawing;//绘图要用
  4: using System.Threading;//延时函数要用
  5: 
  6: namespace 截屏
  7: {
  8:     public partial class Form1 : Form
  9:     {
 10:         bool mouseDown = false, havePainted = false;
 11:         Point start, end;
 12:         Point start1, end1;
 13:         Size size = new Size(0, 0);
 14:         bool saveFile = true;
 15:         public Form1()
 16:         {
 17:             InitializeComponent();
 18:         }
 19: 
 20:         private void button1_Click(object sender, EventArgs e)
 21:         {
 22:             ReadyToCaptrue();
 23:             saveFile = true;
 24:         }
 25: 
 26:         private void Form1_MouseDown(object sender, MouseEventArgs e)
 27:         {
 28:             start = GetRealPoint(e.Location);
 29:             mouseDown = true;
 30:         }
 31: 
 32:         private void Form1_MouseUp(object sender, MouseEventArgs e)
 33:         {
 34:             if (size.Width != 0 && size.Height != 0)
 35:             {
 36:                 ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed);
 37:                 havePainted = false;
 38:             }
 39:             end = GetRealPoint(e.Location);
 40:             if (start.X > end.X)
 41:             {
 42:                 int temp = end.X;
 43:                 end.X = start.X;
 44:                 start.X = temp;
 45:             }
 46: 
 47:             if (start.Y > end.Y)
 48:             {
 49:                 int temp = end.Y;
 50:                 end.Y = start.Y;
 51:                 start.Y = temp;
 52:             }
 53:             this.Opacity = 0.0;
 54:             Thread.Sleep(200);
 55:             if (end.X - start.X > 0 && end.Y - start.Y > 0)
 56:             {
 57:                 Bitmap bit = new Bitmap(end.X - start.X, end.Y - start.Y);
 58:                 Graphics g = Graphics.FromImage(bit);
 59:                 g.CopyFromScreen(start, new Point(0, 0), bit.Size);
 60:                 if (saveFile)
 61:                 {
 62:                     SaveFileDialog saveFileDialog = new SaveFileDialog();
 63:                     saveFileDialog.Filter = "png|*.png|bmp|*.bmp|jpg|*.jpg|gif|*.gif";
 64:                     if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
 65:                     {
 66:                         bit.Save(saveFileDialog.FileName);
 67:                     }
 68:                 }
 69:                 else
 70:                 {
 71:                     Clipboard.SetImage(bit);
 72:                 }
 73:                 g.Dispose();
 74:             }
 75:             this.WindowState = FormWindowState.Normal;
 76:             this.FormBorderStyle = FormBorderStyle.FixedSingle;
 77:             panel1.Visible = true;
 78:             this.Opacity = 1;
 79:             mouseDown = false;
 80:         }
 81: 
 82:         private void Form1_MouseMove(object sender, MouseEventArgs e)
 83:         {
 84:             if (mouseDown)
 85:             {
 86:                 if (size.Width != 0 && size.Height != 0 && havePainted)
 87:                 {
 88:                     ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed);
 89:                 }
 90:                 end1 = GetRealPoint(e.Location);
 91:                 size.Width = Math.Abs(end1.X - start.X);
 92:                 size.Height = Math.Abs(end1.Y - start.Y);
 93:                 start1.X = (start.X > end1.X) ? end1.X : start.X;
 94:                 start1.Y = (start.Y > end1.Y) ? end1.Y : start.Y;
 95: 
 96:                 if (size.Width != 0 && size.Height != 0)
 97:                 {
 98:                     ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed);
 99:                     havePainted = true;
100:                 }
101:             }
102:         }
103: 
104:         private void button2_Click(object sender, EventArgs e)
105:         {
106:             ReadyToCaptrue();
107:             saveFile = false;
108:         }
109: 
110:         private void Form1_Load(object sender, EventArgs e)
111:         {
112: 
113:         }
114: 
115:         private void ReadyToCaptrue()
116:         {
117:             this.Opacity = 0.1;
118:             panel1.Visible = false;
119:             this.FormBorderStyle = FormBorderStyle.None;
120:             this.WindowState = FormWindowState.Maximized;
121:         }
122: 
123:         private Point GetRealPoint(Point position)
124:         {
125:             position.X -= 0;
126:             position.Y -= 0;
127:             return position;
128:         }
129:     }
130: }
131: 

 

用C#实现的小截屏软件

标签:

原文地址:http://www.cnblogs.com/jcwn888/p/4766759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!