SkiaSharp 是一个强大的 2D 图形引擎,可以用来绘制各种图形。本文将详细介绍如何使用 SkiaSharp 在 WinForm 应用中绘制多边形。
首先需要通过 NuGet 包管理器安装以下包:
C#using SkiaSharp;
using SkiaSharp.Views.Desktop;
using System;
using System.Windows.Forms;
namespace SkiaSharpDemo
{
public partial class Form1 : Form
{
private SKControl skControl;
public Form1()
{
InitializeComponent();
InitializeSkiaSharp();
}
private void InitializeSkiaSharp()
{
// 创建 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);
// 创建画笔
using (SKPaint paint = new SKPaint())
{
paint.Style = SKPaintStyle.Stroke; // 设置为描边模式
paint.Color = SKColors.Blue; // 设置颜色为蓝色
paint.StrokeWidth = 2; // 设置线宽为2像素
paint.IsAntialias = true; // 启用抗锯齿
// 1. 绘制三角形
DrawTriangle(canvas, paint);
// 2. 绘制正方形
paint.Color = SKColors.Red;
DrawSquare(canvas, paint);
// 3. 绘制五边形
paint.Color = SKColors.Green;
DrawPentagon(canvas, paint);
// 4. 绘制六边形
paint.Color = SKColors.Purple;
DrawHexagon(canvas, paint);
// 5. 绘制自定义多边形
paint.Color = SKColors.Orange;
DrawCustomPolygon(canvas, paint);
}
}
// 绘制三角形
private void DrawTriangle(SKCanvas canvas, SKPaint paint)
{
// 创建路径
using (SKPath path = new SKPath())
{
// 定义三角形的三个顶点
path.MoveTo(50, 50); // 起始点
path.LineTo(150, 50); // 第二个点
path.LineTo(100, 150); // 第三个点
path.Close(); // 闭合路径
// 绘制路径
canvas.DrawPath(path, paint);
}
}
// 绘制正方形
private void DrawSquare(SKCanvas canvas, SKPaint paint)
{
using (SKPath path = new SKPath())
{
path.MoveTo(200, 50);
path.LineTo(300, 50);
path.LineTo(300, 150);
path.LineTo(200, 150);
path.Close();
canvas.DrawPath(path, paint);
}
}
// 绘制五边形
private void DrawPentagon(SKCanvas canvas, SKPaint paint)
{
float centerX = 400;
float centerY = 100;
float radius = 50;
int sides = 5;
DrawRegularPolygon(canvas, paint, centerX, centerY, radius, sides);
}
// 绘制六边形
private void DrawHexagon(SKCanvas canvas, SKPaint paint)
{
float centerX = 550;
float centerY = 100;
float radius = 50;
int sides = 6;
DrawRegularPolygon(canvas, paint, centerX, centerY, radius, sides);
}
// 绘制自定义多边形
private void DrawCustomPolygon(SKCanvas canvas, SKPaint paint)
{
using (SKPath path = new SKPath())
{
// 定义多边形的顶点
SKPoint[] points = new SKPoint[]
{
new SKPoint(50, 200), // 第一个点
new SKPoint(150, 200), // 第二个点
new SKPoint(200, 300), // 第三个点
new SKPoint(100, 350), // 第四个点
new SKPoint(25, 300) // 第五个点
};
// 移动到第一个点
path.MoveTo(points[0]);
// 连接所有点
for (int i = 1; i < points.Length; i++)
{
path.LineTo(points[i]);
}
path.Close(); // 闭合路径
canvas.DrawPath(path, paint);
}
}
// 通用方法:绘制正多边形
private void DrawRegularPolygon(SKCanvas canvas, SKPaint paint, float centerX, float centerY, float radius, int sides)
{
using (SKPath path = new SKPath())
{
// 计算每个角的弧度
float angleStep = 2 * (float)Math.PI / sides;
// 移动到第一个点
float firstX = centerX + radius * (float)Math.Cos(0);
float firstY = centerY + radius * (float)Math.Sin(0);
path.MoveTo(firstX, firstY);
// 绘制其余的点
for (int i = 1; i <= sides; i++)
{
float angle = i * angleStep;
float x = centerX + radius * (float)Math.Cos(angle);
float y = centerY + radius * (float)Math.Sin(angle);
path.LineTo(x, y);
}
path.Close();
canvas.DrawPath(path, paint);
}
}
}
}

示例中包含了多种多边形的绘制方法:
使用基本的路径绘制,通过 MoveTo 和 LineTo 方法手动指定顶点。
使用 DrawRegularPolygon 方法,通过数学计算自动生成正多边形的顶点:
通过指定一系列顶点坐标来绘制不规则多边形。
填充效果
要给多边形添加填充效果,只需将画笔的 Style 属性设置为 SKPaintStyle.Fill:
C#paint.Style = SKPaintStyle.Fill;

渐变效果
可以使用 SKShader 创建渐变填充:
C#using (SKShader shader = SKShader.CreateLinearGradient(
new SKPoint(0, 0),
new SKPoint(100, 100),
new[] { SKColors.Red, SKColors.Blue },
null,
SKShaderTileMode.Clamp))
{
paint.Shader = shader;
}
阴影效果
添加阴影效果:
C#paint.ImageFilter = SKImageFilter.CreateDropShadow(
dx: 5,
dy: 5,
sigmaX: 5,
sigmaY: 5,
color: SKColors.Gray
);

SKPath.Close() 确保多边形正确闭合SkiaSharp 提供了强大而灵活的多边形绘制功能。通过组合使用基本的绘图命令,我们可以创建各种复杂的图形。上述示例展示了从简单的三角形到复杂的正多边形的绘制方法,这些代码可以作为基础进行扩展,实现更复杂的图形效果。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!