SkiaSharp 是一个强大的跨平台 2D 图形库,提供了丰富的图像处理能力。本文将深入探讨 SkiaSharp 中图像的加载、保存和基本操作。
C#SkiaSharp SkiaSharp.Views.WindowsForms
C#using System.Drawing;
using System.Windows.Forms;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
namespace AppLoadSave
{
public partial class Form1 : Form
{
private SKBitmap loadedBitmap;
public Form1()
{
InitializeComponent();
}
// 从文件加载图像的方法
private SKBitmap LoadImageFromFile(string filePath)
{
try
{
// 使用 SKBitmap.Decode 方法加载图像
using (var stream = System.IO.File.OpenRead(filePath))
{
return SKBitmap.Decode(stream);
}
}
catch (Exception ex)
{
MessageBox.Show($"图像加载错误: {ex.Message}", "错误"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private Bitmap SKBitmapToSystemBitmap(SKBitmap skBitmap)
{
// 创建一个新的 Bitmap,使用 SKBitmap 的宽度和高度
Bitmap bitmap = new Bitmap(skBitmap.Width, skBitmap.Height,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// 锁定位图的位图数据
System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
bitmap.PixelFormat);
// 复制像素数据
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * bitmap.Height;
byte[] rgbValues = new byte[bytes];
// 从 SKBitmap 获取像素数据
byte[] skPixels = skBitmap.Bytes;
// 转换颜色通道(BGRA to ARGB)
for (int i = 0; i < skPixels.Length; i += 4)
{
rgbValues[i] = skPixels[i + 2]; // B
rgbValues[i + 1] = skPixels[i + 1]; // G
rgbValues[i + 2] = skPixels[i]; // R
rgbValues[i + 3] = skPixels[i + 3]; // A
}
// 将像素数据复制到位图
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bitmap.UnlockBits(bmpData);
return bitmap;
}
private void btnLoad_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "图像文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
openFileDialog.Title = "选择图像文件";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// 加载图像
loadedBitmap = LoadImageFromFile(openFileDialog.FileName);
if (loadedBitmap != null)
{
// 直接从 SKBitmap 创建 Bitmap
Bitmap bitmap = SKBitmapToSystemBitmap(loadedBitmap);
pic.Image = bitmap;
}
}
}
}
}
}

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

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

In the modern Industry 4.0 era, data acquisition systems have become the core infrastructure for enterprise digital transformation. Whether it's sensor monitoring on factory production lines, IoT device status collection, or real-time transaction data processing in financial systems, an efficient and stable data acquisition system is crucial.
However, many C# developers often encounter these pain points when building such systems: high data processing latency, excessive memory usage, improper anomalous data handling, and lack of effective quality control mechanisms. This article will teach you how to build a professional-grade data acquisition system using C# through a complete practical case study, thoroughly solving these technical challenges.
We adopt the Producer-Consumer Pattern combined with Channel asynchronous communication to build a three-layer data processing pipeline:
MarkdownData Generation Layer → Data Processing Layer → Data Aggregation Layer → UI Display Layer

The advantages of this design include:
在现代应用程序开发中,精美的视觉效果往往能提升用户体验。作为.NET平台上强大的跨平台2D图形绘图引擎,SkiaSharp提供了丰富的API来创建各种视觉效果。本文将聚焦于SkiaSharp中线条风格设计的艺术表现,通过详细的代码案例,帮助开发者掌握线宽、线帽和线段连接等关键技术,打造独具特色的视觉体验。
关键词:SkiaSharp教程、C#绘图、线条风格设计、.NET图形开发、跨平台UI、Xamarin绘图、MAUI绘图
SkiaSharp是Google's Skia图形库的.NET绑定,支持多平台(Windows、macOS、iOS、Android等)图形渲染。在开始线条艺术设计前,先了解几个核心概念:
C#// SkiaSharp核心组件
// SKCanvas: 画布,提供绘图表面
// SKPaint: 画笔,定义如何绘制(颜色、线宽、线帽等)
// SKPath: 路径,定义要绘制的几何形状
线宽决定线条的粗细,单位为像素。
C#// 创建不同线宽的画笔
SKPaint thinPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Blue,
StrokeWidth = 2, // 细线
IsAntialias = true // 抗锯齿,使线条更平滑
};
SKPaint mediumPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Red,
StrokeWidth = 8, // 中等线宽
IsAntialias = true
};
SKPaint thickPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Green,
StrokeWidth = 16, // 粗线
IsAntialias = true
};
线帽定义线条两端的形状,有三种基本类型:
C#// 线帽类型
SKPaint buttCapPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.DarkBlue,
StrokeWidth = 12,
StrokeCap = SKStrokeCap.Butt, // 平头线帽,线条在端点处切平
IsAntialias = true
};
SKPaint roundCapPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.DarkRed,
StrokeWidth = 12,
StrokeCap = SKStrokeCap.Round, // 圆头线帽,线条两端为半圆形
IsAntialias = true
};
SKPaint squareCapPaint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.DarkGreen,
StrokeWidth = 12,
StrokeCap = SKStrokeCap.Square, // 方头线帽,线条两端为矩形延伸
IsAntialias = true
};