在SkiaSharp中,渐变是一种常用的填充效果,主要包括线性渐变(Linear Gradient)和径向渐变(Radial Gradient)。本文将详细介绍这两种渐变的使用方法和实际案例。
线性渐变是沿着一条直线方向进行颜色过渡的渐变效果。在SkiaSharp中,使用SKShader.CreateLinearGradient()方法来创建线性渐变。
C#using SkiaSharp;
using SkiaSharp.Views.Desktop;
namespace AppStyle
{
public partial class Form1 : Form
{
private SKGLControl skControl;
public Form1()
{
InitializeComponent();
// 创建 SKGLControl
skControl = new SKGLControl();
skControl.Dock = DockStyle.Fill;
skControl.PaintSurface += SkControl_PaintSurface;
// 将控件添加到窗体
this.Controls.Add(skControl);
}
private void SkControl_PaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
{
// 获取画布
SKCanvas canvas = e.Surface.Canvas;
// 清除背景
canvas.Clear(SKColors.White);
// 创建画笔
using (var paint = new SKPaint())
{
// 定义渐变的起点和终点
float x1 = 0;
float y1 = 0;
float x2 = skControl.Width;
float y2 = skControl.Height;
// 创建线性渐变
using (var shader = SKShader.CreateLinearGradient(
new SKPoint(x1, y1), // 起点
new SKPoint(x2, y2), // 终点
new SKColor[] { // 渐变颜色数组
SKColors.Blue, // 起始颜色
SKColors.Red // 结束颜色
},
new float[] { 0, 1 }, // 颜色位置
SKShaderTileMode.Clamp)) // 平铺模式
{
// 设置画笔的着色器
paint.Shader = shader;
// 绘制一个填充整个控件的矩形
canvas.DrawRect(0, 0, skControl.Width, skControl.Height, paint);
}
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// 设置窗体的初始大小
this.ClientSize = new System.Drawing.Size(400, 300);
}
}
}

在SkiaSharp中,颜色是通过SKColor结构体来表示的。它支持RGB(红、绿、蓝)和ARGB(透明度、红、绿、蓝)两种主要的颜色表示方式。本文将详细介绍如何在SkiaSharp中设置和使用颜色。
C#// 创建纯红色
SKColor red = new SKColor(255, 0, 0); // R=255, G=0, B=0
// 创建纯绿色
SKColor green = new SKColor(0, 255, 0); // R=0, G=255, B=0
// 创建纯蓝色
SKColor blue = new SKColor(0, 0, 255); // R=0, G=0, B=255
// 创建白色
SKColor white = new SKColor(255, 255, 255); // R=255, G=255, B=255
// 创建黑色
SKColor black = new SKColor(0, 0, 0); // R=0, G=0, B=0

SkiaSharp 是一个强大的 2D 图形引擎,可以用来绘制各种图形,包括贝塞尔曲线。本文将详细介绍如何在 WinForm 应用程序中使用 SkiaSharp 绘制二次贝塞尔曲线和三次贝塞尔曲线。
首先需要通过 NuGet 包管理器安装以下包:
C#using SkiaSharp;
using SkiaSharp.Views.Desktop;
namespace AppBezier
{
public partial class Form1 : Form
{
private SKGLControl skControl;
public Form1()
{
InitializeComponent();
InitializeSkiaSharp();
}
private void InitializeSkiaSharp()
{
// 创建 SKGLControl 控件
skControl = new SKGLControl();
skControl.Dock = DockStyle.Fill;
skControl.PaintSurface += SkControl_PaintSurface;
this.Controls.Add(skControl);
}
private void SkControl_PaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
{
// 获取画布
SKCanvas canvas = e.Surface.Canvas;
// 清空画布
canvas.Clear(SKColors.White);
// 创建画笔
using (var paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Blue,
StrokeWidth = 2,
IsAntialias = true
})
{
// 绘制二次贝塞尔曲线
DrawQuadraticBezier(canvas, paint);
// 修改画笔颜色
paint.Color = SKColors.Red;
// 绘制三次贝塞尔曲线
DrawCubicBezier(canvas, paint);
// 绘制控制点和辅助线
DrawControlPoints(canvas);
}
}
private void DrawQuadraticBezier(SKCanvas canvas, SKPaint paint)
{
// 定义二次贝塞尔曲线的点
var path = new SKPath();
// 起点
float startX = 50;
float startY = 200;
// 控制点
float controlX = 150;
float controlY = 50;
// 终点
float endX = 250;
float endY = 200;
// 移动到起点
path.MoveTo(startX, startY);
// 绘制二次贝塞尔曲线
path.QuadTo(controlX, controlY, endX, endY);
// 绘制路径
canvas.DrawPath(path, paint);
}
private void DrawCubicBezier(SKCanvas canvas, SKPaint paint)
{
// 定义三次贝塞尔曲线的点
var path = new SKPath();
// 起点
float startX = 300;
float startY = 200;
// 第一个控制点
float control1X = 400;
float control1Y = 50;
// 第二个控制点
float control2X = 500;
float control2Y = 350;
// 终点
float endX = 600;
float endY = 200;
// 移动到起点
path.MoveTo(startX, startY);
// 绘制三次贝塞尔曲线
path.CubicTo(control1X, control1Y, control2X, control2Y, endX, endY);
// 绘制路径
canvas.DrawPath(path, paint);
}
private void DrawControlPoints(SKCanvas canvas)
{
// 创建控制点画笔
using (var pointPaint = new SKPaint
{
Style = SKPaintStyle.Fill,
Color = SKColors.Green,
IsAntialias = true
})
using (var linePaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Gray,
StrokeWidth = 1,
PathEffect = SKPathEffect.CreateDash(new float[] { 5, 5 }, 0),
IsAntialias=true
})
{
// 二次贝塞尔曲线的控制点
canvas.DrawCircle(50, 200, 4, pointPaint); // 起点
canvas.DrawCircle(150, 50, 4, pointPaint); // 控制点
canvas.DrawCircle(250, 200, 4, pointPaint); // 终点
// 绘制辅助线
canvas.DrawLine(50, 200, 150, 50, linePaint);
canvas.DrawLine(150, 50, 250, 200, linePaint);
// 三次贝塞尔曲线的控制点
canvas.DrawCircle(300, 200, 4, pointPaint); // 起点
canvas.DrawCircle(400, 50, 4, pointPaint); // 控制点1
canvas.DrawCircle(500, 350, 4, pointPaint); // 控制点2
canvas.DrawCircle(600, 200, 4, pointPaint); // 终点
// 绘制辅助线
canvas.DrawLine(300, 200, 400, 50, linePaint);
canvas.DrawLine(400, 50, 500, 350, linePaint);
canvas.DrawLine(500, 350, 600, 200, linePaint);
}
}
}
}

SKPath是SkiaSharp中用于创建复杂图形路径的重要类。它可以用来绘制直线、曲线、圆弧等各种图形,并且支持填充和描边操作。本文将详细介绍SKPath的使用方法和常见应用场景。
在WinForms中使用SkiaSharp,首先需要安装以下NuGet包:
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);
}
}
}
}

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);
}
}
}
}
