Sam*_*tte 4 c# gdi azure asp.net-core
对于一个使用 ASP.Net Core 构建的比利时体育社区项目,我使用 C# 动态渲染图像。该图像基于“基本图片”,其中我动态添加文本,然后将图像返回给客户端。
效果很好,我对结果很满意。
但是,当我在我的开发环境(MacOS)上比较客户端生成的图像时,与我的生产环境(Azure 平台中的 Linux VM)相比,该图像呈现得更漂亮。
作为对比,你可以看两张图片,看文字你就会发现差异。在第二张图片中,文本字符串看起来像素化得多。
我可以做些什么来避免这种情况吗?这可能与 GPU 的(不)可用性或类似的情况有关吗?
欢迎任何见解。
(我还将 Nuget 包添加runtime.osx.10.10-x64.CoreCompat.System.Drawing到我的项目中,这是在我的 MacOS 上成功运行所必需的)
供参考:相关代码片段:
public async Task<byte[]> CreateImageAsync(YearReview summary)
{
    var fonts = new PrivateFontCollection();
    fonts.AddFontFile(Path.Combine(_hostingEnvironment.WebRootPath, "fonts/bold.ttf"));
    fonts.AddFontFile(Path.Combine(_hostingEnvironment.WebRootPath, "fonts/light.otf"));
    var boldFont = fonts.Families[0];
    var lightFont = fonts.Families[1];
    var assembly = Assembly.GetAssembly(typeof(YearImageGenerator));
    var resourceName = "CotacolApp.StaticData.year-a.png";
    // Leaving out some variable/constants, irrelevant to the sample
    await using (var stream = assembly.GetManifestResourceStream(resourceName))
    {
        var bitmap = new Bitmap(stream);
        using (var gfx = Graphics.FromImage(bitmap))
        {
                using (Font f = new Font(boldFont, 58, FontStyle.Bold, GraphicsUnit.Pixel))
                {
                    gfx.DrawString($"{summary?.UniqueColsInYear.Number()}", f, Brushes.Black,
                        new RectangleF(0, startHeightYear, yearValuesRightBorder, 100f), rightAlignFormat);
                }
        }
        using (var imgStream = new MemoryStream())
        {
            bitmap.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
            return imgStream.ToArray();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
如果比较图片,请查看图片左侧的黑色数字和右侧的黑色文本。
不要使用System.Drawing. 微软本身在文档中对此提出了警告。它存在于.NET Core中只是为了兼容性
在 .NET 6 及更高版本中,包含此类型的 System.Drawing.Common 包仅在 Windows 操作系统上受支持。在跨平台应用程序中使用此类型会导致编译时警告和运行时异常。有关详细信息,请参阅仅在 Windows 上受支持的 System.Drawing.Common。
链接的文章解释了问题所在,并提供了几种跨平台替代方案,例如ImageSharp和SkiaSharp。
System.Drawing无论如何,它的主要工作是在屏幕上绘制 UI,而不是操作图像。在 Windows 上,它只是 GDI+ 的一个非常薄的包装。等效的跨平台技术是MAUI,目前尚未发布。
ImageSharp 中的等效代码可能是:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.Fonts;
...
//Load the fonts
FontCollection fonts = new FontCollection();
var boldPath = Path.Combine(_hostingEnvironment.WebRootPath, "fonts/bold.ttf");
var lightPath = Path.Combine(_hostingEnvironment.WebRootPath, "fonts/light.ttf");
var boldFont = fonts.Install(boldPath);
var lightFont = fonts.Install(lightPath);
//Load the image
var image = await Image.LoadAsync(stream);
//Draw the text
string yourText = $"{summary?.UniqueColsInYear.Number()}";
Font font = boldFont.CreateFont(58, FontStyle.Bold);
DrawingOptions options = new DrawingOptions()
{
    TextOptions = new TextOptions
    {
        WrapTextWidth = yearValuesRightBorder,
        HorizontalAlignment = HorizontalAlignment.Right
    }
};
var point = PointF(0, startHeightYear);
image.Mutate(x => x.DrawText(options, yourText, font, Color.Black, point));
//Save as PNG to a buffer
using var ms = new MemoryStream();
await image.SaveAsPngAsync(ms);
return ms.ToArray();
Run Code Online (Sandbox Code Playgroud)
您可以查看文档中的入门部分。SaveAsPngAsync 是一种方便的扩展方法。绘制文本显示如何在图像上绘制文本,字体部分显示如何处理字体。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           899 次  |  
        
|   最近记录:  |