W
wardemon
Hi All,
I have a aspx page named: ImageProcess.aspx that creates a thumbnail
version of an image by passing the ImagePath, width, and height. The
ImagePath is taken from a table from a database, while width and
height is user specific - meaning user can change this on demand.
My question is, although this rendering images on the fly is cool, I
would like to implement some sort of mechanism/logic that it wont keep
rendering the thumbnail again and again.
I thought of two things that might solve my problem, however I am
unsure of the pros and cons and which is much faster for the client.
Solution #1: Caching
----------------------------------
using System.Web.Caching;
Cache Cache = context.Cache;
//Get the image
string path = Server.MapPath(Request.FilePath);
//Check the cache
string key = path + "-" + strWidth + "-" strHeight;
if (Cache[key] != null)
{
Image cached = (Image) Cache[key];
cached.Save(Response.OutputStream,ImageFormat.Jpeg);
return;
}
else
{
Image img = Image.FromFile(path);
//Add to the cache
Cache.Add(key, thumb, null, DateTime.Now.AddSeconds(10),
TimeSpan.Zero, CacheItemPriority.High,
new CacheItemRemovedCallback(this.RemovedCallback));
}
this solution checks if a "Cache Key" exists for a particular ImagePath
+Width+Height combo exsits, then it uses the "Saved Cached Image", if
none, it will use the image directly from the Image.FromFile(path) and
adds a new "Cache Key" for it.
Solution #2: Static Images
----------------------------------
1. Create the static image that was generated on the fly by the
ImageProcess.aspx to a directory in the webserver ex: Root
\ThumbnailImages\
2. The static image will be named as ImagePath-ImageName-ImageWidth-
ImageHeight-ImageDateCreated.ImageExtension
Where
ImagePath = Image's Full Path taken from a table.column in a db
ImageName = Image File Name taken from a table column in a db
ImageWidth = Client User specific
ImageHeight = Client User spefici
ImageDateCreated = Image Upload DateTime taken from a table.column in
a db.
3. Subsequent request logic will be:
3.1 First check if the combination filename exists in the Root
\ThumbnailImages\ folder
3.2 If it exist, use that static image
3.2 If it does not exist, create image on the fly using
ImageProcess.aspx & create a thumbnail image in the thumbnail Root
\ThumbnailImages\ folder
I am a bit confuse on which to use. I am after - which is faster ;-)
Lastly, if I use Solution#1: Caching, when the user exists his or her
browser, then opens a new browser, will the "Cache Keys" still be
there? In other words, are the "Cache Keys" stored in the webserver
for use by other concurrent users?
Thanks,
Henry Wu
I have a aspx page named: ImageProcess.aspx that creates a thumbnail
version of an image by passing the ImagePath, width, and height. The
ImagePath is taken from a table from a database, while width and
height is user specific - meaning user can change this on demand.
My question is, although this rendering images on the fly is cool, I
would like to implement some sort of mechanism/logic that it wont keep
rendering the thumbnail again and again.
I thought of two things that might solve my problem, however I am
unsure of the pros and cons and which is much faster for the client.
Solution #1: Caching
----------------------------------
using System.Web.Caching;
Cache Cache = context.Cache;
//Get the image
string path = Server.MapPath(Request.FilePath);
//Check the cache
string key = path + "-" + strWidth + "-" strHeight;
if (Cache[key] != null)
{
Image cached = (Image) Cache[key];
cached.Save(Response.OutputStream,ImageFormat.Jpeg);
return;
}
else
{
Image img = Image.FromFile(path);
//Add to the cache
Cache.Add(key, thumb, null, DateTime.Now.AddSeconds(10),
TimeSpan.Zero, CacheItemPriority.High,
new CacheItemRemovedCallback(this.RemovedCallback));
}
this solution checks if a "Cache Key" exists for a particular ImagePath
+Width+Height combo exsits, then it uses the "Saved Cached Image", if
none, it will use the image directly from the Image.FromFile(path) and
adds a new "Cache Key" for it.
Solution #2: Static Images
----------------------------------
1. Create the static image that was generated on the fly by the
ImageProcess.aspx to a directory in the webserver ex: Root
\ThumbnailImages\
2. The static image will be named as ImagePath-ImageName-ImageWidth-
ImageHeight-ImageDateCreated.ImageExtension
Where
ImagePath = Image's Full Path taken from a table.column in a db
ImageName = Image File Name taken from a table column in a db
ImageWidth = Client User specific
ImageHeight = Client User spefici
ImageDateCreated = Image Upload DateTime taken from a table.column in
a db.
3. Subsequent request logic will be:
3.1 First check if the combination filename exists in the Root
\ThumbnailImages\ folder
3.2 If it exist, use that static image
3.2 If it does not exist, create image on the fly using
ImageProcess.aspx & create a thumbnail image in the thumbnail Root
\ThumbnailImages\ folder
I am a bit confuse on which to use. I am after - which is faster ;-)
Lastly, if I use Solution#1: Caching, when the user exists his or her
browser, then opens a new browser, will the "Cache Keys" still be
there? In other words, are the "Cache Keys" stored in the webserver
for use by other concurrent users?
Thanks,
Henry Wu