2025-11-19
C#
00

目录

Nuget 安装包
基本文本渲染
简单文本绘制
文本对齐与基线
高级文本渲染技术
自定义字体
文本样式与特效
文本测量与布局
文本度量
多行文本渲染
文本特殊效果
文字路径
性能与最佳实践
跨平台注意事项
结语

SkiaSharp 是一个强大的跨平台 2D 图形库,提供了丰富的文本渲染功能。本文将详细介绍如何使用 SkiaSharp 进行文本渲染,并涵盖从基础到高级的各种技术。

Nuget 安装包

C#
SkiaSharp SkiaSharp.Views.WindowsForms

基本文本渲染

简单文本绘制

C#
using SkiaSharp; using SkiaSharp.Views.Desktop; namespace AppFont { public partial class Form1 : Form { private SKGLControl skControl; public Form1() { InitializeComponent(); skControl=new SKGLControl(); skControl.Dock=DockStyle.Fill; this.Controls.Add(skControl); skControl.PaintSurface += SkControl_PaintSurface; } private void SkControl_PaintSurface(object? sender, SKPaintGLSurfaceEventArgs e) { SKCanvas canvas = e.Surface.Canvas; canvas.Clear(SKColors.White); DrawSimpleText(canvas); } public void DrawSimpleText(SKCanvas canvas) { // 创建画笔 using var paint = new SKPaint { Color = SKColors.Black, // 文字颜色 TextSize = 24, // 文字大小 IsAntialias = true, // 抗锯齿 Typeface = SKTypeface.Default // 默认字体 }; // 绘制文本 canvas.DrawText("Hello, SkiaSharp!", 50, 100, paint); } } }

image.png

文本对齐与基线

C#
using SkiaSharp; using SkiaSharp.Views.Desktop; namespace AppFont { public partial class Form1 : Form { private SKGLControl skControl; public Form1() { InitializeComponent(); skControl=new SKGLControl(); skControl.Dock=DockStyle.Fill; this.Controls.Add(skControl); skControl.PaintSurface += SkControl_PaintSurface; } private void SkControl_PaintSurface(object? sender, SKPaintGLSurfaceEventArgs e) { SKCanvas canvas = e.Surface.Canvas; canvas.Clear(SKColors.White); DrawTextWithAlignment(canvas, e.BackendRenderTarget.Width); } public void DrawTextWithAlignment(SKCanvas canvas, int width) { using var paint = new SKPaint { IsAntialias = true, Color = SKColors.Blue, TextSize = 36, TextAlign = SKTextAlign.Center // 文本居中对齐 }; // 绘制居中文本 canvas.DrawText("Centered Text", width / 2, 200, paint); } } }

image.png

高级文本渲染技术

自定义字体

C#
using SkiaSharp; using SkiaSharp.Views.Desktop; namespace AppFont { public partial class Form1 : Form { private SKGLControl skControl; public Form1() { InitializeComponent(); skControl=new SKGLControl(); skControl.Dock=DockStyle.Fill; this.Controls.Add(skControl); skControl.PaintSurface += SkControl_PaintSurface; } private void SkControl_PaintSurface(object? sender, SKPaintGLSurfaceEventArgs e) { SKCanvas canvas = e.Surface.Canvas; canvas.Clear(SKColors.White); DrawCustomFont(canvas); } public void DrawCustomFont(SKCanvas canvas) { // 从文件加载自定义字体 using var typeface = SKTypeface.FromFile("华文彩云.ttf"); using var paint = new SKPaint { IsAntialias = true, Color = SKColors.DarkGreen, TextSize = 48, Typeface = typeface }; canvas.DrawText("Custom Font", 50, 300, paint); } } }

image.png

文本样式与特效

C#
public void DrawStyledText(SKCanvas canvas) { using var paint = new SKPaint { Color = SKColors.Red, TextSize = 40, IsAntialias = true, // 文字样式 Style = SKPaintStyle.Stroke, // 描边模式 StrokeWidth = 2, // 描边宽度 // 阴影效果 MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 3) }; canvas.DrawText("Styled Text", 50, 400, paint); }

image.png

文本测量与布局

文本度量

C#
public void MeasureText(SKCanvas canvas) { using var paint = new SKPaint { TextSize = 30 }; string text = "Text Measurement"; // 获取文本宽度 float textWidth = paint.MeasureText(text); // 获取文本边界 SKRect bounds = new SKRect(); paint.MeasureText(text, ref bounds); Console.WriteLine($"Text Width: {textWidth}"); Console.WriteLine($"Text Bounds: {bounds}"); }

image.png

多行文本渲染

C#
public void DrawMultilineText(SKCanvas canvas) { string[] lines = { "First line of text", "Second line of text", "Third line of text" }; using var paint = new SKPaint { Color = SKColors.Purple, TextSize = 24 }; float lineHeight = paint.TextSize * 1.5f; for (int i = 0; i < lines.Length; i++) { canvas.DrawText( lines[i], 50, 100 + (i * lineHeight), paint ); } }

image.png

文本特殊效果

文字路径

C#
public void CreateTextPath(SKCanvas canvas) { string text = "Path Text"; using var paint = new SKPaint { Color = SKColors.Orange, TextSize = 60, Style = SKPaintStyle.Stroke, StrokeWidth = 3 }; // 将文字转换为路径 using var textPath = paint.GetTextPath(text, 50, 500); // 绘制文字路径 canvas.DrawPath(textPath, paint); }

image.png

性能与最佳实践

  1. 尽可能重用 SKPaint 对象
  2. 对于静态文本,考虑使用 SKTextBlob
  3. 启用抗锯齿提高文本渲染质量
  4. 选择合适的字体和大小

跨平台注意事项

  • 确保字体在不同平台兼容
  • 使用系统无关的字体(如 OpenType)
  • 处理字体回退机制

结语

SkiaSharp 提供了强大且灵活的文本渲染能力,通过合理使用其API,可以创建出既精美又高性能的文本图形。要充分利用这些功能,掌握以下几个关键技巧至关重要:首先,灵活运用 SKPaint 对象来控制文本样式和渲染效果;其次,深入理解文本度量的概念,以精确控制文本布局;再者,学会路径转换技术,实现文本与图形间的无缝结合;最后,在整个过程中始终注重性能优化,确保应用流畅运行。通过以上方法,开发者能够极大提升使用SkiaSharp进行文本渲染时的效果和效率。

本文作者:技术老小子

本文链接:

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