C# 实现截图软件功能

时间:2023-03-09 16:40:08
C# 实现截图软件功能

本文是利用C# 开发截图软件的小例子,以供学习分享使用。

思路:

  1. 截取屏幕图片。
  2. 获取要截取的范围,即左上角,右下角坐标
  3. 填充到PictureBox中。
  4. 笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能

涉及的知识点:

  • MenuStrip:为窗体提供菜单系统。以ToolStripMenuItem为菜单子选项
  • ToolStrip:为 Windows 工具栏对象提供容器。以ToolStripButton【表示包含文本和图像的可选】为工具栏子元素
  • PictureBox:表示用于显示图像的 Windows 图片框控件。不过本文对此空间进行了重写
  • Screen:可用于获取工作屏幕区域
  • Graphics:封装一个 GDI+ 绘图图面。此类不能被继承。此类的CopyFromScreen方法用于获取屏幕图像
  • 鼠标事件:包括MouseDown,MouseMove,MouseUp事件,通过MouseEventArgs中的Location获取鼠标的位置。
  • Clipboard: 提供将数据置于系统剪贴板中以及从中检索数据的方法。此类不能被继承。
  • Cursor:设置鼠标的显示的光标的样式。
  • OnPaint:重绘事件,当控件刷新时响应此事件。

效果图

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

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(, , , , 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 > )
{
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 > )
{
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 - ; i >= ; i--)
{
HLine line = lineHistory[i];
bool flag = false;
foreach (Point p1 in line.PointList)
{
double distance = GetDistance(p0, p1);
if (Math.Abs(distance) < )
{
//需要删除
flag = true;
break;
} }
if (flag)
{
lineHistory.RemoveAt(i);
}
}
//擦除矩形
for (int i = rectHistory.Count - ; i >= ; 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), ) + Math.Pow((p0.Y - p1.Y), ));
} #endregion

以下是源码功能连接,需要的朋友可以自行下载。

源码链接