System.Drawing 在 Linux 上的 .NET Core 8.0 上不起作用

问题描述 投票:0回答:1

我之前使用

System.Drawing
命名空间来生成验证码图像(使用位图、图形、画笔、图像转换器等)。 然而,在 Linux 上 6.0 以上的版本中,
System.Drawing
命名空间似乎已被弃用。

该解决方案或任何第三方库(免费的)是否有任何替代解决方案?

任何帮助将不胜感激。 谢谢你


迄今为止与第三方库所做的努力:

  1. Aspose.Drawing - 这个不是免费的。
  2. IronSoftware.Drawing - 它似乎缺少处理和生成验证码所需的所有功能。
  3. SixLabors.ImageSharp - 然而,它在一定程度上可以工作,但在 Linux 上失败。

编辑:@Panagiotis 这是我尝试使用

ImageSharp
但仍然不起作用。

public string CaptchaGenerator(string captchatxt)
{
    var _options = new SixLaborsCaptchaOptions()
    {
        DrawLines = 7,
        TextColor = [Color.Black]
    };

    Image<Rgba32> imgText = new(_options.Width, _options.Height);
    try
    {
        float num = 0f;
        Random random = new();
        byte b = (byte)random.Next(5, 10);
        imgText.Mutate(delegate (IImageProcessingContext ctx)
        {
            ctx.BackgroundColor(Color.Transparent);
        });
        string name = _options.FontFamilies[random.Next(0, _options.FontFamilies.Length)];
        Font font = SystemFonts.CreateFont(name, (int)_options.FontSize, _options.FontStyle);
        for (int j = 0; j < captchatxt.Length; j++)
        {
            char c = captchatxt[j];
            PointF location = new((float)(int)b + num, random.Next(6, 13));
            imgText.Mutate(delegate (IImageProcessingContext ctx)
            {
                ctx.DrawText(c.ToString(), font, Color.Black, location);
            });

            num += TextMeasurer.MeasureSize(c.ToString(), new TextOptions(font)).Width;
        }

        AffineTransformBuilder rotation = GetRotation(_options);
        imgText.Mutate(delegate (IImageProcessingContext ctx)
        {
            ctx.Transform(rotation);
        });
        ushort num2 = (ushort)TextMeasurer.MeasureSize(captchatxt, new TextOptions(font)).Width;
        Image<Rgba32> img = new(num2 + 10 + 5, _options.Height);
        img.Mutate(delegate (IImageProcessingContext ctx)
        {
            ctx.BackgroundColor(_options.BackgroundColor[random.Next(0, _options.BackgroundColor.Length)]);
        });
        img.Mutate(delegate (IImageProcessingContext ctx)
        {
            ctx.DrawImage(imgText, 0.8f);
        });
        img.Mutate(delegate (IImageProcessingContext x)
        {
            x.Resize(_options.Width, _options.Height);
        });
        using MemoryStream memoryStream = new();
        img.Save(memoryStream, _options.Encoder);
        return Convert.ToBase64String(memoryStream.ToArray());
    }
    finally
    {
        if (imgText != null)
        {
            ((IDisposable)imgText).Dispose();
        }
    }
}

private AffineTransformBuilder GetRotation(SixLaborsCaptchaOptions _options)
{
    Random random = new();
    AffineTransformBuilder affineTransformBuilder = new();
    int num = random.Next(10, _options.Width);
    int num2 = random.Next(10, _options.Height);
    PointF pointF = new(num, num2);
    int num3 = random.Next(0, _options.MaxRotationDegrees);
    return affineTransformBuilder.PrependRotationDegrees(num3, pointF);
}
linux asp.net-core captcha system.drawing system.drawing.graphics
1个回答
0
投票

上面的代码运行良好。

© www.soinside.com 2019 - 2024. All rights reserved.