0

I am going to show images from database as the user input image id. and I have to show it in picture box below it.

I used this code to convert base64string to image

using System;
using System.Collections.Generic;
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)
    {
        if (Request.QueryString["ImageID"] != null)
        {
            string ImgData = Request.QueryString["ImageID"].ToString();
            Byte[] bytes = Convert.FromBase64String(ImgData);
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "image/jpg";
            Response.AddHeader("content-disposition", "attachment;");
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }
}

and my main webpage sourcecode is

protected void Button1_Click(object sender, EventArgs e)
    {
        NpgsqlCommand cmd = null;
        string selPhoto = @"select * from photodetails where photoid=@photoid";
        System.Drawing.Image newImage;
        string tempfilename=@"C:\Users\Public\temp_image";
        try
        {
            cmd = new NpgsqlCommand(selPhoto, con);

            cmd.Parameters.Add("@photoid",Convert.ToInt16(txtpid.Text));
            if (con.State == ConnectionState.Open)
                con.Close();
            con.Open();

            NpgsqlDataReader drphoto = cmd.ExecuteReader();
            while (drphoto.Read())
            {
                //System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
                //System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                Byte[] bytes = (byte[])drphoto["photo_bytearr"]; //br.ReadBytes((Int32)fs.Length);
                string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                img1.ImageUrl = @"http://localhost:29450/SampleWeb/showimg.aspx?ImageData=" + Convert.ToBase64String(bytes);                
            }

        }
        catch (Exception ex)
        {

        }
    }

but still it doesn't showing image?

Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
  • Actually, there are dozens of related posts. Just give the site a search. – emerson.marini Jul 26 '13 at 12:55
  • 1
    Images will usually be too big to save in the query string, which has a max length (depending on the browser, but IE has a max of 2000, FF 64K), so you need to come up with a better design. – Klaus Byskov Pedersen Jul 26 '13 at 12:56
  • Do a google search for "How to display an image in ASP.NET" Somewhere among the 40 million responses I'm sure you'll find the answer. Probably in the first 2-3 links. – Pete Jul 26 '13 at 12:58
  • You should implement a handler (`ashx`), not a page (`aspx`). – Myrtle Jul 26 '13 at 13:05

1 Answers1

2

The following line will not work.

img1.ImageUrl = @"http://localhost:29450/SampleWeb/showimg.aspx?ImageData=" + Convert.ToBase64String(bytes);

Rather pass an id of the image, and look that up, vs trying to pass the binarydata via the url.

img1.ImageUrl = @"http://localhost:29450/SampleWeb/showimg.aspx?ImageId=" + imageId; 

Note: Internet Explorer will only allow to have 2,048 characters in your url.