In this post I will show how to display ThumbNail Images Dynamically Using Httphandlers in asp.net and C#. In this post i will Show How to Access Session Values in HttpHandler
Add new generic handler and name it as you like In my case ThumbNail.ashx and the code for this handler can be seen below.
Out Put :
Http Handler (*.ashx) :
Add new generic handler and name it as you like In my case ThumbNail.ashx and the code for this handler can be seen below.
Out Put :
Http Handler (*.ashx) :
<%@ WebHandler Language="C#" Class="ThumbNail" %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
public class ThumbNail : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
//string imageurl = context.Request.QueryString["ImURL"];
string imageurl = context.Session["imgID"].ToString() + ".jpg";
string str = context.Server.MapPath("~/images/" + imageurl);
Bitmap bmp = new Bitmap(str);
System.Drawing.Image img = bmp.GetThumbnailImage(250, 250, null, IntPtr.Zero);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bmpBytes = ms.GetBuffer();
img.Dispose();
ms.Close();
context.Response.BinaryWrite(bmpBytes);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
Default.aspx Page :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="UploadImageThumbnail.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>
<center>
<table>
<tr>
<td>
Select ID :
</td>
<td>
<asp:DropDownList ID="ddlImage" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlImage_SelectedIndexChanged">
<asp:ListItem Text="1" Value="1"/>
<asp:ListItem Text="2" Value="2"/>
<asp:ListItem Text="3" Value="3"/>
<asp:ListItem Text="4" Value="4"/>
<asp:ListItem Text="5" Value="5"/>
<asp:ListItem Text="6" Value="6"/>
<asp:ListItem Text="7" Value="7"/>
<asp:ListItem Text="8" Value="8"/>
<asp:ListItem Text="9" Value="9"/>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<asp:Image ID="imgLoad" runat="server" AlternateText="Image" />
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
Code Behind :
using System;
namespace UploadImageThumbnail
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlImage_SelectedIndexChanged(object sender, EventArgs e)
{
Session["imgID"] = ddlImage.SelectedValue.ToString();
imgLoad.ImageUrl = "ThumbNail.ashx?ImURL=" + Session["imgID"].ToString();
}
}
}
Download from Here
No comments:
Post a Comment