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>");
        }
    }
}
anderZubi
  • 6,414
  • 5
  • 37
  • 67
Udit Sharma
  • 158
  • 1
  • 2
  • 17
  • check http://stackoverflow.com/questions/8219694/how-can-i-show-an-image-in-webbrowser-control-directly-from-memory – lastr2d2 Dec 25 '13 at 10:03
  • I just solve this problem....'string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); Image1.ImageUrl = "data:image/jpeg;base64," +base64String;' – Udit Sharma Dec 25 '13 at 11:18
  • You should write that as an answer and accept it if the solution solves your problem – lastr2d2 Dec 25 '13 at 11:23

5 Answers5

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 ..

Udit Sharma
  • 158
  • 1
  • 2
  • 17
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>");
lastr2d2
  • 3,604
  • 2
  • 22
  • 37
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

nZeus
  • 2,475
  • 22
  • 21
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);
Pouya Samie
  • 3,718
  • 1
  • 21
  • 34
  • 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 – Udit Sharma Dec 25 '13 at 10:21
  • i just found out you are using base64 , look at my update answer – Pouya Samie Dec 25 '13 at 10:53
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>");
            }
        }
    }
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
Udit Sharma
  • 158
  • 1
  • 2
  • 17