Click here to Skip to main content
15,886,689 members
Articles / Web Development / ASP.NET

Generic Image Handler Using IHttpHandler

Rate me:
Please Sign up or sign in to vote.
4.61/5 (17 votes)
13 Mar 2009Public Domain2 min read 101.4K   2.6K   34  
Example of an HttpHandler to handle image URLs and hide server mapped paths from users. This can also be used to generate thumbnails of any size.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ImageHandlerClient.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" AutoGenerateColumns="false">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                
                <asp:TemplateField HeaderText="User">                    
                    
                    <ItemTemplate >
                        <table>
                            <tr>
                                <td valign="middle">
                                    <asp:Label ID="Label1" runat="server" ><%# Eval("Name") %></asp:Label>
                                </td>
                                <td valign="top">
                                    <asp:Image ID="Image1" ImageUrl='<%# "ImageHandler.aspx?tName=UserImage&id=" + DataBinder.Eval(Container.DataItem,"id") %>' runat="server" />                                
                                </td>
                            </tr>
                        </table>                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Designation" HeaderText="Designation" />
                <asp:BoundField DataField="Contact" HeaderText="Contact" />
                <asp:TemplateField HeaderText="Status">                                        
                    <ItemTemplate >                       
                        <asp:Image  ID="Image2" ImageUrl='<%# "ImageHandler.aspx?tName=StatusImage&xlen=60&ylen=60&id=" + DataBinder.Eval(Container.DataItem,"status") %>' runat="server" />                                                       
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Geometric
India India
B.E. in Information Technology
MCTS(.NET 2.0 )

Comments and Discussions