Click here to Skip to main content
15,889,776 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi guys,

I was working on Image Handler in asp.net,

where i have stored images in sql db, n fetching the imgs from db,

to view those imgs i m using ImgHndlr.ashx using Sessions

So, i want to reuse image handler instead of creating img hndlr for every img...

MY CODE:

Session["viewImg"] = dt.Rows[0]["EmpPicture"];
imgEmp.ImageUrl = "~/EmpImgHndlr.ashx";
Session["viewImg"] = dt.Rows[0]["IqamaPicture"];
imgI.ImageUrl = "~/EmpImgHndlr.ashx";
Session["viewImg"] = dt.Rows[0]["License"];
imgL.ImageUrl = "~/EmpImgHndlr.ashx";
Session["viewImg"] = dt.Rows[0]["Pp1"];
imgP1.ImageUrl = "~/EmpImgHndlr.ashx";
Session["viewImg"] = dt.Rows[0]["Pp2"];
imgP2.ImageUrl = "~/EmpImgHndlr.ashx";





ImgHndlr.ashx CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Web.SessionState;
using DataAccessLayer;

namespace RASAKTMS
{
    /// <summary>
    /// Summary description for EmpImgHndlr
    /// </summary>
    public class EmpImgHndlr : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Session["viewImg"] != null)
            {
                Byte[] imgdata = (Byte[])(context.Session["viewImg"]);
                context.Response.BinaryWrite(imgdata);
                context.Response.End();
            }
        }

       

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


Please guys, help me to sort out this issue.

or

is it possible or not...

THANKS
Posted
Updated 1-Mar-14 20:04pm
v2

Okay, it seems that you've used DataTable and you're getting Image URL from that.
Let me show you one example, where I have created one custom Listview, in which I'm retrieving images from the database using Handler.
.aspx :
ASP.NET
<asp:listview id="ListView1" runat="server" datakeynames="ImgID" xmlns:asp="#unknown">
                onitemediting="ListView1_ItemEditing"
                onitemdeleting="ListView1_ItemDeleting" 
                onitemupdating="ListView1_ItemUpdating" 
                onitemcommand="ListView1_ItemCommand">
                <itemtemplate>
                    <table style="width:auto">
                        <tr>
                            <td>
                                <asp:checkbox id="CheckBox1" runat="server" />
                            </td>
                            <td>
                                <asp:image id="Image1" runat="server" imageurl="<%# "ImageHandler.ashx?ImageId=" + Eval("ImgID") %>" cssclass="Image" />
                            </td>
                            <td>
                                 <asp:label id="NameLabel" runat="server" text="<%# Eval("Name") %>" style="color:#FFF; font-family:Segoe UI Semibold;" /> <br />
                                 <asp:label id="CreatorLabel" runat="server" text="<%# Eval("Creator") %>" style="color:#ECECEC; font-family:Segoe UI; font-size:small;" /> <br />
                                 <asp:linkbutton id="EditBtn" runat="server" text="Edit" commandname="Edit" cssclass="Btn" />
                                 <asp:linkbutton id="DeleteBtn" runat="server" text="Delete" commandname="Delete" cssclass="Btn" />
                            </td>
                        </tr>
                    </table>
                </itemtemplate>

            </asp:listview>

.aspx.cs :
public void BindGrid()
{
    con.Open();

    SqlCommand cmd = new SqlCommand("SELECT * FROM GamesList", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    ListView1.DataSource = dt;
    ListView1.DataBind();

    con.Close();
}

Handler :
C#
public class ImageHandler : IHttpHandler
   {
       public void ProcessRequest(HttpContext context)
       {
           SqlConnection con = new SqlConnection(@"Data Source=rohit\sqlexpress;Initial Catalog=Games;Integrated Security=True");

           try
           {
               con.Open();

               string ImageId = context.Request.QueryString["ImageId"];

               SqlCommand cmd = new SqlCommand("select AlbumArt from GamesList where ImgID = " + ImageId, con);

               cmd.Connection = con;
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();
               da.Fill(dt);

               foreach (DataRow dr in dt.Rows)
               {
                   byte[] photu = dr[0] as byte[];
                   context.Response.BinaryWrite(photu);
                   context.Response.End();
               }
           }

           catch (Exception ex)
           {
               ex.ToString();
               context.Response.Write(ex.ToString());
           }
           finally
           {
               con.Close();
           }
       }

       public bool IsReusable
       {
           get
           {
               return false;
           }
       }
   }

Go with this example, (in my opinion)you don't have use Session over here. Let me know If I lost you somewhere.

-KR
 
Share this answer
 
v3
You should pass to the handler the "type" of image you want the handler to render back. In your code you could do this:

C#
Session["EmpPicture"] = dt.Rows[0]["EmpPicture"];
imgEmp.ImageUrl = "~/EmpImgHndlr.ashx?type=EmpPicture";
Session["IqamaPicture"] = dt.Rows[0]["IqamaPicture"];
imgI.ImageUrl = "~/EmpImgHndlr.ashx?type=IqamaPicture";
Session["License"] = dt.Rows[0]["License"];
imgL.ImageUrl = "~/EmpImgHndlr.ashx?type=License";
Session["Pp1"] = dt.Rows[0]["Pp1"];
imgP1.ImageUrl = "~/EmpImgHndlr.ashx?type=Pp1";
Session["Pp2"] = dt.Rows[0]["Pp2"];
imgP2.ImageUrl = "~/EmpImgHndlr.ashx?type=Pp2";


Then in your handler....

C#
public void ProcessRequest(HttpContext context)
{
    string imgType = context.Request["type"];
    if (!string.IsNullOrEmpty(imgType) && context.Session[imgType] != null)
    {
        Byte[] imgdata = (Byte[])(context.Session[imgType]);
        context.Response.BinaryWrite(imgdata);
        //context.Response.End();
    }
}
 
Share this answer
 
Comments
abdul subhan mohammed 2-Mar-14 1:30am    
In this line "imgEmp.ImageUrl = "~/EmpImgHndlr.ashx?type=EmpPicture";"
what is 'EmpPicture'?, is a session? or what?
Marc Gabrie 3-Mar-14 10:26am    
It's the name you assigned to the image which you stored in the Session that you want to display. That is the param that the Handler will take to get the image from the Session object and render it back to the client.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900