从 base64string 生成缩略图 C#

Generating Thumbnail C# from base64string

我正在尝试从 base64string 生成缩略图。我将图像存储在 table 中,并尝试从存储的 base64string 中生成缩略图。

如果我提供图像的路径,我可以生成缩略图,但这对我来说不起作用。

这是从图像路径生成缩略图的工作解决方案:

protected void GenerateThumbnail(object sender, EventArgs e)
{
        string path = Server.MapPath("../src/img/myImage.png");
        System.Drawing.Image image = System.Drawing.Image.FromFile(path);
        using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                thumbnail.Save(memoryStream, ImageFormat.Png);
                Byte[] bytes = new Byte[memoryStream.Length];
                memoryStream.Position = 0;
                memoryStream.Read(bytes, 0, (int)bytes.Length);
                string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                Image2.ImageUrl = "data:image/png;base64," + base64String;
                Image2.Visible = true;
            }
        }
}

任何人都可以就如何使用 base64string 而不是生成缩略图的图像路径提供任何建议吗?

假设 b64 是 base64 字符串,您可以将其转换为字节数组并使用它来构造起始图像。

byte[] bytes = Convert.FromBase64String(b64);
using (MemoryStream ms = new MemoryStream(bytes))
{
    Bitmap thumb = new Bitmap(100, 100);
    using (Image bmp = Image.FromStream(ms))
    {
        using (Graphics g = Graphics.FromImage(thumb))
        { 
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.DrawImage(bmp, 0, 0, 100, 100);
        }
    }
    // a picturebox to show/test the result
    picOut.Image = thumb;
}

一定要在用完拇指后将其丢弃。

在网上找到的混合技巧。 (一个来自@Plutonix)

string ThumbNailBase64 = ResizeBase64Image(YourBase64String,200, 300);

base64 输入 => 调整大小 => base64 输出

您获得了所需的自动纵横比缩略图。

public static string ResizeBase64Image(string Base64String, int desiredWidth, int desiredHeight)
{
    Base64String = Base64String.Replace("data:image/png;base64,", "");

    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(Base64String);

    using (MemoryStream ms = new MemoryStream(imageBytes))
    {                
        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        Image image = Image.FromStream(ms, true);

        var imag = ScaleImage(image, desiredWidth, desiredHeight);

        using (MemoryStream ms1 = new MemoryStream())
        {
            //First Convert Image to byte[]
            imag.Save(ms1, imag.RawFormat);
            byte[] imageBytes1 = ms1.ToArray();

            //Then Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes1);
            return "data:image/png;base64,"+base64String;
        }
    }
}

public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);

    var newImage = new Bitmap(newWidth, newHeight);

    using (var graphics = Graphics.FromImage(newImage))
        graphics.DrawImage(image, 0, 0, newWidth, newHeight);

    return newImage;
}