




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#实现屏幕截图运行效果:代码: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.Drawing.Drawing2D;namespace Test.UIFrm public class Form2 : Form #region 窗体设计器代码 private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) if (disposing) if (components != null) components.Dispose(); base.Dispose(disposing); private void InitializeComponent() this.tiptext = new System.Windows.Forms.Label(); this.btnOk = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.SuspendLayout(); / / tiptext / this.tiptext.BackColor = System.Drawing.Color.DimGray; this.tiptext.ForeColor = System.Drawing.Color.Transparent; this.tiptext.Location = new System.Drawing.Point(86, 9); this.tiptext.Name = tiptext; this.tiptext.Size = new System.Drawing.Size(85, 13); this.tiptext.TabIndex = 0; this.tiptext.Text = 1200*1200; this.tiptext.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.tiptext.Visible = false; / / btnOk / this.btnOk.Location = new System.Drawing.Point(0, 0); this.btnOk.Name = btnOk; this.btnOk.Size = new System.Drawing.Size(40, 20); this.btnOk.TabIndex = 22; this.btnOk.Text = 完成; this.btnOk.Visible = false; this.btnOk.Click += new System.EventHandler(this.btnOk_Click); / / btnSave / this.btnSave.Location = new System.Drawing.Point(0, 0); this.btnSave.Name = btnSave; this.btnSave.Size = new System.Drawing.Size(40, 20); this.btnSave.TabIndex = 1; this.btnSave.Text = 保存; this.btnSave.Visible = false; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); / / btnCancel / this.btnCancel.Location = new System.Drawing.Point(0, 0); this.btnCancel.Name = btnCancel; this.btnCancel.Size = new System.Drawing.Size(40, 20); this.btnCancel.TabIndex = 3; this.btnCancel.Text = 取消; this.btnCancel.Visible = false; this.btnCancel.Click += new EventHandler(btnCancel_Click); / / Form2 / this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(456, 320); this.Controls.Add(this.btnCancel); this.Controls.Add(this.tiptext); this.Controls.Add(this.btnOk); this.Controls.Add(this.btnSave); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = Form2; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds; this.Text = 鼠标截屏; this.TopMost = true; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.DoubleClick += new System.EventHandler(this.MouseCaptureForm_DoubleClick); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MouseCaptureForm_KeyDown); this.ResumeLayout(false); #endregion private Point pot; private Rectangle area = Rectangle.Empty; private Image img; private int index = -1; private System.Windows.Forms.Label tiptext; public Form2() this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; this.BackgroundImage = this.GetScreen(); InitializeComponent(); this.DoubleBuffered = true; #region 操作栏 private System.Windows.Forms.Button btnOk; private System.Windows.Forms.Button btnSave; private void btnSave_Click(object sender, EventArgs e) this.SaveToFile(); private void btnOk_Click(object sender, EventArgs e) this.SaveToClipBoard(); private void btnCancel_Click(object sender, EventArgs e) this.Close(); private void ToolBars(bool show) if (show) btnCancel.Location = new Point(this.area.X + this.area.Width - btnCancel.Width, this.area.Y + this.area.Height + 10); btnOk.Location = new Point(this.area.X + this.area.Width - btnOk.Width - btnCancel.Width, this.area.Y + this.area.Height + 10); btnSave.Location = new Point(this.area.X + this.area.Width - btnOk.Width - btnSave.Width - btnCancel.Width, this.area.Y + this.area.Height + 10); btnOk.Visible = show; btnSave.Visible = show; btnCancel.Visible = show; #endregion #region 获取屏幕 / 引用API System.Runtime.InteropServices.DllImportAttribute(gdi32.dll) public static extern bool BitBlt( IntPtr hdcDest, /目标设备的句柄 int nXDest, / 目标对象的左上角的X坐标 int nYDest, / 目标对象的左上角的X坐标 int nWidth, / 目标对象的矩形的宽度 int nHeight, / 目标对象的矩形的长度 IntPtr hdcSrc, / 源设备的句柄 int nXSrc, / 源对象的左上角的X坐标 int nYSrc, / 源对象的左上角的X坐标 System.Int32 dwRop / 光栅的操作值 ); private Bitmap GetScreen() /建立屏幕Graphics Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero); /根据屏幕大小建立位图 Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grpScreen); /建立位图相关Graphics Graphics grpBitmap = Graphics.FromImage(bitmap); /建立屏幕上下文 IntPtr hdcScreen = grpScreen.GetHdc(); /建立位图上下文 IntPtr hdcBitmap = grpBitmap.GetHdc(); /将屏幕捕获保存在图位中 BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcScreen, 0, 0, 0x00CC0020); /关闭位图句柄 grpBitmap.ReleaseHdc(hdcBitmap); /关闭屏幕句柄 grpScreen.ReleaseHdc(hdcScreen); /释放位图对像 grpBitmap.Dispose(); /释放屏幕对像 grpScreen.Dispose(); /返回捕获位图 return bitmap; #endregion #region 绘制窗口 / / 设置鼠标方案 / private void SetCursor() Cursor cr = Cursors.Default; if (index = 1 | index = 5) cr = Cursors.SizeNWSE; else if (index = 2 | index = 6) cr = Cursors.SizeNS; else if (index = 3 | index = 7) cr = Cursors.SizeNESW; else if (index = 4 | index = 8) cr = Cursors.SizeWE; else if (index = 0) cr = Cursors.SizeAll; Cursor.Current = cr; protected override void OnPaint(PaintEventArgs e) SuspendLayout(); base.OnPaint(e); Rectangle tmp = this.GetSelectedRectangle(); e.Graphics.DrawRectangle(new Pen(Color.Green), tmp); this.tiptext.Location = new Point(tmp.X, tmp.Y - this.tiptext.Height - 4); /mask GraphicsPath path = new GraphicsPath(); int x = tmp.X, y = tmp.Y, w = tmp.Width, h = tmp.Height; int sw = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, sh = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; path.AddRectangle(new Rectangle(0, 0, sw, y); path.AddRectangle(new Rectangle(0, y, x, h); path.AddRectangle(new Rectangle(x + w, y, sw - x - w, h); path.AddRectangle(new Rectangle(0, y + h, sw, sh - y - h); Region region = new Region(path); SolidBrush brush = new SolidBrush(Color.FromArgb(100, Color.Black); e.Graphics.FillRegion(brush, region); if (this.area != Rectangle.Empty) for (int i = 1; i 9; i+) e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.GetHandleRect(i); ResumeLayout(true); / / 获取选择的矩形区域 / / private Rectangle GetSelectedRectangle() int x1 = this.area.X, y1 = this.area.Y , x2 = this.area.X + this.area.Width, y2 = this.area.Y + this.area.Height; Rectangle tmp = new Rectangle(x1, y1, Math.Abs(this.area.Width), Math.Abs(this.area.Height); if (x1 x2) tmp.X = x2; if (y1 y2) tmp.Y = y2; return tmp; private Rectangle GetHandleRect(int index) int x, y, xCenter, yCenter; xCenter = area.X + area.Width / 2; yCenter = area.Y + area.Height / 2; x = area.X; y = area.Y; switch (index) case 1: x = area.X; y = area.Y; break; case 2: x = xCenter; y = area.Y; break; case 3: x = area.Right; y = area.Y; break; case 4: x = area.Right; y = yCenter; break; case 5: x = area.Right; y = area.Bottom; break; case 6: x = xCenter; y = area.Bottom; break; case 7: x = area.X; y = area.Bottom; break; case 8: x = area.X; y = yCenter; break; Point point = new Point(x, y); return new Rectangle(point.X - 3, point.Y - 3, 6, 6); private int GetSelectedHandle(Point p) int index = -1; for (int i = 1; i 9; i+) if (GetHandleRect(i).Contains(p) index = i; break; if (this.area.Contains(p) index = 0; return index; #endregion #region 鼠标事件 protected override void OnMouseDown(MouseEventArgs e) base.OnMouseDown(e); if (this.area = Rectangle.Empty & e.Button = MouseButtons.Left) this.area.Location = new Point(e.X, e.Y); this.pot = new Point(e.X, e.Y); this.index = this.GetSelectedHandle(new Point(e.X, e.Y); this.ToolBars(false); this.SetCursor(); protected override void OnMouseUp(MouseEventArgs e) base.OnMouseUp(e); int left = area.Left; int top = area.Top; int right = area.Right; int bottom = area.Bottom; area.X = Math.Min(left, right); area.Y = Math.Min(top, bottom); area.Width = Math.Abs(left - right); area.Height = Math.Abs(top - bottom); this.ToolBars(true); if (e.Button = MouseButtons.Right) if (this.area = Rectangle.Empty) this.Close(); else this.area = Rectangle.Empty; this.ToolBars(false); this.Invalidate(); this.index = this.GetSelectedHandle(new Point(e.X, e.Y); this.SetCursor(); protected override void OnMouseMove(MouseEventArgs e) base.OnMouseMove(e); if (this.Capture) this.MoveHandleTo(new Point(e.X, e.Y); this.Invalidate(); else this.index = this.GetSelectedHandle(new Point(e.X, e.Y); this.SetCursor(); private void MoveHandleTo(Point point) int left = area.Left; int top = area.Top; int right = area.Right; int bottom = area.Bottom; switch (index) case 0: area.X += point.X - this.pot.X; area.Y += point.Y - pot.Y; this.pot = point; return; case 1: left = point.X; top = point.Y; break; case 2: top = point.Y; break; case 3: right = point.X; top = point.Y; break; case 4: right = point.X; break; case 5: right = point.X; bottom = point.Y; break; case 6: bottom = point.Y; break; case 7: left = point.X; bottom = point.Y; break; case 8: left = point.X; break; this.pot = point; area.X = left; area.Y = top; area.Width = right - left; area.Height = bottom - top; this.tiptext.Text = string.Format(01, Math.Abs(this.area.Width), Math.Abs(this.area.Height); #endregion private void MouseCaptureForm_DoubleClick(object sender, System.EventArgs e) this.SaveToClipBoard(); private void MouseCaptureForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) if (e.KeyData = Keys.Escape & System.Windows.Forms.Button.MouseButtons = MouseButtons.Left) this.area = Rectangle.Empty; this.ToolBars(false); this.Invalidate(); else if (e.KeyData = Keys.Escape) this.Close(); #region 保存图片 private void CaptureImage() if (this.area = Rectangle.Empty) return; /修正截取图片过界提示内存不足BUG int left = area.Left; int right = area.Right; int top = area.Top; int bottom = area.Bottom; if (left Screen.PrimaryScreen.Bounds.Right) right = Screen.PrimaryScreen.Bounds.Right; if (top Screen.PrimaryScreen.Bounds.Bottom) bottom = Screen.PrimaryScreen.Bounds.Bottom; area.X = left; area.Y = top; area.Width = right - left; area.Height = bottom - top; /截取选择区域图片 Bitmap bm = new Bitmap(this.BackgroundImage); this.img = bm.Clone(this.area, System.Drawing.Imaging.PixelFormat.Format32bppArgb); this.Close(); private void SaveToClipBoard() this.CaptureImage(); Clipboard.SetDataObject(this.img, true); private void SaveToFile() SaveFileDi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年福建南平武夷有轨电车有限公司社会招聘考前自测高频考点模拟试题及答案详解参考
- 2025年福建省龙岩市武平县招聘教育卫生干部10人模拟试卷及参考答案详解
- 2025湖南澄迈县农业技术推广中心招聘见习生7人考前自测高频考点模拟试题及一套答案详解
- 2025广东广州市黄埔区人民政府萝岗街道办事处招聘政府聘员2人模拟试卷附答案详解
- 2025广西卫生职业技术学院公开招聘高层次人才22人考前自测高频考点模拟试题参考答案详解
- 2025北京海淀青龙桥社区卫生服务中心面向社会招聘2人考前自测高频考点模拟试题及答案详解(夺冠系列)
- 2025中文版运输合同范本
- 2025河北唐山市市直事业单位招聘工作人员277人模拟试卷(含答案详解)
- 2025年山东法官培训学院公开招聘人员模拟试卷及参考答案详解一套
- 2025贵州剑河县农村集体经济组织选聘职业经理人(总经理)17人考前自测高频考点模拟试题及答案详解1套
- 2025少先队基础知识题库(含答案)
- 人教版九年级物理上-各单元综合测试卷含答案共五套
- 三折页设计课件
- 防诈骗消防安全知识培训课件
- 数据标注课件
- 山河已无恙+吾辈当自强+课件-2025-2026学年高二上学期用《南京照相馆》和731上一节思政课
- 2025至2030年川渝地区成品油行业市场运行现状及未来发展预测报告
- 减肥与能量代谢课件
- 《三借芭蕉扇》课件
- 综合实践课程培训大纲
- 半导体公司内部管理制度
评论
0/150
提交评论