十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍C#如何实现截图软件功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

思路:
截取屏幕图片。
获取要截取的范围,即左上角,右下角坐标
填充到PictureBox中。
笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能
涉及的知识点:
MenuStrip:为窗体提供菜单系统。以ToolStripMenuItem为菜单子选项
ToolStrip:为 Windows 工具栏对象提供容器。以ToolStripButton【表示包含文本和图像的可选】为工具栏子元素
PictureBox:表示用于显示图像的 Windows 图片框控件。不过本文对此空间进行了重写
Screen:可用于获取工作屏幕区域
Graphics:封装一个 GDI+ 绘图图面。此类不能被继承。此类的CopyFromScreen方法用于获取屏幕图像
鼠标事件:包括MouseDown,MouseMove,MouseUp事件,通过MouseEventArgs中的Location获取鼠标的位置。
Clipboard: 提供将数据置于系统剪贴板中以及从中检索数据的方法。此类不能被继承。
Cursor:设置鼠标的显示的光标的样式。
OnPaint:重绘事件,当控件刷新时响应此事件。
效果图如下【主要实现了截图,保存,复制,画矩形,笔触,荧光笔,橡皮擦等功能】:

保存后图片如下:

-------------------------------------------------------------------------------------------------------------------------------
核心代码如下:
截取屏幕图像:
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);
}
}
}
}
///
/// 绘制线
///
///
///
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()); //画平滑曲线
}
}
///
/// 绘制矩形
///
///
///
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.Start.Y && p0.Y < rect.End.Y) {
rectHistory.RemoveAt(i);
}
}
}
///
/// 获取两点之间的距离
///
///
///
///
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#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。
以上是“C#如何实现截图软件功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。