从base64string生成C#缩略图

4
我正在尝试从base64string生成缩略图。我将图片存储在表中,并尝试从存储的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来生成缩略图而不是使用图片路径的建议?


如果你想要生成位图缩略图,请在将其转换为base64之前进行。从base64字符串开始意味着需要撤销最后几个步骤。 - Ňɏssa Pøngjǣrdenlarp
你能提供一个例子吗? - Code
抱歉,这篇文章让我感到困惑。您有一个base64字符串并希望将其转换回位图并生成缩略图吗? - Ňɏssa Pøngjǣrdenlarp
是的,我能够将FromFile(path)转换为缩略图,但不知道如何将base64转换为缩略图。 - Code
byte[] bytes = Convert.FromBase64String(b64); 将 b64 字符串转换为 byte[],您可以在 memstream 构造函数中使用它。 - Ňɏssa Pøngjǣrdenlarp
是的,但是我该如何将其转换为缩略图呢? - Code
2个回答

5
假设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;
}

在使用完拇指后,请确保妥善处理它。


忘记声明 picOut - Rodolfo
嗯...在我们声明“bmp”变量的那一行上,我得到了“参数无效”的错误。有什么想法吗? - Code
好像实际上没有以base64保存..那就是问题所在。 - Code
这是你的数据 - 如果B64不代表图像数据,它将在那一行失败。如果它不是真正的Base64,它应该在Convert.From那一行失败。 - Ňɏssa Pøngjǣrdenlarp

2

我从网上找到了几个技巧(其中一个来自@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;
}

1
尝试访问scaledImage.RawFormat时出现了空异常。尝试使用JPEG格式,但无法正确加载图像。 - Nick Gallimore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接