|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionCreating thumbnail with ASP.NET with C#. There are several ways to do that but this way I feel much better and efficient and also this doesn't affect initial page load time. BackgroundWhile working for a website or an web application, you must have came across a situation where you need to display a thumbnail images for a larger images. There are several work around for it but I am going to show how to do that in the much better and efficient way, in this way your initial page load time will not be delayed because of the thumbnail images. ApproachMy approach of creating thumbnail is to create an arrays of bytes at run time and specify the img src attribute to it. I will also display NoImage.gif if my no source is being given to the function or any error occurred while dynamically generating thumbnail images. ShowImage.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowImage.aspx.cs" Inherits="images_ShowImage" %>
<!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>Show Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Just right click to your project and add a .aspx page with default contents mostly similar to above. ShoeImage.ascx.cs protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Response.Clear();
int maxHeight = 200 // you can get from your config file;
int maxWidth = 250 // you can get from your config file;
if (Request["width"] != null)
{
maxWidth = Int32.Parse(Request["width"].ToString());
}
if (Request["height"] != null)
{
maxHeight = Int32.Parse(Request["height"].ToString());
}
string imageName = Request.QueryString["image"];
string extension = System.IO.Path.GetExtension(Server.MapPath(imageName));
byte[] pBuffer = Utility.CreateThumbnail(imageName, maxHeight, maxWidth, extension);
//set response content type: image/jpeg, image/gif, image/png, etc...
Response.ContentType = "image/" + extension;
//write the image to the output stream
Response.OutputStream.Write(pBuffer, 0, pBuffer.Length);
Response.End();
}
}
In the above Page_Load method, first I am clearing all the contents of the page by using Response.Clear() then I am getting my standard height and width of my thumbnail images. As specified you may specify it in your config file and get it here. CreateThumbnail function In the above function, i am getting the actual path of the image and creating an image object from the image path parameter passed to this function. Now I am getting the width and height property of the image I got and deciding whether I need to resize my image or not to best fit based on the standard height and width I am getting from my Page_Laod method.
How to callTo call the above created functionality you need to specify the src attribute of the img element to ShowImage.aspx file and specify the requeired querystring value (In my case image is must and width and height is optional the optional querystring value) <img src="/images/ShowImage.aspx?image=/images/sheonarayan.gif" />[
OR
<img src="/images/ShowImage.aspx?image=/images/sheonarayan.gif&width=200&height=250" />
ConclusionWhen you will use above way of creating thumbnail images, you will notice that your entire pages will be loaded then one by one your thumbnail images will be generated on the fly and it will be displayed on the page so It is not affecting your initial page load time. If you have any comment of suggestions, please write to me. Thanks and Happy coding !!! Originally posted by me at http://www.dotnetfunda.com/articles/article63.aspx
|
|||||||||||||||||||||||||||||||||||||||||||||||||||