图形学图像处理跨平台【免费下载链接】SkiaSharpSkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Googles Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.项目地址https://gitcode.com/gh_mirrors/sk/SkiaSharp点击查看免费下载导读本文基于 documentation/docfx/releases/1.68.1 中的官方 API 变更记录深入剖析 SkiaSharp 1.68.1 对桌面视图程序集的一次重要重构原SkiaSharp.Views.Desktop.dll被整体移除取而代之的是SkiaSharp.Views.Desktop.Common、SkiaSharp.Views.WPF、SkiaSharp.Views.WindowsForms、SkiaSharp.Views.Gtk3等按 UI 框架独立分发的程序集与 NuGet 包。读完本文你将了解这次拆分的动机、受影响的全部类型、迁移到新包的步骤以及各桌面视图控件SKControl、SKGLControl、SKElement、SKDrawingArea在源码层面的实现原理。变更背景为什么桌面视图要拆分SkiaSharp 1.68.12019 年 11 月 22 日发布历经 6 个 RC 候选版本的长期 servicing 版本在其官方发布说明 documentation/docfx/releases/1.68.1.md 中明确列出了一条 Platform 层面的改动Desktop view packages split; WPF Forms backend added— Splits the desktop view projects and NuGet packages so consumers can pull only the framework they need.在此之前WPF、Windows Forms 等桌面框架的视图控件View/Layer被统一打包进一个SkiaSharp.Views.Desktop程序集。这种一把梭的方式虽然简单却存在明显问题WPF 用户被迫引用 WinForms 依赖如System.Windows.Forms、OpenTK 的 WinForms 控件GTK 用户同样要承担其他框架的传递依赖包体积与依赖面都被无谓放大。1.68.1 的应对方案是将桌面视图项目与 NuGet 包彻底拆分让消费者只拉取自己所用 UI 框架对应的包。这意味着原SkiaSharp.Views.Desktop.dll被废弃移除属于破坏性变更在 1.68.1 API diff 索引 中被标注为 ⚠️ breaking。移除清单SkiaSharp.Views.Desktop.dll 消失的五个类型关联文档 的内容非常聚焦它只记录了一件事命名空间SkiaSharp.Views.Desktop连同其下 5 个公开类型被整体移除移除的类型原先的职责SkiaSharp.Views.Desktop.ExtensionsSystem.Drawing与 SkiaSharp 类型之间的双向转换扩展方法SkiaSharp.Views.Desktop.SKControlWinForms 下的 CPU 光栅化绘图控件继承System.Windows.Forms.ControlSkiaSharp.Views.Desktop.SKGLControlWinForms 下的 OpenGL GPU 硬件加速绘图控件继承OpenTK.GLControlSkiaSharp.Views.Desktop.SKPaintGLSurfaceEventArgsGPU 绘制的PaintSurface事件参数SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgsCPU 绘制的PaintSurface事件参数这里需要特别指出移除指的是从单一程序集中迁走而不是 API 被删除。这些类型在拆分后的新程序集中以新的命名空间/程序集身份继续存在只是你必须按框架引入对应的新包。同时共享的事件参数类型与转换扩展方法被下沉到新的公共基础设施包SkiaSharp.Views.Desktop.Common中命名空间SkiaSharp.Views.Desktop本身在公共包中得以保留。拆分后的新包结构对照 1.68.1 API diff 索引拆分后的桌面相关程序集分为以下几类公共基础设施包SkiaSharp.Views.Desktop.CommonSkiaSharp.Views.Desktop.Common 的 API 变更文档 记录了它承接的核心类型命名空间仍为SkiaSharp.Views.DesktopExtensionsSystem.Drawing与 SkiaSharp 的互转扩展方法包括ToSKPoint/ToDrawingPoint、ToSKRect/ToDrawingRect、ToSKSize/ToDrawingSize、ToSKColor/ToDrawingColor等成对方法。SKPaintSurfaceEventArgsCPU 光栅绘制的统一事件参数构造签名为SKPaintSurfaceEventArgs(SKSurface surface, SKImageInfo info)暴露Surface与Info两个只读属性。SKPaintGLSurfaceEventArgsGPU 绘制的统一事件参数提供两个构造重载SKPaintGLSurfaceEventArgs(SKSurface surface, GRBackendRenderTarget renderTarget, GRSurfaceOrigin origin, SKColorType colorType)推荐SKPaintGLSurfaceEventArgs(SKSurface surface, GRBackendRenderTargetDesc renderTarget)已标记[Obsolete]提示改用前者属性包括Surface、BackendRenderTarget、Origin、ColorType以及过时的RenderTarget建议改用BackendRenderTarget。从工程文件 source/SkiaSharp.Views/SkiaSharp.Views.Desktop.Common/SkiaSharp.Views.Desktop.Common.csproj 可以看到它的PackageReadmeNotice明确写道This is a shared infrastructure package. Most applications should install the package for their UI framework, such asSkiaSharp.Views.WPF,SkiaSharp.Views.WindowsForms, orSkiaSharp.Views.Gtk3.——即这是一个共享基础设施包普通应用不应直接引用而是按自己的 UI 框架安装对应包。按框架分发的视图包NuGet 包 / 程序集核心控件对应源码目录SkiaSharp.Views.WindowsFormsSKControlCPU 光栅、SKGLControlOpenGL GPUsource/SkiaSharp.Views/SkiaSharp.Views.WindowsFormsSkiaSharp.Views.WPFSKElementCPU 光栅、SKGLElementOpenGL GPUsource/SkiaSharp.Views/SkiaSharp.Views.WPFSkiaSharp.Views.Gtk3SKDrawingAreasource/SkiaSharp.Views/SkiaSharp.Views.Gtk3SkiaSharp.Views.Gtk4SKDrawingArea、SKGLAreasource/SkiaSharp.Views/SkiaSharp.Views.Gtk4各包的工程文件通过DefineConstants声明了自己的平台标识如 WPF 包定义__DESKTOP__;__WPF__WinForms 包定义__DESKTOP__;__WINFORMS__GTK3 包定义__DESKTOP__;__GTK__;__GTK3__并统一ProjectReference到SkiaSharp.Views.Desktop.Common与核心绑定库 binding/SkiaSharp/SkiaSharp.csproj由此形成核心库 → 公共视图层 → 框架专属视图层的清晰依赖链。迁移指南升级到 1.68.1 后如何选择包对于 1.68.0 及更早版本的用户升级到 1.68.1 后需要完成两步调整第一步替换 NuGet 包引用。删掉对SkiaSharp.Views.Desktop的引用改为WPF 应用安装SkiaSharp.Views.WPFWindows Forms 应用安装SkiaSharp.Views.WindowsFormsGTK# 3 应用安装SkiaSharp.Views.Gtk3若使用了桌面通用扩展Extensions、事件参数类型它们会作为上述包的传递依赖自动带入SkiaSharp.Views.Desktop.Common第二步核对命名空间与类型归属。SKControl、SKGLControl仍在命名空间SkiaSharp.Views.Desktop下见 SkiaSharp.Views.WindowsForms 的 API 文档因此多数using语句无需改动但程序集从SkiaSharp.Views.Desktop.dll变为SkiaSharp.Views.WindowsForms.dll涉及显式程序集引用如 XAML 中的assembly声明、App.config或*.csproj中的引用时必须更新。例如 WPF 的 XAML 中声明命名空间时需要指定新程序集名xmlns:skiaclr-namespace:SkiaSharp.Views.WPF;assemblySkiaSharp.Views.WPF这在仓库示例 samples/Basic/WPF/SkiaSharpSample/CpuPage.xaml 中有完整体现。同时注意 GPU 事件参数SKPaintGLSurfaceEventArgs的GRBackendRenderTargetDesc构造已过时请改用(SKSurface, GRBackendRenderTarget, GRSurfaceOrigin, SKColorType)重载。源码级剖析拆分后各控件如何工作SKControlWinForms 的 CPU 光栅绘图source/SkiaSharp.Views/SkiaSharp.Views.WindowsForms/SKControl.cs 展示了 WinForms 光栅控件的完整实现控件内部维护一个Format32bppPArgb的Bitmap在OnPaint时先通过LockBits拿到像素缓冲区指针再调用SKSurface.Create(info, data.Scan0, data.Stride)把 Skia 直接画进该内存最后canvas.Flush()后把位图DrawImage到Graphics。它暴露CanvasSize属性受设备像素密度影响可能与控件尺寸不同和PaintSurface事件构造函数中开启DoubleBuffered与ResizeRedraw。// 典型的 SKControl 使用方式见 samples/Basic/WindowsForms/SkiaSharpSample/CpuPage.cs private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e) { var canvas e.Surface.Canvas; var width e.Info.Width; var height e.Info.Height; canvas.Clear(SKColors.White); // ... 使用 SKPaint / SKShader / SKFont 绘制 canvas.DrawText(SkiaSharp, center.X, center.Y font.Size / 3f, SKTextAlign.Center, font, textPaint); }SKGLControlWinForms 的 OpenGL GPU 绘图source/SkiaSharp.Views/SkiaSharp.Views.WindowsForms/SKGLControl.cs 展示了 GPU 路径的典型实现它继承OpenTK.GLControl默认以Rgba8888颜色类型和GRSurfaceOrigin.BottomLeft表面原点创建首次绘制时通过GRGlInterface.Create()GRContext.CreateGl(...)建立 GPU 上下文再从 GL 帧缓冲构建GRGlFramebufferInfo与GRBackendRenderTarget最终SKSurface.Create(grContext, renderTarget, surfaceOrigin, colorType)得到 GPU 表面每次重绘后调用canvas.Flush()与SwapBuffers()完成交换。它还暴露GRContext属性便于直接操作 GPU 资源。Dispose中按逆序释放canvas → surface → renderTarget → grContext。GPU 事件处理示例见 samples/Basic/WPF/SkiaSharpSample/GpuPage.xaml.cs其中通过e.BackendRenderTarget.Width/Height获取实际渲染尺寸并用SKRuntimeEffect构建 shader 绘制动画。SKElementWPF 的 CPU 光栅元素source/SkiaSharp.Views/SkiaSharp.Views.WPF/SKElement.cs 实现了一个FrameworkElement在OnRender中创建WriteableBitmapPbgra32格式DPI 96通过SKSurface.Create(info, bitmap.BackBuffer, bitmap.BackBufferStride)直接绘制到 WPF 的后台缓冲bitmap.AddDirtyRect标记脏区后DrawImage上屏。它特有的IgnorePixelScaling属性控制是否按物理像素绘制默认false时画布按 1 像素对应 1 显示像素高 DPI 下更清晰设为true时按设备无关像素绘制并拉伸性能更好但可能模糊。其扩展类WPFExtensions提供SKColor ↔ System.Windows.Media.Color、BitmapSource ↔ SKBitmap/SKImage/SKPixmap、SKPicture → WriteableBitmap等转换见 source/SkiaSharp.Views/SkiaSharp.Views.WPF/WPFExtensions.cs。SKDrawingAreaGTK3/GTK4 的绘图区域GTK 侧的SKDrawingAreaGtk3 实现、Gtk4 实现以 CairoImageSurface为后备存储将 Skia 表面直接创建在 Cairo 像素数据上绘制完成后做一次SKSwizzle.SwapRedBlue通道交换以匹配 Cairo 的 ARGB 布局再通过cr.SetSourceSurfacecr.Paint呈现。共性设计两种绘制模式与统一事件模型拆分后的桌面视图控件在 API 设计上高度统一均提供两种绘制入口源码 XML 文档注释中明确说明订阅事件挂接PaintSurface事件在处理器中通过e.Surface.Canvas绘制重写虚方法继承控件并重写OnPaintSurface必须调用base.OnPaintSurface(e)否则事件不会触发。两种模式的事件参数也得到统一CPU 光栅控件使用SKPaintSurfaceEventArgs携带SKSurface与SKImageInfoGPU 控件使用SKPaintGLSurfaceEventArgs携带SKSurface、GRBackendRenderTarget、GRSurfaceOrigin与SKColorType并且统一以canvas.Flush()结束一次绘制。结语SkiaSharp 1.68.1 的桌面视图包拆分是一次以按框架按需引用为目标的架构收敛单一SkiaSharp.Views.Desktop.dll退出历史舞台SkiaSharp.Views.Desktop.Common承载共享类型SkiaSharp.Views.WindowsForms、SkiaSharp.Views.WPF、SkiaSharp.Views.Gtk3等各司其职。对开发者而言升级的关键动作只有两个——替换包引用、核对程序集名而 API 层的事件模型与绘制模式完全保留迁移成本集中在项目配置而非代码逻辑。这一拆分模式也延续到了后续版本成为 SkiaSharp 桌面视图分发的长期形态。赞分享图形学图像处理跨平台【免费下载链接】SkiaSharpSkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Googles Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.项目地址https://gitcode.com/gh_mirrors/sk/SkiaSharp点击查看免费下载相关推荐SkiaSharp.Views.Desktop 桌面视图 API 详解SKControl 与 SKGLControl 在 Windows Forms 中的集成实战SkiaSharp.Views.Desktop 桌面视图 API 详解SKControl 与 SKGLControl 在 Windows Forms 中的集成图形学图像处理跨平台解读 SkiaSharp GTK 视图的完整 API 面从 1.60.0 引入的 SKWidget 到现代 SKDrawingArea解读 SkiaSharp GTK 视图的完整 API 面从 1.60.0 引入的 SKWidget 到现代 SKDrawingArea 本篇基于 SkiaSh图形学图像处理跨平台SkiaSharp 1.56.2 API 解析SkiaSharp.Views.Desktop 新增 System.Drawing 与 SkiaSharp 类型互转扩展方法SkiaSharp 1.56.2 API 解析SkiaSharp.Views.Desktop 新增 System.Drawing 与 SkiaSharp 类型图形学图像处理跨平台创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考