完成道理
基础截图的功用主要靠相应主窗体的鼠标按下、鼠标挪动、鼠标抬起几个事宜的功用来完成的。截取的图片地区运用“Label”组件来显现,须要从新完成“Label”组件的“Paint”要领。
左键单击最先截图,右键单击作废截图,双击鼠标左键完成截图,将截取的图片保留到Windows剪贴板中。
增加“Label”组件
东西箱》大众组件》双击“Label”组件,修正组件属性:
Name=lbl_CutImage,
AutoSize=False,
BackColor=Transparent,
Text = “”
“Form1_Load”事宜增加代码:
this.lbl_CutImage.Hide();
定义截图功用依靠的基础变量
#region 截图基础变量 /// <summary> /// 用于推断是不是已最先截图,掌握信息框是不是显现。 /// </summary> private bool isCuting; /// <summary> /// 鼠标按下的点 /// </summary> private Point beginPoint; /// <summary> /// 终究肯定的画图基点 /// </summary> private Point endPoint; /// <summary> /// 用于纪录截图显现地区的大小(包含调解块的地区,调解地区边框宽度2px) /// </summary> private Rectangle cutImageRect = new Rectangle(0, 0, 5, 5); #endregion
定义罗列范例:更新UI的形式
/// <summary> /// 更新UI的形式,用于标记哪些须要显现,哪些须要隐蔽; /// </summary> [FlagsAttribute] public enum UpdateUIMode : uint { //值得注重的是,假如要运用组合值,那末就不能用衔接的数字示意,必需是多少级增进! None = 0, ShowTextPro = 1, ShowPenStyle = 2, ShowToolBox = 4, ShowInfoBox = 8, ShowZoomBox = 16, ShowCutImage = 32, HideTextPro = 64, HidePenStyle = 128, HideToolBox = 256, HideInfoBox = 512 }
增加要领:盘算并保留截图的地区框的大小
/// <summary> /// 盘算并保留截图的地区框的大小 /// </summary> private void SaveCutImageSize(Point beginPoint, Point endPoint) { // 保留终究的画图基点,用于截取选中的地区 this.endPoint = beginPoint; // 盘算截取图片的大小 int imgWidth = Math.Abs(endPoint.X - beginPoint.X) + 1; int imgHeight = Math.Abs(endPoint.Y - beginPoint.Y) + 1; int lblWidth = imgWidth + 4; int lblHeight = imgHeight + 4; // 设置截图地区的位置和大小 this.cutImageRect = new Rectangle(beginPoint.X - 2, beginPoint.Y - 2, lblWidth, lblHeight); }
增加要领:实行截图,将选定地区的图片保留到剪贴板
/// <summary> /// 实行截图,将选定地区的图片保留到剪贴板 /// </summary> /// <param name="saveToDisk"> /// 是不是将图片保留到磁盘 /// </param> private void ExecCutImage(bool saveToDisk, bool uploadImage) //bool saveToDisk = false, bool uploadImage = false { // 假如图片猎取地区不可见,则退出保留图片历程 if (!this.lbl_CutImage.Visible) { return; } Rectangle srcRect = new Rectangle(); srcRect.X = this.lbl_CutImage.Location.X + 2; srcRect.Y = this.lbl_CutImage.Location.Y + 2; srcRect.Width = this.lbl_CutImage.Width - 4; srcRect.Height = this.lbl_CutImage.Height - 4; Rectangle destRect = new Rectangle(0, 0, srcRect.Width, srcRect.Height); Bitmap bmp = new Bitmap(srcRect.Width, srcRect.Height); Graphics g = Graphics.FromImage(bmp); g.DrawImage(this.screenImage, destRect, srcRect, GraphicsUnit.Pixel); Clipboard.SetImage(bmp); ExitCutImage(true); }
增加要领:退出截图历程
/// <summary> /// 退出截图历程 /// </summary> private void ExitCutImage(bool hideWindow) // = true { this.lbl_CutImage.Visible = false; this.isCuting = false; if (hideWindow) { this.screenImage.Dispose(); this.Hide(); } }
主窗口鼠标按下事宜处置惩罚顺序
/// <summary> /// 截图窗口鼠标按下事宜处置惩罚顺序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseDown(object sender, MouseEventArgs e) { // 左键单击事宜 if (e.Button == MouseButtons.Left && e.Clicks == 1) { if (!this.lbl_CutImage.Visible) { this.isCuting = true; this.beginPoint = e.Location; this.endPoint = e.Location; SaveCutImageSize(e.Location, e.Location); UpdateCutInfoLabel(UpdateUIMode.ShowCutImage | UpdateUIMode.ShowInfoBox); } } // 左键双击事宜 if (e.Button == MouseButtons.Left && e.Clicks == 2) { if (this.lbl_CutImage.Visible) { ExecCutImage(false, false); } } // 右键单击事宜 if (e.Button == MouseButtons.Right) { ExitCutImage(!this.lbl_CutImage.Visible); } }
主窗口鼠标挪动事宜处置惩罚顺序
/// <summary> /// 截图窗口鼠标挪动事宜处置惩罚顺序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseMove(object sender, MouseEventArgs e) { // 假如截取地区不可见,则退出处置惩罚历程 if (!this.lbl_CutImage.Visible) { UpdateCutInfoLabel(UpdateUIMode.None); return; } Point pntBgn = this.beginPoint; Point pntEnd = e.Location; // 假如是反向拖动,从新设置起始点 if (e.Location.X < this.beginPoint.X && e.Location.Y < this.beginPoint.Y) { pntBgn = e.Location; pntEnd = this.beginPoint; } else { if (e.Location.X < this.beginPoint.X) { pntBgn = new Point(e.Location.X, this.beginPoint.Y); pntEnd = new Point(this.beginPoint.X, e.Location.Y); } else { if (e.Location.Y < this.beginPoint.Y) { pntBgn = new Point(this.beginPoint.X, e.Location.Y); pntEnd = new Point(e.Location.X, this.beginPoint.Y); } } } if (this.isCuting) { SaveCutImageSize(pntBgn, pntEnd); } UpdateCutInfoLabel(UpdateUIMode.None); }
主窗口鼠标抬起事宜处置惩罚顺序
/// <summary> /// 截图窗口鼠标抬起事宜处置惩罚顺序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (this.isCuting) { this.isCuting = false; UpdateCutInfoLabel(UpdateUIMode.None); } } }
截取地区图片绘制
/// <summary> /// 截取地区图片的绘制事宜处置惩罚顺序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lbl_CutImage_Paint(object sender, PaintEventArgs e) { int imgWidth = this.lbl_CutImage.Width - 4; int imgHeight = this.lbl_CutImage.Height - 4; if (imgWidth < 1) { imgWidth = 1; } if (imgHeight < 1) { imgHeight = 1; } // 建立缓存图象,先将要绘制的内容悉数绘制到缓存中,末了再一次性绘制到 Label 上, // 如许能够进步机能,而且能够防备屏幕闪灼的题目 Bitmap bmp_lbl = new Bitmap(this.lbl_CutImage.Width, this.lbl_CutImage.Height); Graphics g = Graphics.FromImage(bmp_lbl); // 将要截取的部份绘制到缓存 Rectangle destRect = new Rectangle(2, 2, imgWidth, imgHeight); Point srcPoint = this.lbl_CutImage.Location; srcPoint.Offset(2, 2); Rectangle srcRect = new Rectangle(srcPoint, new System.Drawing.Size(imgWidth, imgHeight)); g.DrawImage(this.screenImage, destRect, srcRect, GraphicsUnit.Pixel); SolidBrush brush = new SolidBrush(Color.FromArgb(10, 124, 202)); Pen pen = new Pen(brush, 1.0F); //以下部份(边框和调解块)的绘制放在(编辑内容)的背面,是处理绘制编辑内容会掩盖(边框和调解块)的题目 // 绘制边框外的地区,处理会被编辑内容掩盖的题目 // 上边 destRect = new Rectangle(0, 0, this.lbl_CutImage.Width, 2); srcPoint = this.lbl_CutImage.Location; //srcPoint.Offset(2, 2); srcRect = new Rectangle(srcPoint, new System.Drawing.Size(this.lbl_CutImage.Width, 2)); g.DrawImage(this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel); // 下边 destRect = new Rectangle(0, this.lbl_CutImage.Height - 2, this.lbl_CutImage.Width, 2); srcPoint = this.lbl_CutImage.Location; srcPoint.Offset(0, this.lbl_CutImage.Height - 2); srcRect = new Rectangle(srcPoint, new System.Drawing.Size(this.lbl_CutImage.Width, 2)); g.DrawImage(this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel); // 左侧 destRect = new Rectangle(0, 2, 2, this.lbl_CutImage.Height - 4); srcPoint = this.lbl_CutImage.Location; srcPoint.Offset(0, 2); srcRect = new Rectangle(srcPoint, new System.Drawing.Size(2, this.lbl_CutImage.Height - 4)); g.DrawImage(this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel); // 右侧 destRect = new Rectangle(this.lbl_CutImage.Width - 2, 2, 2, this.lbl_CutImage.Height - 4); srcPoint = this.lbl_CutImage.Location; srcPoint.Offset(this.lbl_CutImage.Width - 2, 2); srcRect = new Rectangle(srcPoint, new System.Drawing.Size(2, this.lbl_CutImage.Height - 4)); g.DrawImage(this.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel); // 绘制边框 g.DrawLine(pen, 2, 2, this.lbl_CutImage.Width - 3, 2); g.DrawLine(pen, 2, 2, 2, this.lbl_CutImage.Height - 3); g.DrawLine(pen, this.lbl_CutImage.Width - 3, 2, this.lbl_CutImage.Width - 3, this.lbl_CutImage.Height - 3); g.DrawLine(pen, 2, this.lbl_CutImage.Height - 3, this.lbl_CutImage.Width - 3, this.lbl_CutImage.Height - 3); // 绘制四个角的调解块 g.FillRectangle(brush, 0, 0, 4, 5); g.FillRectangle(brush, this.lbl_CutImage.Width - 4, 0, 4, 5); g.FillRectangle(brush, 0, this.lbl_CutImage.Height - 5, 4, 5); g.FillRectangle(brush, this.lbl_CutImage.Width - 4, this.lbl_CutImage.Height - 5, 4, 5); // 绘制中心的四个调解块 int blockX = this.lbl_CutImage.Width / 2 - 2; int blockY = this.lbl_CutImage.Height / 2 - 2; g.FillRectangle(brush, blockX, 0, 4, 5); g.FillRectangle(brush, 0, blockY, 4, 5); g.FillRectangle(brush, blockX, this.lbl_CutImage.Height - 5, 4, 5); g.FillRectangle(brush, this.lbl_CutImage.Width - 4, blockY, 4, 5); // 绘制到 Label 上 e.Graphics.DrawImage(bmp_lbl, 0, 0); bmp_lbl.Dispose(); }
双击鼠标左键完成截图功用
/// <summary> /// 截取地区图片的鼠标按下事宜处置惩罚顺序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lbl_CutImage_MouseDown(object sender, MouseEventArgs e) { // 左键双击事宜 if (e.Button == MouseButtons.Left && e.Clicks == 2) { if (this.lbl_CutImage.Visible) { ExecCutImage(false, false); } } }
注重:代码都贴完了,别忘了为窗体或组件绑定事宜处置惩罚顺序;
比方:截取地区图片的鼠标按下事宜处置惩罚顺序“lbl_CutImage_MouseDown”,就是“lbl_CutImage”组件的“MouseDown”事宜的处置惩罚顺序,绑定要领参考下图:
到此,基础截图的功用完成已完成,赶快去截取一张图片,粘贴到QQ的谈天窗口看看吧。
以上就是C#开辟实例-订制屏幕截图东西(四)基础截图功用完成代码的细致内容,更多请关注ki4网别的相干文章!