In the Image Tag specify the URL as below
<img src="ThumbnailGen.aspx?img=xyz.jpg" />
Create a file ThumbnailGen.aspx as below
public class ThumbnailGen : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // get the file name from querystring string file = Request.QueryString["img"];
// create an image object, System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file));
// create the actual thumbnail image // by default the thumbnail is configured to 64x64 System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
// make a memory stream to work with the image bytes MemoryStream imageStream = new MemoryStream();
// save the image into the memory stream thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg);
// make byte array the same size as the image byte[] imageContent = new Byte[imageStream.Length];
// rewind the memory stream imageStream.Position = 0;
// load the byte array with the image imageStream.Read(imageContent, 0, (int)imageStream.Length);
// return byte array to caller with image type Response.ContentType = "image/jpeg"; Response.BinaryWrite(imageContent); }
/// /// Required, but not used. /// This is a GetThumbnailImageAbort handler /// /// true public bool ThumbnailCallback() { return true; // nothing is done here } }
The Thumbnail is configured by default to 64 x 64, if required it can be sent thru querystring along with the img file name and image can be generated as per that.
Happy Coding.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|