2025-11-11
C#
00

目录

基本直线绘制
高级直线绘制技巧
线条抗锯齿和高级渲染
渐变线条绘制
简单的动画
性能建议
结论

SkiaSharp是一个强大的跨平台2D图形绘制库,源自Google的Skia图形库。本文将深入探讨如何使用SkiaSharp绘制各种类型的直线。

基本直线绘制

C#
override protected void OnPaint(PaintEventArgs e) { var info=new SKImageInfo(ClientSize.Width, ClientSize.Height); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; canvas.Clear(SKColors.White); // 创建画笔(Paint)对象 using (var paint = new SKPaint()) { // 设置线条颜色为蓝色 paint.Color = SKColors.Blue; // 设置线条宽度 paint.StrokeWidth = 5; // 设置绘制模式为描边 paint.Style = SKPaintStyle.Stroke; // 绘制从点(50, 50)到点(200, 200)的直线 canvas.DrawLine(50, 50, 200, 200, paint); } using(var view=new SKGLControl()) { // 从绘制的表面生成快照图像 var image = surface.Snapshot(); // 从图像生成 SKBitmap var bitmap = SKBitmap.FromImage(image); // 将 SKBitmap 转换为 GDI+ Bitmap,并绘制到 PaintEventArgs 的 Graphics 对象上 e.Graphics.DrawImage(bitmap.ToBitmap(), 0, 0); } } }

image.png

高级直线绘制技巧

C#
override protected void OnPaint(PaintEventArgs e) { var info=new SKImageInfo(ClientSize.Width, ClientSize.Height); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; canvas.Clear(SKColors.White); // 创建多种风格的线条 using (var solidLinePaint = new SKPaint()) { // 实线 solidLinePaint.Color = SKColors.Red; solidLinePaint.StrokeWidth = 3; solidLinePaint.Style = SKPaintStyle.Stroke; // 绘制实线 canvas.DrawLine(50, 50, 200, 50, solidLinePaint); } using (var dashedLinePaint = new SKPaint()) { // 虚线 dashedLinePaint.Color = SKColors.Green; dashedLinePaint.StrokeWidth = 3; dashedLinePaint.Style = SKPaintStyle.Stroke; // 创建虚线效果 dashedLinePaint.PathEffect = SKPathEffect.CreateDash( new float[] { 10, 5 }, // 10像素实线,5像素空白 0 // 偏移量 ); // 绘制虚线 canvas.DrawLine(50, 100, 200, 100, dashedLinePaint); } using (var view=new SKGLControl()) { // 从绘制的表面生成快照图像 var image = surface.Snapshot(); // 从图像生成 SKBitmap var bitmap = SKBitmap.FromImage(image); // 将 SKBitmap 转换为 GDI+ Bitmap,并绘制到 PaintEventArgs 的 Graphics 对象上 e.Graphics.DrawImage(bitmap.ToBitmap(), 0, 0); } } }

image.png

线条抗锯齿和高级渲染

C#
override protected void OnPaint(PaintEventArgs e) { var info=new SKImageInfo(ClientSize.Width, ClientSize.Height); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; canvas.Clear(SKColors.White); using (var paint = new SKPaint()) { // 启用抗锯齿 paint.IsAntialias = true; // 设置线帽样式 paint.StrokeCap = SKStrokeCap.Round; // 圆形线帽 paint.Color = SKColors.Orange; paint.StrokeWidth =20; paint.Style = SKPaintStyle.Stroke; // 绘制带圆形线帽的平滑线条 canvas.DrawLine(50, 200, 250, 200, paint); } using (var view=new SKGLControl()) { // 从绘制的表面生成快照图像 var image = surface.Snapshot(); // 从图像生成 SKBitmap var bitmap = SKBitmap.FromImage(image); // 将 SKBitmap 转换为 GDI+ Bitmap,并绘制到 PaintEventArgs 的 Graphics 对象上 e.Graphics.DrawImage(bitmap.ToBitmap(), 0, 0); } } }

image.png

渐变线条绘制

C#
override protected void OnPaint(PaintEventArgs e) { var info=new SKImageInfo(ClientSize.Width, ClientSize.Height); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; canvas.Clear(SKColors.White); // 创建线性渐变 var shader = SKShader.CreateLinearGradient( new SKPoint(50, 250), // 渐变起点 new SKPoint(250, 250), // 渐变终点 new SKColor[] { SKColors.Red, // 起始颜色 SKColors.Blue // 结束颜色 }, null, SKShaderTileMode.Clamp ); using (var paint = new SKPaint()) { paint.Shader = shader; paint.StrokeWidth = 20; paint.Style = SKPaintStyle.Stroke; // 绘制渐变线条 canvas.DrawLine(50, 250, 450, 250, paint); } using (var view=new SKGLControl()) { // 从绘制的表面生成快照图像 var image = surface.Snapshot(); // 从图像生成 SKBitmap var bitmap = SKBitmap.FromImage(image); // 将 SKBitmap 转换为 GDI+ Bitmap,并绘制到 PaintEventArgs 的 Graphics 对象上 e.Graphics.DrawImage(bitmap.ToBitmap(), 0, 0); } } }

image.png

简单的动画

C#
using System; using System.Windows.Forms; using SkiaSharp; using SkiaSharp.Views.Desktop; using Timer = System.Windows.Forms.Timer; namespace AppLine { public partial class Form1 : Form { private float dashOffset = 0; private Timer animationTimer; public Form1() { InitializeComponent(); // 设置双缓冲,减少闪烁 SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); UpdateStyles(); // 初始化动画计时器 animationTimer = new Timer(); animationTimer.Interval = 50; // 50毫秒更新一次 animationTimer.Tick += AnimationTimer_Tick; animationTimer.Start(); } private void AnimationTimer_Tick(object sender, EventArgs e) { // 更新虚线偏移 dashOffset += 2; // 触发重绘 Invalidate(); } protected override void OnPaint(PaintEventArgs e) { var info = new SKImageInfo(ClientSize.Width, ClientSize.Height); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; canvas.Clear(SKColors.White); using (var dashedLinePaint = new SKPaint()) { // 虚线 dashedLinePaint.Color = SKColors.Green; dashedLinePaint.StrokeWidth = 3; dashedLinePaint.Style = SKPaintStyle.Stroke; // 创建动态偏移的虚线效果 dashedLinePaint.PathEffect = SKPathEffect.CreateDash( new float[] { 10, 5 }, // 10像素实线,5像素空白 dashOffset // 动态偏移量 ); // 绘制虚线 canvas.DrawLine(50, 100, 500, 100, dashedLinePaint); } // 从绘制的表面生成快照图像 var image = surface.Snapshot(); // 从图像生成 SKBitmap var bitmap = SKBitmap.FromImage(image); // 将 SKBitmap 转换为 GDI+ Bitmap,并绘制到 PaintEventArgs 的 Graphics 对象上 e.Graphics.DrawImage(bitmap.ToBitmap(), 0, 0); } } // 在窗体关闭时停止计时器 protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); animationTimer.Stop(); } } }

image.png

性能建议

  • 尽量重用SKPaint对象
  • 对于复杂绘制,考虑使用SKPath
  • 启用抗锯齿会略微增加性能开销

结论

SkiaSharp提供了丰富的API来绘制各种风格和类型的直线。通过灵活运用SKPaint和不同的绘制方法,您可以创建出各种精美的线条效果。

希望这篇文章能帮助您深入了解SkiaSharp中的直线绘制技术!

本文作者:技术老小子

本文链接:

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