C#如何实现截图软件功能

发布时间:2021-05-21 14:08:35 作者:小新
来源:亿速云 阅读:305

这篇文章主要介绍C#如何实现截图软件功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

思路:

  1. 截取屏幕图片。

  2. 获取要截取的范围,即左上角,右下角坐标

  3. 填充到PictureBox中。

  4. 笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能

涉及的知识点:

效果图如下【主要实现了截图,保存,复制,画矩形,笔触,荧光笔,橡皮擦等功能】:

C#如何实现截图软件功能

保存后图片如下:

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, 0, 0, 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#是什么

C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。

以上是“C#如何实现截图软件功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. C# 如何实现截图功能
  2. 如何实现Python Selenium截图功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:微信小程序 sha1如何实现密码加密

下一篇:node.js + socket.io怎么实现点对点随机匹配聊天

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》