学习笔记:GDI,截图功能实现
2012-11-04 18:22
405 查看
(以下是参考了作者:stg609的博客,自己动手做后,写下来的步骤,方便自己以后复习,无剽窃他人作品之意,勿喷!)
需求:基本实现截图功能.
工具:Visual studio 2008 (winform).
结构:共有两个form,第一个form为:originForm
由一个button控件和一个richTextBox组成.
第一个form的代码如下:
第二个form:Catch.在其属性中,将FormBorderStyle设为None.WindowState设为Maximized.
Catch中的代码如下:
参考资料:http://www.cnblogs.com/stg609/archive/2008/03/19/1113694.html
需求:基本实现截图功能.
工具:Visual studio 2008 (winform).
结构:共有两个form,第一个form为:originForm
由一个button控件和一个richTextBox组成.
第一个form的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace Catch { public partialclass originForm : Form { publicoriginForm() { InitializeComponent(); } privatevoid button1_Click(object sender, EventArgs e) { this.Hide();//隐藏窗体 Thread.Sleep(50);//线程休息 CatchCatchForm = new Catch();//新建一个名为CatchForm的窗体 BitmapCatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width,Screen.AllScreens[0].Bounds.Height);//创建一个宽高与屏幕大小相同的位图 Graphics g = Graphics.FromImage(CatchBmp); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), newSize(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height)); //将屏幕图像复制到位图中 CatchForm.BackgroundImage = CatchBmp; if(CatchForm.ShowDialog() == DialogResult.OK) { IDataObject iData = Clipboard.GetDataObject(); DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap); if(iData.GetDataPresent(DataFormats.Bitmap)) { richTextBox1.Paste(myFormat); Clipboard.Clear(); } this.Show(); } } } }
第二个form:Catch.在其属性中,将FormBorderStyle设为None.WindowState设为Maximized.
Catch中的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Catch { public partial class Catch : Form { private Point DownPoint = Point.Empty;//鼠标左键按下的坐标 private bool CatchFinished = false;//确定截图完成 private bool CatchStart = false;//表示截图开始 private Bitmap originBmp;//保存原始图像 private Rectangle CatchRect;//保存截图的矩形 public Catch() { InitializeComponent(); } private void Catch_Load(object sender, EventArgs e) { this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);//双缓冲,可减少图片闪烁 this.UpdateStyles(); originBmp = new Bitmap(this.BackgroundImage);//将整个屏幕的图片放到originBmp中 } private void Catch_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right)//鼠标点击右键,完成截图 { this.DialogResult = DialogResult.OK;//点击右键后,将DialogResult.OK赋值给DialogResult this.Close();//关闭窗口 } } private void Catch_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left)//点击鼠标左键 { if (CatchStart == false) { CatchStart = true;//开始截图 DownPoint = new Point(e.X, e.Y);//记录点击点坐标 } } } private void Catch_MouseMove(object sender, MouseEventArgs e) { if (CatchStart)//如果开始截图 { Bitmap destBmp = (Bitmap)originBmp.Clone(); Point newPoint = new Point(DownPoint.X, DownPoint.Y); Graphics g = Graphics.FromImage(destBmp); Pen p = new Pen(Color.Blue, 1); int width = Math.Abs(e.X - DownPoint.X);//X坐标的差的绝对值为宽 int height = Math.Abs(e.Y - DownPoint.Y);//Y坐标的差的绝对值为高 if (e.X < DownPoint.X) { newPoint.X = e.X; } if (e.Y < DownPoint.Y) { newPoint.Y = e.Y; } CatchRect = new Rectangle(newPoint, new Size(width, height));//获得新的矩形 g.DrawRectangle(p, CatchRect); g.Dispose();//释放画板 p.Dispose(); Graphics g1 = this.CreateGraphics(); g1 = this.CreateGraphics(); g1.DrawImage(destBmp, new Point(0, 0));//将刚才所画的图片画到这个窗体上 g1.Dispose(); destBmp.Dispose(); } } private void Catch_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (CatchStart) { CatchStart = false; CatchFinished = true;//标记为截图完成状态 } } } private void Catch_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && CatchFinished)//在截图的矩形中,双击鼠标,完成截图.此处判断有无左键双击,并且截图状态是否属于完成状态. { if(CatchRect.Contains(new Point(e.X,e.Y)))//判断有无在矩形中 { Bitmap CatchedBmp = new Bitmap(CatchRect.Width,CatchRect.Height);//新建CatchedBmp Graphics g = Graphics.FromImage(CatchedBmp);//在CatchedBmp上创建画板 g.DrawImage(originBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel);//将截图画上CatchedBmp Clipboard.SetImage(CatchedBmp);//复制到粘贴板中 g.Dispose(); CatchFinished = false; CatchedBmp.Dispose(); this.DialogResult = DialogResult.OK; this.Close(); } } } } }
参考资料:http://www.cnblogs.com/stg609/archive/2008/03/19/1113694.html
相关文章推荐
- Spring学习笔记(14)----使用CGLIB实现AOP功能
- JavaScript学习笔记:用JavaScript部分小功能的实现
- QTableView和QTableWidget翻页功能实现---Qt学习笔记5
- Java学习笔记——应用矢量和迭代器来实现类似链表的功能
- Dynamic CRM 2013学习笔记(十二)实现子表合计(汇总,求和)功能的通用插件
- node学习笔记(五)网站访问量统计功能实现
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- struts2 学习笔记(简单的登陆功能实现)
- 研读asp.net排课功能实现学习笔记
- Andorid学习笔记:实现图片选择器功能(上)
- android 学习笔记3--静默安装功能的实现
- ATL学习笔记(3): QueryInterface功能的实现
- iOS学习笔记55- iOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
- 学习笔记二:checkbox实现全选功能
- 【unity学习笔记】unity实现钩子功能
- Java学习笔记之 swing 基本实现QQ截屏功能
- GTK进阶学习:GTK实现截图功能( 可以指定截图范围 )
- seaJs学习笔记之seaJs实现webQQ部分功能2
- Qt5基础(六)实现Qt文本编辑功能学习笔记
- [原创]java WEB学习笔记89:Hibernate学习之路-- -Hibernate检索方式(5种),HQL介绍,实现功能,实现步骤,