首先需要了解的是微软的原生bitmap库对于版本的支持,翻阅微软的文档,发现在.net6以及更高版本,已经在跨平台上不支持使用bitmap了。原文如下:
在 .NET 6 及更高版本中, System.Drawing.Common 包(包括此类型)仅在 Windows 操作系统上受支持。 在跨平台应用中使用此类型会导致编译时警告和运行时异常。 有关详细信息,请参阅 System.Drawing.Common 仅在 Windows 上受支持
https://learn.microsoft.com/zh-cn/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only。
为此,需要实现在Linux或macos上图像支持,必须去找其他替代方案。
方案一:依旧使用bitmap,通过官方,按照如下操作即可
在 .csproj 文件中配置允许不安全的代码调用:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!-- 关键:允许非 Windows 平台使用 System.Drawing -->
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
<ItemGroup>
<!-- 添加包引用 -->
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>
</Project>
或按照官方说明直接修改运行时配置。
修改runtimeconfig.template.json 模板文件:
{
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
最后在linux平台上需要安装依赖库libgdiplus
# Ubuntu/Debian
sudo apt-get install -y libgdiplus
# CentOS
sudo yum install -y libgdiplus
方案二:按照官方建议,迁移到其他库,这里我选择了SkiaSharp
首先通过Nuget获取Library,安装完成,发布,在使用图形的时候会报如下错误
The type initializer for 'SkiaSharp.SKImageInfo' threw an exception.
at SkiaSharp.SKBitmap..ctor(Int32 width, Int32 height, Boolean isOpaque)
at NowAppN.Helper.Utils.ValidateCodeHelper.GenerateCaptchaImageSkia(String code, Int32 width, Int32 height)
at NowAppN.Web.Controller.CaptchaController.GetCaptchaImage()
at lambda_method9(Closure, Object, Object[])
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)Unable to load shared library 'libSkiaSharp' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable:
/home/app/wwwroot/Admin/runtimes/linux-x64/native/libSkiaSharp.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.19/libSkiaSharp.so: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/libSkiaSharp.so: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/runtimes/linux-x64/native/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.19/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/liblibSkiaSharp.so: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/runtimes/linux-x64/native/libSkiaSharp: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.19/libSkiaSharp: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/libSkiaSharp: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/runtimes/linux-x64/native/liblibSkiaSharp: cannot open shared object file: No such file or directory
/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.19/liblibSkiaSharp: cannot open shared object file: No such file or directory
/home/app/wwwroot/Admin/liblibSkiaSharp: cannot open shared object file: No such file or directory
at SkiaSharp.SkiaApi.sk_colortype_get_default_8888()
at SkiaSharp.SKImageInfo..cctor()"
其实如果不着急,仔细阅读github的文档,这个错误可以完全避免掉,还由此浪费了很多时间去找资料解决问题,最终才在SkiaSharp的github文档上找到解决方案。
原文:Because there are multiple distros of Linux, and we cannot possibly support them all, we have a separate NuGet package that will contain the supported binaries for a few distros: SkiaSharp.NativeAssets.Linux. (distros) (more info)
到这里,再次引入上述包,问题解决。
1667

被折叠的 条评论
为什么被折叠?



