2025-11-19
C#
00

目录

文本渲染基础
Nuget 安装包
基本文本绘制
文本特效详解
渐变文字效果
阴影文字效果
描边文字效果
模糊文字效果
路径文字效果
高级文本特效技巧
混合特效组合
性能与注意事项
结语

文本渲染基础

SkiaSharp 提供了丰富的文本渲染和特效能力,让开发者可以创造出极具视觉吸引力的文字效果。本文将深入探讨 SkiaSharp 中文本特效的各种技巧和方法。

Nuget 安装包

C#
SkiaSharp SkiaSharp.Views.WindowsForms

基本文本绘制

C#
public void DrawSimpleText(SKCanvas canvas) { // 创建画笔 using var paint = new SKPaint { TextSize = 50, Color = SKColors.Black, IsAntialias = true, Typeface = SKTypeface.FromFamilyName("Arial") }; // 绘制基础文本 canvas.DrawText("Hello SkiaSharp", 50, 100, paint); }

image.png

文本特效详解

渐变文字效果

C#
public void DrawGradientText(SKCanvas canvas) { // 创建线性渐变 var shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(200, 0), new SKColor[] { SKColors.Blue, SKColors.Purple }, null, SKShaderTileMode.Clamp ); using var paint = new SKPaint { TextSize = 80, Shader = shader, IsAntialias = true }; canvas.DrawText("test font", 50, 200, paint); }

image.png

阴影文字效果

C#
public void DrawShadowText(SKCanvas canvas) { // 加载支持中文的字体 SKTypeface typeface = SKTypeface.FromFamilyName( "Microsoft YaHei", // 或 "SimHei", "SimSun" 等常用中文字体 SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright); // 阴影画笔 using var shadowPaint = new SKPaint { Typeface = typeface, // 设置字体 TextSize = 70, Color = SKColors.Gray.WithAlpha(128), IsAntialias = true }; // 主文字画笔 using var textPaint = new SKPaint { Typeface = typeface, // 设置字体 TextSize = 70, Color = SKColors.DeepPink, IsAntialias = true }; // 绘制阴影(稍微偏移) canvas.DrawText("阴影文字", 52, 302, shadowPaint); // 绘制主文字 canvas.DrawText("阴影文字", 50, 300, textPaint); }

image.png

描边文字效果

C#
public void DrawStrokeText(SKCanvas canvas) { // 加载支持中文的字体 SKTypeface typeface = SKTypeface.FromFamilyName( "Microsoft YaHei", // 或 "SimHei", "SimSun" 等常用中文字体 SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright); using var paint = new SKPaint { Typeface = typeface, TextSize = 80, Color = SKColors.Red, IsAntialias = true, Style = SKPaintStyle.Stroke, StrokeWidth = 3, StrokeJoin = SKStrokeJoin.Round }; // 描边效果 canvas.DrawText("this is test", 50, 300, paint); }

image.png

模糊文字效果

C#
public void DrawBlurText(SKCanvas canvas) { // 加载支持中文的字体 SKTypeface typeface = SKTypeface.FromFamilyName( "Microsoft YaHei", // 或 "SimHei", "SimSun" 等常用中文字体 SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright); using var paint = new SKPaint { Typeface = typeface, TextSize = 70, Color = SKColors.Blue, IsAntialias = true }; // 创建模糊遮罩 using var filter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 3); paint.MaskFilter = filter; canvas.DrawText("模糊文字", 50, 200, paint); }

image.png

路径文字效果

C#
public void DrawPathText(SKCanvas canvas) { // 创建路径 var path = new SKPath(); path.AddArc(new SKRect(50, 50, 300, 800), -90, 180); using var paint = new SKPaint { TextSize = 40, Color = SKColors.Green, IsAntialias = true }; // 沿路径绘制文字 canvas.DrawTextOnPath("this is test", path, 0, 20, paint); }

image.png

高级文本特效技巧

混合特效组合

C#
public void DrawComplexTextEffect(SKCanvas canvas) { using var paint = new SKPaint { TextSize = 60, IsAntialias = true, Color = SKColors.White }; // 创建复合效果:渐变 + 阴影 + 描边 var shader = SKShader.CreateLinearGradient( new SKPoint(0, 0), new SKPoint(200, 0), new SKColor[] { SKColors.Red, SKColors.Orange }, null, SKShaderTileMode.Clamp ); paint.Shader = shader; paint.Style = SKPaintStyle.StrokeAndFill; paint.StrokeWidth = 2; // 添加模糊效果 paint.MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 1); canvas.DrawText("this is test", 50, 250, paint); }

image.png

性能与注意事项

  • 频繁创建和销毁 SKPaint 对象会影响性能,建议重用
  • 使用 IsAntialias = true 提高文字渲染质量
  • 对于复杂特效,考虑预渲染或缓存

结语

SkiaSharp 为文本渲染提供了强大且灵活的工具。通过合理运用渐变、阴影、描边等技术,您可以创造出令人惊叹的文字视觉效果。

提示:实际应用中,根据具体需求和性能要求选择合适的文本特效。

本文作者:技术老小子

本文链接:

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