por Cesar Cassiano Schimanco

C# - Base64 para Bitmap (JPG / PNG)

Exemplo simples de como converter string base64 em imagem JPG ou PNG com C#.
Obs. Se precisar fazer o oposto, converter Bitmap para Base64, acesse este artigo: http://cbsa.com.br/post/c---bitmap-jpg-png-para-base64.aspx

No exemplo abaixo estou salvando a imagem no formato JPG na raiz do site. Para salvar como PNG basta trocar a extensão e o ImageFormat.Jpeg por ImageFormat.Png.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //String Base64
        string base64 = "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBAQFBAYFBQYJBgUGCQsIBgYICwwKCgsKCgwQDAwMDAwMEAwODxAPDgwTExQUExMcGxsbHCAgICAgICAgICD/2wBDAQcHBw0MDRgQEBgaFREVGiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICD/wAARCAA0ALADAREAAhEBAxEB/8QAHAAAAgMAAwEAAAAAAAAAAAAAAAcEBQYCAwgB/8QARhAAAQMDAwICBwQFBg8AAAAAAgEDBAUGEQAHEhMhFDEIFSIjQVFhMkJxgRYzUnKhFzZzdIKzGCQlJjU4U1RiY3WDkbLB/8QAFwEBAQEBAAAAAAAAAAAAAAAAAAIDAf/EACARAQADAAICAgMAAAAAAAAAAAABAhEhMQMSE0EyQlH/2gAMAwEAAhEDEQA/APVOgNAaA0BoDQKb0hqrWIFKtsaZUJNOOXWGY77kV02SJswLIkoKOU+i6Bs6A0BoE7vlvHc1hVWmRKPGhPtzWDddWWDpqiifFOPTda7auldRe2HFqFjQGgNAaA0BoDQUl71uVQrQrFZiCByqfEdkMg6iq2pNjlEJBUVx+C67DksdsbubXr9pVTl1hiKw5CfBppIguAioQcl5dRx3vqr1xNLaZmoWXc/cuq1W75No2PCYnTad3rFWmESQo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ymSSFhSYhuvKvI1V1HVeXKOqSYyiY5FkONyVRluq3kY1uSNzQajBbtyl+KcbFFchwiabZjISC6D75mLmRVPPy76CddT9RabuLwsz1fEcuSGFVnqjnFmItLh8iNWXI7ggriAJkjg4FVVVxnQdEumOQqLFL9L4VSjRpMp9IT0+RAjPNkAe4SUkmU+nhyVSHmRiiHjimAVA0lUuWAu1w18wnRIUiDHd4I90pjYP8ETnJMlUMc/eOqWUTJZ0GPgz/W0esx4VdjQoESZCca/yrKkwpBK04r8YZ69B4RNQ9rpKqIQrlF9sdBwnXFE9TRbl8SngKfHkNOW2VYktumUaU4CyoEkDRZfUVtRY6gpyTCCod00Gir8iqy6ZcjEeoSYJncNGiMPtmQusMSEpfVBv9jKPH28skuU7roKesUuTTYl2To1TqCN0SoQm6NHKZJIWFJiG68q8jVXUdV5co6pJjKJjkWQbegNAaA0BoDQGgNAaA0BoOBsMmYGbYkbf6slRFUc/JfhoOegNBWU2lxWKjVZQpyelS0fIiwvEvCMMYDtlEUWUzoLPQdPgofTFroN9IF5AHFOKF80T56DtVEVFRUyi+aaDgUdgmegTYkzhE6SonHCeSY8u2gCjsETZE2Kk1+qVUT2f3floOzQGg//2Q==";

        using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
        {
            //Criar um novo Bitmap baseado na MemoryStream
            using (Bitmap bmp = new Bitmap(ms))
            {
                //Local onde vamos salvar a imagem
                string path = Server.MapPath("~/cbsa.jpg");

                //Salvar a imagem no formato JPG na raiz do site
                bmp.Save(path, ImageFormat.Jpeg);
            }
        }
    }
}

 

Comentários

Carregando comentários

Postar um novo comentário



Processando...