C# 实现截图软件功能实例代码
发布时间:2020-12-15 06:04:20 所属栏目:百科 来源:网络整理
导读:本文是利用C# 开发截图软件的小例子,以供学习分享使用。 思路: 截取屏幕图片。 获取要截取的范围,即左上角,右下角坐标 填充到PictureBox中。 笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能 涉及的知识点: MenuStrip:为窗体提供菜单系统。以ToolStrip
本文是利用C# 开发截图软件的小例子,以供学习分享使用。 思路:
涉及的知识点:
效果图如下【主要实现了截图,保存,复制,画矩形,笔触,荧光笔,橡皮擦等功能】: 保存后图片如下: ------------------------------------------------------------------------------------------------------------------------------- 核心代码如下: 截取屏幕图像: public Bitmap GetScreen() { //获取整个屏幕图像,不包括任务栏 Rectangle ScreenArea = Screen.GetWorkingArea(this); Bitmap bmp = new Bitmap(ScreenArea.Width,ScreenArea.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(0,new Size(ScreenArea.Width,ScreenArea.Height)); } return bmp; } 绘制图形功能: #region 绘制功能 protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); Graphics g = pe.Graphics; DrawHistory(g); //绘制当前线 if (startDraw && this.curLine.PointList != null && this.curLine.PointList.Count > 0) { DrawLine(g,this.curLine); } if (startDraw && this.curRect.Start != null && this.curRect.End != null && this.curRect.Start != this.curRect.End) { DrawRectangle(g,this.curRect); } } public void DrawHistory(Graphics g) { //绘制线历史记录 if (LineHistory != null) { foreach (HLine lh in LineHistory) { if (lh.PointList.Count > 10) { DrawLine(g,lh); } } } //绘制矩形历史记录 if (RectHistory != null) { foreach (HRectangle lh in RectHistory) { if (lh.Start!=null&& lh.End!=null && lh.Start!=lh.End) { DrawRectangle(g,lh); } } } } /// <summary> /// 绘制线 /// </summary> /// <param name="g"></param> /// <param name="line"></param> private void DrawLine(Graphics g,HLine line) { g.SmoothingMode = SmoothingMode.AntiAlias; using (Pen p = new Pen(line.LineColor,line.LineWidth)) { //设置起止点线帽 p.StartCap = LineCap.Round; p.EndCap = LineCap.Round; //设置连续两段的联接样式 p.LineJoin = LineJoin.Round; g.DrawCurve(p,line.PointList.ToArray()); //画平滑曲线 } } /// <summary> /// 绘制矩形 /// </summary> /// <param name="g"></param> /// <param name="rect"></param> private void DrawRectangle(Graphics g,HRectangle rect) { g.SmoothingMode = SmoothingMode.AntiAlias; using (Pen p = new Pen(rect.LineColor,rect.LineWidth)) { //设置起止点线帽 p.StartCap = LineCap.Round; p.EndCap = LineCap.Round; //设置连续两段的联接样式 p.LineJoin = LineJoin.Round; g.DrawRectangle(p,rect.Start.X,rect.Start.Y,rect.End.X - rect.Start.X,rect.End.Y - rect.Start.Y); //画平滑曲线 } } public void Earser(Point p0) { for (int i = lineHistory.Count - 1; i >= 0; i--) { HLine line = lineHistory[i]; bool flag = false; foreach (Point p1 in line.PointList) { double distance = GetDistance(p0,p1); if (Math.Abs(distance) < 6) { //需要删除 flag = true; break; } } if (flag) { lineHistory.RemoveAt(i); } } //擦除矩形 for (int i = rectHistory.Count - 1; i >= 0; i--) { HRectangle rect = rectHistory[i]; if (p0.X>rect.Start.X && p0.X<rect.End.X && p0.Y > rect.Start.Y && p0.Y < rect.End.Y) { rectHistory.RemoveAt(i); } } } /// <summary> /// 获取两点之间的距离 /// </summary> /// <param name="p0"></param> /// <param name="p1"></param> /// <returns></returns> private double GetDistance(Point p0,Point p1) { return Math.Sqrt(Math.Pow((p0.X - p1.X),2) + Math.Pow((p0.Y - p1.Y),2)); } #endregion 以下是源码功能连接,需要的朋友可以自行下载。 源码链接 以上所述是小编给大家介绍的C# 实现截图软件功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |