使用SkiaSharp进行绘制
vs版本:2019
skiasharp版本: 2.88.0
1. 安装SkiaSharp以及SkiaSharp.Views.WPF
在VS中启动程序包管理控制台, 输入如下命令
1. 1 获取SkiaSharp包
PM> Install-Package SkiaSharp -Version 2.88.0-preview.150
1.2 获取WPF支持
PM> Install-Package SkiaSharp.Views.WPF -Version 2.88.0-preview.150
2. 新建WPF程序
1. 在xaml设置设置布局
Grid为两行
在windows标签中, 引入SkiaSharp.Views.WPF命名空间
xmlns:skia="clr-namespace:SkiaSharp.Views.WPF;assembly=SkiaSharp.Views.WPF"
在Grid标签中插入画布
<skia:SKElement Name="MyView" Grid.Row="0" PaintSurface="onCanvasViewPaintSurface"> </skia:SKElement>
3. 实现PaintSurface事件处理程序
private void onCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs e) { SKImageInfo info = e.Info; SKSurface surface = e.Surface; SKCanvas canvas = surface.Canvas; canvas.Clear(); SKPaint paint = new SKPaint { Style = SKPaintStyle.Stroke, Color = Colors.Red.ToSKColor(), StrokeWidth = 25 }; canvas.DrawCircle(info.Width / 2, info.Height / 2, 100, paint); paint.Style = SKPaintStyle.Fill; paint.Color = SKColors.Blue; canvas.DrawCircle(info.Width / 2, info.Height / 2, 100, paint); }
4. 编译生成,效果如下
-------------------- 勿在浮沙筑高台