locked
Dynamically display images from SQL database RRS feed

  • Question

  • User-1300571109 posted

    I've found several good examples of how to display an image from a database, but I'm looking for an example of how to display an image within an aspx page with other controls.  This code works fine, but only displays the image by itself on the page (item4 is the image data, stored in SQL Server as type=Image):

    Stream strmImage = new MemoryStream(item4.Image_Data.ToArray());
    byte[] buffer = new byte[4096];
    int byteSeq = strmImage.Read(buffer, 0, 4096);
    while (byteSeq > 0)
    {
        Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = strmImage.Read(buffer, 0, 4096);
    }
    

    I've also tried to save the data as a System.Drawing.Image, but like using the Response.Outputstream instruction above, it immediately outputs the image and nothing else on the page.

    System.Drawing.Image myImage = System.Drawing.Image.FromStream(strmImage);
    myImage.Save(Context.Response.OutputStream, ImageFormat.Jpeg);
    

    In simple terms, I want to get the image from the database, stick it in a variable or something like that, then display it in a table.  Any suggestions?

     

     

    Wednesday, October 6, 2010 3:54 PM

Answers

All replies

  • User1992938117 posted

     Have a look at the following:

    • http://www.codeproject.com/aspnet/EasyThumbs.asp
    • http://www.codeproject.com/database/ADOImageDB.asp
    • http://www.codeproject.com/aspnet/PicManager.asp
    • http://www.codeproject.com/useritems/ImageFromDBtoGrid.asp


    Thursday, October 7, 2010 2:49 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 7, 2010 2:55 AM
  • User-1300571109 posted

    Thanks guys!  Here's my final result, if anyone cares.  I modified the example vinay13mar suggested from http://csharpdotnetfreak (which uses a GridView) to use a Repeater control, which I create on the fly.  Here is the .cs code:

    Repeater rpAssetImage = new Repeater();
    rpAssetImage.DataSource = query4; //query4 is a LINQ query
    rpAssetImage.DataBind();
    
    foreach (RepeaterItem item5 in rpAssetImage.Items)
    {
        System.Web.UI.WebControls.Image imgAsset = new System.Web.UI.WebControls.Image();
        imgAsset.ImageUrl = "../../AssetImageHandler.ashx?ID=" + Convert.ToString(item4.ID);
        rpAssetImage.Controls.Add(imgAsset);
    }
    tcPost_Body.Controls.Add(rpAssetImage); //tcPost_Body is a TableCell
    

    And this is what's in AssetImageHandler.ashx:

    public void ProcessRequest (HttpContext context) {
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["MyOwnConnectionString"].ConnectionString;
    
        // Create SQL Command 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select MyBinaryImageData from MyTable where ID =@ID";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
    
        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["MyBinaryImageData"]);
        dReader.Close();
        
        con.Close();
        }
    }



     

     

    Thursday, October 7, 2010 1:31 PM