How to decode an image in Base64

Asked

Viewed 2,101 times

3

I’m trying to decode an image, the string that comes as a parameter in the Image attribute of the post object looks like this: "data:image/png;Base64," and a lot of letters, numbers, and symbols on the right,

I want to decode and get the original image to save in a directory, is it possible? I tried to use Convert.Frombase64string however I was unsuccessful, someone has any idea what it is?

public void NewPost(Post post)
        {
           post.Image = post.Image.Replace("data:image/png;base64,", "");
           byte[] byteObject = Convert.FromBase64String(post.Image);
           .
           .
           .
           .

        }
  • 1

    I don’t get it: in theory when you encode an image for Base64, it’s its entire content, not the URL. That’s right?

  • Gypsy, yes, you are correct, it was my mistake to put the attribute as imageurl, I will edit, vlw

  • @Ukyron has how to make available the value of this Base64?

2 answers

2


Providing the image string Base64 would help a lot!

Either way, it follows:

[TestMethod]
public void como_decodificar_uma_imagem_em_base64()
{
    var img = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwABEI8AARCPAbZ2bGgAAAAadEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My41LjExR/NCNwAAAnNJREFUWEfd10+IjWEUx/F7zQxmNURYGDUayYrmFruZkp3cbFlMSWlKycbCKDaKkVE2kmxsRlJMycKfptjZKbFQIn9SLFiR/76/d87lmdMzz31n7nMtnPrk3vOc55zXe9/7vncqZaNWq1WxCftxDjdwz+i1clpTTdW2tR4068UJvMSvklSrPb3WZu7B5h6cxRfEhpShveqxxNqWCzYM4hViTedDvQatfTooHMZX25iTeg7bmHhQsAs/bEM7qPduGzczWBjAZytsJ80YsLHTQaILj6zgX9CsLhtfHMBIsFjGVex0ubkaaQzXDeZpsJDyDYegPRssN1+aWdUBbA6SKe+wtThqgtetHoBsUaPDLhnzADPuarzvwHKsxRAO4Ca+I9YjZlSNJlzSm8Qim9s0qO3HXdvbzIQ2TLmk9xj91r9UUN8JPaBi/UJTKtbTLLYY+oC69S+C94uxEevQY+k/QW4VPiHWr+G+CnWKY4veTxxHhw3wF+FbXMZQcQQEr6/Z2mwmVTTmks3cwjKkvgVjdgBHXd47paIdLlnGC5x2OW87DrqcV9cBdONjkMxFpz91BjSzu/FZjQcLuTzDJZcLnSmGK3izArnPwns8cbkGzVpp46eDxJ6gIAc9dvXNia3ttbF/g6RcCIra5SJsqgsWdAdrdmtuxRV02rh4UNAXbMhFH8VJLLAxswdFo7YpFz33t1n7dFC4Brm+DXqI7cNCa58OCnV7fYhYs9BzvEb461mv3+A2jkFhnUsExfqfp36UasAd1FF8jvyrC3apSV9YqWDzeujoY4N1Ro6gz8rzB831m/A8rkOPU12t+gNltZX8z1Gp/AarUlJmUIF4ggAAAABJRU5ErkJggg==";

    var decoder = new DecodificarImagem();

    var imgStrm = decoder.DecodeBase64(img);

    imgStrm.Should().NotBeEmpty();
}

And the class:

public class DecodificarImagem
{
    public byte[] DecodeBase64(string img)
    {
        img = FixBase64ForImage(img);
        return Convert.FromBase64String(img);
    }

    public string FixBase64ForImage(string Image) { 
        System.Text.StringBuilder sbText = new System.Text.StringBuilder(Image, Image.Length); 
        sbText.Replace("\r\n", String.Empty); 
        sbText.Replace(" ", "+"); 
        return sbText.ToString(); 
    }
}

0

Basically that’s how:

public void NewPost(Post post)
{
    post.Image = post.Image.Replace("data:image/png;base64,", "");
    byte[] bytes = Convert.FromBase64String(post.Image);

    Image image;
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        image = Image.FromStream(ms);
    }

    image.Save(caminhoDoDiscoPraSalvar, System.Drawing.Imaging.ImageFormat.Jpeg);
}

I don’t know what format it is, but the example saves in JPEG.

  • Gypsy, at the time of the conversion, I’m trying to use the Convert.Frombase64string method, only it gives an error like this: "Invalid length for a Base-64 char array or string." but if I copy the string and paste these decoders that have on the internet it decofifica perfectly for me, have idea?

  • I think my answer is really wrong. If you want to edit the way it works, feel free.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.