ImageVerifierCode 换一换
格式:DOC , 页数:14 ,大小:608.50KB ,
资源ID:5786713      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-5786713.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#实现屏幕截图.doc)为本站会员(11xg27ws)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

C#实现屏幕截图.doc

1、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.UIFrmpublic class Form2 : Form#region 窗体设计器代码private System.Co

2、mponentModel.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();

3、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

4、);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

5、, 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.N

6、ame = “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

7、.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

8、 = 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 = Sy

9、stem.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(

10、this.MouseCaptureForm_KeyDown);this.ResumeLayout(false);#endregionprivate 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.Backgroun

11、dImage = 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

12、.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.Wid

13、th - 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#re

14、gion 获取屏幕/ 引用 APISystem.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

15、 nYSrc, / 源对象的左上角的 X 坐标 System.Int32 dwRop / 光栅的操作值 );private Bitmap GetScreen()/建立屏幕 GraphicsGraphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);/根据屏幕大小建立位图Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grpScreen);/建立位图相关 GraphicsGraphics grpBitmap

16、 = 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);/释放位图对

17、像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.S

18、izeNESW;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

19、.tiptext.Location = new Point(tmp.X, tmp.Y - this.tiptext.Height - 4);/maskGraphicsPath 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

20、.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);

21、e.Graphics.FillRegion(brush, region);if (this.area != Rectangle.Empty)for (int i = 1; i / 获取选择的矩形区域/ / 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.Ab

22、s(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

23、.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 = n

24、ew 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 Screen.PrimaryScreen.Bounds.Right)right = Screen.PrimaryScreen.Bounds.Right;if (top Screen.PrimaryScreen.Bounds.Bottom)bottom = Screen.PrimaryScreen.Bounds.Botto

25、m;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.SetDataO

26、bject(this.img, true);private void SaveToFile()SaveFileDialog saveImageDialog = new SaveFileDialog();saveImageDialog.Title = “保存图片.“;saveImageDialog.Filter = “png|*.png|jpeg|*.jpg|bmp|*.bmp|gif|*.gif“;if (saveImageDialog.ShowDialog() = DialogResult.OK)string fileName = saveImageDialog.FileName.ToStr

27、ing();if (!string.IsNullOrEmpty(fileName)string fileExtName = fileName.Substring(fileName.LastIndexOf(“.“) + 1).ToString();System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Png;if (!string.IsNullOrEmpty(fileExtName)switch (fileExtName.ToLower()case “jpg“:imgformat = S

28、ystem.Drawing.Imaging.ImageFormat.Jpeg;break;case “bmp“:imgformat = System.Drawing.Imaging.ImageFormat.Bmp;break;case “gif“:imgformat = System.Drawing.Imaging.ImageFormat.Gif;break;case “png“:imgformat = System.Drawing.Imaging.ImageFormat.Png;break;default:break;trythis.CaptureImage();this.img.Save(fileName, imgformat);catchMessageBox.Show(“保存失败!“);#endregionpublic delegate void Finised(Image image);/ / 截图操作完成事件/ public event Finised OnFinised;protected override void OnClosing(CancelEventArgs e)base.OnClosing(e);if (this.OnFinised != null)this.OnFinised(this.img);private Button btnCancel;

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报