实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using XBC; using System.IO; namespace XBC { /// <summary> /// 用于截图的类 /// </summary> public partial class frmShow : Form { /// <summary> /// 构造函数 /// </summary> public frmShow() { InitializeComponent(); BackgroundImage = ControlComputer.CaptrueAllScreen(false); backImage = BackgroundImage as Bitmap; Init(); } FromState _State; Point one; Point two; Rectangle rec; Bitmap backImage; Bitmap result; /// <summary> /// 获取得到的图片的Bitmap对象 /// </summary> public Bitmap Result { get { return result; } } #region 方法区 private void Init() { ChangLocation(); Stream s = new MemoryStream(Properties.Resources.a); // byte[] bs =; //for (int i = 0; i < bs.Length - 1; i) { // s.WriteByte(bs[i]); //} Cursor = new Cursor(s); _State = FromState.Start; lblStata.Text = "请单击选择第一个点"; } private void ChangLocation() { pnlInfo.Left = this.Width - pnlInfo.Width; pnlInfo.Top = this.Height - pnlInfo.Height; } private void DrawRectangle() { Graphics g = this.CreateGraphics(); g.DrawRectangle(Pens.Red, rec); } private void ShowInfo() { lblLeft.Text = ""; lblTop.Text = ""; lblWidth.Text = ""; lblHeight.Text = ""; } private void ShowInfo(Rectangle value) { lblLeft.Text = value.Left.ToString(); lblTop.Text = value.Top.ToString(); lblWidth.Text = value.Width.ToString(); lblHeight.Text = value.Height.ToString(); } private void ShowImage(Rectangle temp) { if (temp.Width != 0 && temp.Height != 0) { Graphics g = picBig.CreateGraphics(); g.DrawImage(backImage.Clone(temp, PixelFormat.DontCare), 0, 0, picBig.Width, picBig.Height); } } //刷新区域 private void KeyInvalidate(Rectangle rec) { Rectangle temp = rec; temp.Height ; temp.Width ; Invalidate(temp); } #endregion private void frmShow_SizeChanged(object sender, EventArgs e) { ChangLocation(); } private void pnlInfo_MouseEnter(object sender, EventArgs e) { if (pnlInfo.Top == this.Height - 300) { pnlInfo.Top = 0; } else { pnlInfo.Top = this.Height - 300; } } private void frmShow_MouseMove(object sender, MouseEventArgs e) { if (_State == FromState.Start || _State == FromState.One) { Rectangle temp = new Rectangle(); temp.X = e.Location.X - 10; temp.Y = e.Location.Y - 10; temp.Width = 20; temp.Height = 20; CheckTempRectangle(ref temp); ShowImage(temp); } } //选择图像 private void frmShow_MouseDown(object sender, MouseEventArgs e) { if (_State == FromState.Start) { if (e.Button == MouseButtons.Left) { one = e.Location; lblLeft.Text = one.X.ToString(); lblTop.Text = one.Y.ToString(); lblStata.Text = "已选择第一个点"; _State ; } else if (e.Button == MouseButtons.Right) { this.Close(); } } else if (_State == FromState.One) { if (e.Button == MouseButtons.Left) { two = e.Location; rec.X = Math.Min(one.X, two.X); rec.Y = Math.Min(one.Y, two.Y); rec.Width = Math.Abs(one.X - two.X); rec.Height = Math.Abs(one.Y - two.Y); ShowInfo(rec); lblStata.Text = "已选择了区域"; DrawRectangle(); ShowImage(rec); _State ; } else if (e.Button == MouseButtons.Right) { _State--; lblStata.Text = "已选择第一个点"; } } else if (_State == FromState.Two) { if (e.Button == MouseButtons.Right) { rec.Height ; rec.Width ; Invalidate(rec); _State -= 2; ShowInfo(); lblStata.Text = "请单击选择第一个点"; } } } //改变位置和大小 private void frmShow_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { KeyInvalidate(rec); Update(); if (e.Shift) { rec.Height--; } else { rec.Y--; } DrawRectangle(); } else if (e.KeyCode == Keys.Down) { KeyInvalidate(rec); Update(); if (e.Shift) { rec.Height ; } else { rec.Y ; } DrawRectangle(); } else if (e.KeyCode == Keys.Left) { KeyInvalidate(rec); Update(); if (e.Shift) { rec.Width--; } else { rec.X--; } DrawRectangle(); } else if (e.KeyCode == Keys.Right) { KeyInvalidate(rec); Update(); if (e.Shift) { rec.Width ; } else { rec.X ; } DrawRectangle(); } else if (e.KeyCode == Keys.Enter) { if (rec.Width != 0 && rec.Height != 0) { result = backImage.Clone(rec, PixelFormat.DontCare); //frmSelect objSelect = new frmSelect(); //DialogResult d = objSelect.ShowDialog(); //if (d == DialogResult.Yes) { // Clipboard.SetImage(b); //} //else if (d == DialogResult.No) { // if (saveFileDialog1.ShowDialog() == DialogResult.OK) { // b.Save(saveFileDialog1.FileName,ImageFormat.Bmp); // } //} } this.Close(); this.DialogResult = DialogResult.OK; return; } CheckRectangle(); ShowInfo(rec); ShowImage(rec); } private void CheckRectangle() { if (rec.X < 0) rec.X = 0; if (rec.Y < 0) rec.Y = 0; if (rec.X > 1280) rec.X = 1280; if (rec.Y > 800) rec.Y = 800; if (rec.Width < 0) rec.Width = 0; if (rec.Height < 0) rec.Height = 0; if (rec.Width rec.X > 1280) rec.Width = 1280 - rec.X; if (rec.Height rec.Y > 1280) rec.Height = 800 - rec.Y; } private void CheckTempRectangle(ref Rectangle value) { if (value.X < 0) value.X = 0; if (value.Y < 0) value.Y = 0; if (value.Width value.X > 1280) value.Width = 1280 - value.X; if (value.Height value.Y > 800) value.Height = 800 - value.Y; } } internal enum FromState { /// <summary> /// 表示窗体此时刚进入截图状态 /// </summary> Start, /// <summary> /// 表示现在已选定了一个点 /// </summary> One, /// <summary> /// 表示现在已选定了二个点 /// </summary> Two, } }
标签: windows api
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论