2025-11-18
C#
00

目录

基础知识
完整示例代码
主要功能说明
1. 基本路径操作
2. 曲线绘制
3. 圆弧绘制
4. 路径填充和描边
高级特性
路径效果
路径变换
注意事项
总结

SKPath是SkiaSharp中用于创建复杂图形路径的重要类。它可以用来绘制直线、曲线、圆弧等各种图形,并且支持填充和描边操作。本文将详细介绍SKPath的使用方法和常见应用场景。

基础知识

在WinForms中使用SkiaSharp,首先需要安装以下NuGet包:

  • SkiaSharp
  • SkiaSharp.Views
  • SkiaSharp.Views.WindowsForms

完整示例代码

C#
using SkiaSharp.Views.Desktop; using SkiaSharp; namespace AppSKPath { public partial class Form1 : Form { private SKControl skControl; public Form1() { InitializeComponent(); // 创建SKControl控件 skControl = new SKControl(); skControl.Dock = DockStyle.Fill; skControl.PaintSurface += OnPaintSurface; this.Controls.Add(skControl); } private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { // 获取绘图表面 SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; // 清空画布 canvas.Clear(SKColors.White); // 1. 基础路径绘制 using (var path = new SKPath()) using (var paint = new SKPaint()) { // 设置画笔属性 paint.Style = SKPaintStyle.Stroke; paint.Color = SKColors.Blue; paint.StrokeWidth = 2; paint.IsAntialias= true; // 移动到起始点 path.MoveTo(50, 50); // 绘制直线 path.LineTo(150, 50); path.LineTo(150, 150); path.LineTo(50, 150); // 闭合路径 path.Close(); // 绘制路径 canvas.DrawPath(path, paint); } // 2. 曲线路径示例 using (var path = new SKPath()) using (var paint = new SKPaint()) { paint.Style = SKPaintStyle.Stroke; paint.Color = SKColors.Red; paint.StrokeWidth = 2; paint.IsAntialias = true; // 二次贝塞尔曲线 path.MoveTo(200, 50); path.QuadTo(250, 0, 300, 50); // 三次贝塞尔曲线 path.MoveTo(200, 100); path.CubicTo(225, 75, 275, 125, 300, 100); canvas.DrawPath(path, paint); } // 3. 圆弧路径示例 using (var path = new SKPath()) using (var paint = new SKPaint()) { paint.Style = SKPaintStyle.Stroke; paint.Color = SKColors.Green; paint.StrokeWidth = 2; paint.IsAntialias = true; // 绘制圆弧 var rect = new SKRect(350, 50, 450, 150); path.AddArc(rect, 0, 180); // 从0度开始,绘制180度的圆弧 canvas.DrawPath(path, paint); } // 4. 复合路径示例(填充) using (var path = new SKPath()) using (var paint = new SKPaint()) { paint.Style = SKPaintStyle.Fill; paint.Color = SKColors.Orange; paint.IsAntialias = true; // 创建一个五角星 float centerX = 500; float centerY = 100; float radius = 50; for (int i = 0; i < 5; i++) { double angle = i * 4 * Math.PI / 5 - Math.PI / 2; float x = centerX + radius * (float)Math.Cos(angle); float y = centerY + radius * (float)Math.Sin(angle); if (i == 0) path.MoveTo(x, y); else path.LineTo(x, y); } path.Close(); canvas.DrawPath(path, paint); } } } }

image.png

主要功能说明

1. 基本路径操作

  • MoveTo(float x, float y): 移动到指定坐标点
  • LineTo(float x, float y): 从当前点绘制直线到指定点
  • Close(): 闭合路径(连接最后一个点和起始点)

2. 曲线绘制

  • QuadTo(float x1, float y1, float x2, float y2): 绘制二次贝塞尔曲线
  • CubicTo(float x1, float y1, float x2, float y2, float x3, float y3): 绘制三次贝塞尔曲线

3. 圆弧绘制

  • AddArc(SKRect oval, float startAngle, float sweepAngle): 添加圆弧
  • AddOval(SKRect rect): 添加椭圆
  • AddCircle(float x, float y, float radius): 添加圆形

4. 路径填充和描边

使用SKPaintStyle属性来控制:

  • SKPaintStyle.Stroke: 仅描边
  • SKPaintStyle.Fill: 仅填充
  • SKPaintStyle.StrokeAndFill: 描边和填充

高级特性

路径效果

C#
using (var path = new SKPath()) using (var paint = new SKPaint()) { // 创建路径 path.MoveTo(50, 200); path.LineTo(150, 200); // 设置虚线效果 paint.Style = SKPaintStyle.Stroke; paint.Color = SKColors.Purple; paint.StrokeWidth = 2; paint.PathEffect = SKPathEffect.CreateDash(new float[] { 10, 5 }, 0); canvas.DrawPath(path, paint); }

image.png

路径变换

C#
using (var path = new SKPath()) using (var paint = new SKPaint()) { // 创建路径 path.AddCircle(100, 300, 30); // 应用矩阵变换 var matrix = SKMatrix.CreateScale(1.5f, 0.5f); path.Transform(matrix); paint.Style = SKPaintStyle.Stroke; paint.Color = SKColors.Brown; canvas.DrawPath(path, paint); }

image.png

注意事项

  1. 使用完SKPath和SKPaint对象后要及时释放(使用using语句)
  2. 路径绘制前确保已经设置了起始点(MoveTo)
  3. 复杂路径绘制时注意性能问题
  4. 填充路径时确保路径是闭合的

总结

SKPath是SkiaSharp中非常强大的图形绘制工具,通过合理使用其提供的各种方法,可以实现各种复杂的图形效果。在实际应用中,建议根据具体需求选择合适的路径绘制方式,并注意代码的性能优化。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!