Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Displaying Images in ASP.NET Using HttpHandlers

Viewed:  2374 
Posted On:  05/10/2013 03:11:59 
How to display Images using HttpHandlers in 

In this article I am going to show how we can show images using handler.

In below example I am using a XML which have my Image related data... You can pass your own height and width to show image...

<?xml version="1.0" encoding="utf-8" ?>

I am reading this XML on asp page and showing image in a DataList.. my aspx is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Showing Image Using Handler</title>
    <form id="form1" runat="server">
        <table cellpadding="4" cellspacing="4" width="40%" align="center" style="border: solid 1px Gray;">
                    <asp:DataList ID="listImg" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"
                        CellPadding="4" ForeColor="#333333">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <tr width="100%">

                                        <img src="ImageHandler.ashx?width=100&Height=100&id=<%# DataBinder.Eval(Container.DataItem, "Id") %>&ImagePath=<%# DataBinder.Eval(Container.DataItem, "ImgPath") %>" />

                        <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
                        <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
My aspx.cs is: 

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; 

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
    private void ReadData()
        DataSet ds = new DataSet();
        listImg.DataSource = ds;
My Handler code is:

 <%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using System.IO;
using System.Web.SessionState;

public class ImageHandler : IHttpHandler

    private string XLen = "40";
    private string YLen = "40";
    public void ProcessRequest(HttpContext context)
        if (context.Request["id"] != null)
            if (context.Request["Height"] != null)
                XLen = context.Request["Height"].ToString();
            if (context.Request["width"] != null)
                YLen = context.Request["width"].ToString();
            string id = context.Request["id"].ToString();
            string ImgPath = context.Request["ImagePath"].ToString();
             Hashtable ht;
            ht = new Hashtable();
            FileStream fInfo = new FileStream(context.Server.MapPath(ImgPath), FileMode.Open);
            byte[] bFile;
            bFile = GenerateThumbnail(fInfo, XLen, YLen);
            ht.Add(id, bFile);
            Byte[] arrImg = (byte[])ht[id];
            context.Response.ContentType = "image/jpeg";
    private byte[] GenerateThumbnail(Stream fStream, string xLen, string yLen)
        Image img = Image.FromStream(fStream);
        Image thumbnailImage = img.GetThumbnailImage(int.Parse(xLen), int.Parse(yLen), new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); 
        MemoryStream imageStream = new MemoryStream();
        thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] imageContent = new Byte[imageStream.Length];
        imageStream.Position = 0;
        imageStream.Read(imageContent, 0, (int)imageStream.Length);
        return imageContent;

    public bool ThumbnailCallback()
        return true;

public bool IsReusable
            return false;

Below is my output: 

ASHX Handler.png

Image 1.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us