'Generating Thumbnail C# from base64string

I am trying to generate a thumbnail from the base64string. I am storing the image in a table and am trying to generate a thumbnail from the base64string being stored.

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?



Solution 1:[1]

Mixed few tricks found online. (one from @Plutonix)

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

base64 Input => resize => base64 Output

You get the desired thumbnail with auto aspect ratio.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1