本文转自:http://www.tuicool.com/wx/iuaINjy
目前.Net Core上没有System.Drawing这个类库,想要在.Net Core上处理图片得另辟蹊径。
微软给出了将来取代System.Drawing的方案,偏向于使用一个单独的服务端进行各种图片处理
https://github.com/dotnet/corefx/issues/2020
https://github.com/imazen/Graphics-vNext
但目前仍然没有一个可用的实现。
下面我介绍一些目前确实可用于代替System.Drawing的类库,包括我发布的 ZKWeb.System.Drawing
ImageProcessor
地址: https://github.com/JimBobSquarePants/ImageProcessor/
从3.0开始支持了.Net Core。
支持的很全面,如果只用于转换缩放图片,或手动处理像素的话可以最优先考虑这个类库。
但是不支持描画验证码等描画类的功能,在将来会支持,可见 https://github.com/JimBobSquarePants/ImageProcessor/issues/264 。
因为作者尚未把3.0发布到nuget,安装需要添加myget的源。
如何添加myget的源可以参考 https://www.myget.org/nuget 。
添加后使用nuget安装 ImageProcessorCore
即可。
CoreCompat
地址: https://github.com/CoreCompat/CoreCompat
这个类库使用了mono的System.Drawing实现,只要安装了之前使用System.Drawing的代码完全不用修改。
也支持描画验证码等描画类的功能。
如果需要linux或osx支持,可以安装 runtime.linux.CoreCompat.System.Drawing
和 runtime.osx.10.10-x64.CoreCompat.System.Drawing
。
ZKWeb.System.Drawing
地址: https://github.com/zkweb-framework/zkweb.system.drawing
这个类库是我在使用CoreCompat后感到不满意而重新创建的一个类库,也是从mono的System.Drawing修改得来。
这个类库和CoreCompat的不同点如下
- 没有使用强名称,CoreCompat为了让程序集名称一样使用了一个伪造的签名,但是导致Asp.Net和Owin等会检查签名的旧项目启动失败
- CoreCompat的项目如果直接下载编译会出现100多个错误,大多是类型找不到的错误,我也不知道作者是怎么编译过去的 这个项目从mono 4.6.1.13复制了所有需要的文件并修改,直接下载编译就可以通过
- 可以使用dotnet test跑单元测试,目前通过率约为80%
- 实际在linux上测试过并且给出了各个发行版安装libgdiplus的命令,目前已测试
- Ubuntu Server 16.04 LTS 64bit
- Fedora 24 64bit
- CentOS 7.2 64bit
- 不引用System.Drawing.Primitive,因为System.Drawing.Primitive在.Net Framework下同时引用了原来的System.Drawing,有可能导致编译时类型冲突(实测只有警告)
ZKWeb.System.Drawing这个类库在有更好的代替方案之前将会一直维护,如果使用中遇到问题或错误欢迎到项目地址提出issue。