Hitesh,
I am copying contents of one of the image handlers i have written and how i
tend to use it.
What i tend to do is store images in database and then i render them as
needed. For that purpose i pass the ProductColorID. You can choose to pass
path or some unique identifier as a part of handlers query string. See the
way i am calling the handler. I just write the handler url in my aspx page.
The brower then calls the handler to fetch the image.
If you are passing image from hard drive then you will have to use
Image.FromFile();
so what you can do is pass a unique identified or path and load the image
from file or. Infact you dont need to anything if you dont want manipulation.
you can also use
Response.WriteFile() method. look into that as well...
HTH
Hermit
-------------------------------------------------------------------------------------
IProductList.aspx
------------------------
<asp:datalist id="dlProducts" runat="server" RepeatDirection="Horizontal"
ShowFooter="False" HorizontalAlign="Center"
ShowHeader="False" CellPadding="0">
<EditItemStyle HorizontalAlign="Center"></EditItemStyle>
<ItemStyle VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<TABLE cellSpacing="0" cellPadding="1" border="0">
<TR>
<TD vAlign="bottom" align="center">
<asp:HyperLink id="pfProductNav" runat="server" Target=_top
BorderStyle="None" NavigateUrl='<%# "ProductInfo.aspx?Product=" +
DataBinder.Eval(Container.DataItem, "ProductID") + "&Color=" +
DataBinder.Eval(Container.DataItem, "ProductColorID") %>' Height="100px"
ImageUrl='<%#
"~/GetImage.ashx?IsDefault=true&showSize=Thumbnail&ProductColorID=" +
DataBinder.Eval(Container.DataItem, "ProductColorID") %>'>
</asp:HyperLink>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist>
---------------------------------------------------------------------
GetImage.ashx
----------------------------------------------------------------------
<%@ WebHandler Language="C#" Class="ImageGenerator" %>
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.Caching;
using StepTallV2.Components;
public class ImageGenerator : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
//
// Extract input parameters from the query string
//
bool bIsDefaultImage = true;
bool bShowThumbnail = true;
bool bIsFeatured = false;
string ProductColorID = (string) context.Request["ProductColorID"];
string showImageSize = (string)context.Request["showSize"];
string isDefaultImage = (string) context.Request["isDefaultImage"];
if(ProductColorID == null || ProductColorID == "")
{
DrawImageNotFound(ref context);
return;
}
if(isDefaultImage != null && isDefaultImage == "false")
bIsDefaultImage = false;
ImageType imgType = ImageType.Full;
switch(showImageSize)
{
case "UltraSmall":
imgType = ImageType.UltraSmall;
break;
case "Thumbnail":
imgType = ImageType.Thumbnail;
break;
case "Full":
imgType = ImageType.Full;
break;
default:
imgType = ImageType.Featured;
break;
}
// write the content type
context.Response.ContentType = "image/jpeg";
context.Response.Expires = 20;
// check the cache
Cache myCache = context.Cache;
string key = ProductColorID + "-" + Convert.ToString(bIsDefaultImage) +
"-" + imgType.ToString();
if(myCache[key] != null)
{
byte[] bytesImg = (byte[]) myCache[key];
try
{
context.Response.BinaryWrite(bytesImg);
}
finally
{
bytesImg = null;
}
return;
}
// fetch the object from db or create it.
ImagesDB myImageDB = new ImagesDB();
byte[] ImageData = myImageDB.GetImage(int.Parse(ProductColorID),
bIsDefaultImage);
if(ImageData != null)
{
WriteImage(ref ImageData, ref context, imgType, key);
ImageData = null;
return;
}
else
{
DrawImageNotFound(ref context);
}
}
public bool IsReusable
{
get { return true; }
}
public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
{
}
public bool ThumbnailCallback()
{
return true;
}
private void WriteImage(ref byte[] ImageData, ref HttpContext context,
ImageType imgType, string cacheKey)
{
System.Drawing.Image myImage = null;
MemoryStream myStream = new MemoryStream();
MemoryStream processedMemStream = new MemoryStream();
try
{
myStream.Write(ImageData, 0, ImageData.Length);
myImage = System.Drawing.Image.FromStream(myStream);
int imageWidth = myImage.Width;
int imageHeight = myImage.Height;
int processedHeight;
if(imgType == ImageType.Full)
{
processedHeight = imageHeight;
}
else
{
processedHeight = (int)imgType;
}
double multiplicationFactor = (double)processedHeight/(double)imageHeight;
int processedWidth = (int)( (double)imageWidth * multiplicationFactor);
Bitmap processedBP = new Bitmap(processedWidth, processedHeight);
Graphics g = Graphics.FromImage(processedBP);
try
{
g.SmoothingMode =SmoothingMode.HighQuality;
g.InterpolationMode =InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode =PixelOffsetMode.HighQuality;
Rectangle rect=new Rectangle(0,0,processedWidth,processedHeight);
//Draw the old image on to the new image using the graphics object:
g.DrawImage(myImage,rect,0,0,myImage.Width,myImage.Height,GraphicsUnit.Pixel);
processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);
processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);
processedBP.Save(processedMemStream, ImageFormat.Jpeg);
byte[] processedImageData = processedMemStream.ToArray();
if(processedImageData != null)
{
context.Cache.Add(cacheKey, processedImageData, null,
Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10),
CacheItemPriority.Normal, new
CacheItemRemovedCallback(this.RemovedCallback));
context.Response.BinaryWrite(processedImageData);
processedImageData = null;
}
}
finally
{
g.Dispose();
processedBP.Dispose();
}
}
finally
{
processedMemStream.Close();
myStream.Close();
myImage.Dispose();
}
}
private void DrawImageNotFound(ref HttpContext context)
{
Bitmap myBitMap = new Bitmap(50,50);
Graphics myGraphics = Graphics.FromImage(myBitMap);
myGraphics.DrawLine(new Pen(Color.Red), 0, 0, 50, 50);
myGraphics.DrawLine(new Pen(Color.Red), 0, 50, 50, 0);
context.Response.ContentType = "image/gif";
myBitMap.Save(context.Response.OutputStream, ImageFormat.Gif);
myBitMap.Dispose();
myGraphics.Dispose();
}
}
---------------------------------------------------------------------------