.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Generate Thumbnail in Image Uploading

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  
 

Below is the code to Generate Thumbnail in Image while Uploading Image in ASP.Net and save on the server on the fly.

Design Page:





.cs page:

protected void btnSave_Click(object sender, EventArgs e)
{

System.Drawing.Image imagefile = System.Drawing.Image.FromStream(upImage.PostedFile.InputStream);
string strext = Path.GetExtension(upImage.FileName).ToLower().Trim();
Button btnButton = (Button)sender;
string sImageType = btnButton.ID;
string strFilePath = Request.PhysicalApplicationPath + "Images/ItemPictures/";
string LocalFilename = "";

if (sImageType == "btnSave")
{
LocalFile = FilePath + UniqueID + "_" + "ItemPhoto" + ext;
if (File.Exists(LocalFile))
File.Delete(LocalFile);
upImage.SaveAs(LocalFile);
}
float imgWidth = image.PhysicalDimension.Width;
float imgHeight = image.PhysicalDimension.Height;
float imgSize = imgHeight > imgWidth ? imgHeight : imgWidth;
float imgResize = imgSize <= 150 ? (float)1.0 : 150 / imgSize;
imgWidth *= imgResize; imgHeight *= imgResize;
System.Drawing.Image thumb1 = image.GetThumbnailImage((int)imgWidth, 
(int)imgHeight, delegate() { return false; }, (IntPtr)0);
LocalFile = FilePath + UniqueID + "_" + "ItemPhoto_th1" + ext;
if (File.Exists(LocalFile))
File.Delete(LocalFile);

thumb1.Save(LocalFile);
oItem.Thumb1Image = UniqueID + "_" + "ItemPhoto_th1" + ext;

}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend