第一次写博客,文笔较差,将就看吧
日常生活中会经常使用到截屏功能,使用最多的无非就是Windows自带的截图工具、QQ截图和PrintScreen键,但要达到截图到word或保存到文件,需要鼠标选择多次。比如我们想截图并将图插入到Word中,不需要保存图片,我们希望直接点击截图按钮,选择截图区域,Ctrl+V简单三步就行了(感觉说了好多废话),切入正题。
1.基本功能
选择屏幕区域后提醒你保存所截的图片,或直接将所截图片放到剪切板里(以便用Ctrl+V粘贴)。
2.界面设计
界面很简单,无非就是可实现以上功能的两个按钮和其他文字,见图:
界面的属性需要注意几个问题:
1)窗口设为固定大小,并禁用窗口最大化(因为我们不希望窗口大小会变)
2)窗口最好设为顶置
3)把两个文字label和两个按钮都放到一个panel里,以便于后面程序对控件属性的操作
4)那么大的按钮,最好改变一下它的样式,还可以设置背景为gif动图
3.功能实现
那么关键问题来了,怎么截图呢?见下图
原理其实很简单,就是在点击按钮后,窗口变为全屏覆盖在屏幕最上方,并变为半透明,使你能看到窗口下面的屏幕内容,然后拖动鼠标(此时其实是在软件的主窗口上拖动,这样就方便程序捕捉鼠标坐标了),根据坐标在屏幕上绘制选框,接着松开鼠标后,根据鼠标落下和松开的坐标,截取屏幕,最后保存或复制到剪切板。
4.敲代码吧
using System; using System.Windows.Forms; using System.Drawing;//绘图要用 using System.Threading;//延时函数要用 namespace 截屏 { public partial class Form1 : Form { bool mouseDown = false, havePainted = false; Point start, end; Point start1, end1; Size size = new Size(0, 0); bool saveFile = true; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReadyToCaptrue(); saveFile = true; } private void Form1_MouseDown(object sender, MouseEventArgs e) { start = e.Location; mouseDown = true; } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (size.Width != 0 && size.Height != 0) { ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed); havePainted = false; } end = e.Location; if (start.X > end.X) { int temp = end.X; end.X = start.X; start.X = temp; } if (start.Y > end.Y) { int temp = end.Y; end.Y = start.Y; start.Y = temp; } this.Opacity = 0.0; Thread.Sleep(200); if (end.X - start.X > 0 && end.Y - start.Y > 0) { Bitmap bit = new Bitmap(end.X - start.X, end.Y - start.Y); Graphics g = Graphics.FromImage(bit); g.CopyFromScreen(start, new Point(0, 0), bit.Size); if (saveFile) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "png|*.png|bmp|*.bmp|jpg|*.jpg|gif|*.gif"; if (saveFileDialog.ShowDialog() != DialogResult.Cancel) { bit.Save(saveFileDialog.FileName); } } else { Clipboard.SetImage(bit); } g.Dispose(); } this.WindowState = FormWindowState.Normal; this.FormBorderStyle = FormBorderStyle.FixedSingle; panel1.Visible = true; this.Opacity = 1; mouseDown = false; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { if (size.Width != 0 && size.Height != 0 && havePainted) { ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed); } end1 = e.Location; size.Width = Math.Abs(end1.X - start.X); size.Height = Math.Abs(end1.Y - start.Y); start1.X = (start.X > end1.X) ? end1.X : start.X; start1.Y = (start.Y > end1.Y) ? end1.Y : start.Y; if (size.Width != 0 && size.Height != 0) { ControlPaint.DrawReversibleFrame(new Rectangle(start1, size), Color.Transparent, FrameStyle.Dashed); havePainted = true; } } } private void button2_Click(object sender, EventArgs e) { ReadyToCaptrue(); saveFile = false; } private void Form1_Load(object sender, EventArgs e) { } private void ReadyToCaptrue() { this.Opacity = 0.1; panel1.Visible = false; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。