4
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>");
        }
    }
}
3
  • check stackoverflow.com/questions/8219694/… Commented Dec 25, 2013 at 10:03
  • I just solve this problem....'string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); Image1.ImageUrl = "data:image/jpeg;base64," +base64String;' Commented Dec 25, 2013 at 11:18
  • You should write that as an answer and accept it if the solution solves your problem Commented Dec 25, 2013 at 11:23

5 Answers 5

11
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 is working well ..

Sign up to request clarification or add additional context in comments.

Comments

2

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

first, create a client JavaScript function to set src attribute of your <img /> tag:

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

then, Invoke that method by

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

Comments

1

I would recommend you to create ImageHandler (inherited from IHttpHandler), register it in the web.config file, and modify your Button2_Click function:

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

You can read about http-handlers here: http://www.codeproject.com/Articles/34084/Generic-Image-Handler-Using-IHttpHandler

This solution is much better than Base64 approach, it's also possible to implement caching support here

Comments

0

See this Tutorial you can use byte array 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 image by Image.FromStream. You will need to convert the base64string to stream first.

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

2 Comments

Image returnImage = Image.FromStream(ms); in this section i am gettin g error--Error 6 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream
i just found out you are using base64 , look at my update answer
0

I have just solved this question..this is the 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>");
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.