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

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

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

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

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

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

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

SKPaint 对象SKTextBlobSkiaSharp 提供了强大且灵活的文本渲染能力,通过合理使用其API,可以创建出既精美又高性能的文本图形。要充分利用这些功能,掌握以下几个关键技巧至关重要:首先,灵活运用 SKPaint 对象来控制文本样式和渲染效果;其次,深入理解文本度量的概念,以精确控制文本布局;再者,学会路径转换技术,实现文本与图形间的无缝结合;最后,在整个过程中始终注重性能优化,确保应用流畅运行。通过以上方法,开发者能够极大提升使用SkiaSharp进行文本渲染时的效果和效率。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!