.NET 8 中的跨平台 System.Drawing.Bitmap

发布时间:2024-05-16 14:01:53来源:本站阅读(140)

    使用 SkiaSharp(通知中提到的第一个替代方案)进行直接替换。

    它没有许可证限制。

    using (SKCanvas canvas = new SKCanvas(bitmap))
    {
        using (SKPaint textPaint = new SKPaint())
        {
            textPaint.Color = SKColors.Black;
            textPaint.IsAntialias = true;
            textPaint.TextSize = 30;
    
            // 将验证码文本居中绘制到图片上
            float textWidth = textPaint.MeasureText(chkCode);
            float x = (codeW - textWidth) / 2;
            float y = (codeH - textPaint.TextSize) / 2;
            canvas.DrawText(chkCode, x, y, textPaint);
        }
    }

    返回byte[]

    using (SKImage img = SKImage.FromBitmap(bitmap))
    {
        using (SKData p = img.Encode(SKEncodedImageFormat.Png, 100))
        {
            return p.ToArray();
        }
    }

关键字.NET 8