How to decode a Base64 image

3

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

I want to decode and get the original image to save to a directory, is it possible? I tried to use Convert.FromBase64String but I did not succeed, does anyone have any idea what that is?

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

        }
    
asked by anonymous 09.06.2014 / 21:29

2 answers

2

Providing the base64 string of the image would help a lot!

In any case, 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(); 
    }
}
    
11.06.2014 / 15:40
1

It's basically like this:

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 do not know what format it is, but the sample saved in JPEG.

    
09.06.2014 / 21:39