To create a Thumpnail Image in .NET
The coding is listed below:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class ThumpNailImage : System.Web.UI.Page
{
#region Declaration
string[] ImageFormat = { "jpg", "JPG", "jpeg", "JPEG", "gif", "GIF" };
string ImagePath = System.Configuration.ConfigurationManager.AppSettings["PrdUplodPath"];
#endregion
protected void Page_Load(object sender, EventArgs e)
{
//string CODE = Request.QueryString["CODE"].ToString();
string PRD_CODE = "PRD1";
#region Retrieve Image
for (int i = 0; i <= ImageFormat.Length - 1; i++)
{
FileInfo Fileinfo = new FileInfo(Server.MapPath(ImagePath) + PRD_CODE + "_S." + ImageFormat[i]);
if (Fileinfo.Exists)
{
string file = Server.MapPath(ImagePath) + PRD_CODE + "_S." + ImageFormat[i]; System.Drawing.Image image = System.Drawing.Image.FromFile(file); System.Drawing.Image thumbnailImage = image.GetThumbnailImage(150, 200, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); MemoryStream imageStream = new MemoryStream(); thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] imageContent = new Byte[imageStream.Length]; imageStream.Position = 0;
imageStream.Read(imageContent, 0, (int)imageStream.Length); Response.ContentType = "image/"+ImageFormat[i]+"";
Response.BinaryWrite(imageContent);
}
}
#endregion
}
public bool ThumbnailCallback()
{
return true;
}
}