Link an image in C # using a byte array

protected void Button2_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string ext = Path.GetExtension(filename);
            if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".PNG" || ext == ".JPG" || ext == ".JPEG" || ext == ".gif" || ext == ".GIF")
            {

                Stream fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

                Image1.ImageUrl = "data:image/jpeg;base64," +base64String ;
            }
            else
            {
                Response.Write("<script>alert('unsupported format of photo file');</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "');</script>");
        }
    }
}
+4
source share
5 answers
Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

            Image1.ImageUrl = "data:image/jpeg;base64," +base64String ;

This code works well.

+10
source

I assume you are trying to display an image using a base64 string in System.Web.UI.WebControls.Image.

first create a JavaScript client function to set srcyour tag attribute <img />:

function setImageData(imageBase64) {
    document.getElementById("imageId").src = "data:image/png;base64," + imageBase64;
}

then this method is called

Response.Write("<script>setImageData("+ base64String +")</script>");
+2
source

ImageHandler ( IHttpHandler), web.config Button2_Click:

public void Button2_Click(object sender, EventArgs e)
{
    ...
    Image1.ImageUrl = "URL_TO_IMAGE_HANDLER.jpg";
    ...
}

http-: http://www.codeproject.com/Articles/34084/Generic-Image-Handler-Using-IHttpHandler

, Base64,

+1
source

See this . In the tutorial, you can use an array of bytes to get images.

UPDATE: By the way, you should use this function:

string imageDataParsed = imageData.Substring( imageData.IndexOf( ',' ) + 1 );
byte[] imageBytes = Convert.FromBase64String( imageDataParsed );
using ( var imageStream = new MemoryStream( imageBytes, false ) )
{
Bitmap image = new Bitmap( imageStream );
}

Edit You can convert base64string to an image using Image.FromStream. First you need to convert base64string to a stream.

byte[] imageBytes = Convert.FromBase64String(imgBase64String);
MemoryStream ms1 = new MemoryStream(imageBytes);
Image img = Image.FromStream(ms1);
0
source

I just solved this question ... this is updated code.

protected void Button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string ext = Path.GetExtension(filename);
                if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".PNG" || ext == ".JPG" || ext == ".JPEG" || ext == ".gif" || ext == ".GIF")
                {

                    Stream fs = FileUpload1.PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

                    Image1.ImageUrl = "data:image/jpeg;base64," + base64String;
                }
                else
                {
                    Response.Write("<script>alert('unsupported format of photo file');</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        }
    }
0
source

All Articles