根据https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only System.Drawing.Common 不再受非支持Windows 操作系统,除非设置了运行时配置开关。我已经设置了runtimeconfig.template.json并看到了开关:
"runtimeOptions": {
"configProperties": {
"System.Drawing.EnableUnixSupport": true
}
}
Run Code Online (Sandbox Code Playgroud)
bin/Debug/net6.0 中的文件 .runtimeconfig.json 内
但是,当我在 Linux 盒子中运行该应用程序时,dotnet exec app.dll
我仍然得到 PlatformNotSupportedException
我有一些现有的 C# 代码,用于System.Drawing.Common
测量字符串的近似宽度(以像素为单位):
var text = "abc123 this is some long text my dog's name is fido.";
using (var bitmap = new Bitmap(500, 50))
using (var graphics = Graphics.FromImage(bitmap))
{
// Size: 9 Points
using var font = new System.Drawing.Font(familyName: "Times New Roman", emSize: 9f);
var ms = graphics.MeasureString(text, font);
// Output: 'abc123 this is some long text my dog's name is fido.' via System.Drawing: 394.00195 x 22.183594
Console.WriteLine($"'{text}' via System.Drawing: {ms.Width} x {ms.Height}");
}
Run Code Online (Sandbox Code Playgroud)
升级到 后.NET …