简体   繁体   English

从base64string生成缩略图C#

[英]Generating Thumbnail C# from base64string

I am trying to generate a thumbnail from the base64string . 我试图从base64string生成一个缩略图。 I am storing the image in a table and am trying to generate a thumbnail from the base64string being stored. 我将图像存储在表中,并尝试从存储的base64string生成缩略图。

I am able to generate the thumbnail if I provide a path to the image, but that will not work in my case. 如果我提供图像的路径,我可以生成缩略图,但在我的情况下这不起作用。

This is the working solution of generating a thumbnail from an image path: 这是从图像路径生成缩略图的工作解决方案:

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;
            }
        }
}

Can anyone provide any advice on how to use the base64string instead of the image path the generate thumbnail? 任何人都可以提供有关如何使用base64string而不是生成缩略图的图像路径的任何建议吗?

Assuming, b64 is the base64 string, you can convert it to a byte array and use that to construct the starting image. 假设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;
}

Be sure to dispose of the thumb when you are done with it. 完成后请务必丢弃拇指。

Mixed few tricks found online. 在线发现混合的一些技巧。 (one from @Plutonix) (一个来自@Plutonix)

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

base64 Input => resize => base64 base64输入=> resize => base64

You get the desired thumbnail with auto aspect ratio . 您可以获得具有自动宽高比的所需缩略图。

public static string ResizeBase64ImageString(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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM