Introduction While uploading Images, we always show that image to the user by reducing the height and width of the Image. So the Image is seen shrinked, which is annoying. Now we can create thumbnail size of the Original image which will have the same quality as the original image.
Place the Controls Open a webform and place a File Field(File1), mark it as Server Control and add a Command button(cmdUpload). Also add a Image Control(Image1).
The method 'NewImageSize' will get the height and width of the original image and converts it to base 75 pixels.
private Size NewImageSize(int OriginalHeight,int OriginalWidth) { Size NewSize; double tempval; if (OriginalHeight>75 && OriginalWidth>75) { if (OriginalHeight>OriginalWidth) tempval=75.0/Convert.ToDouble(OriginalHeight); else tempval=75.0/Convert.ToDouble(OriginalWidth); NewSize=new
Size(Convert.ToInt32(tempval*OriginalWidth),Convert.ToInt32(tempval*OriginalHeight)); } else NewSize=new Size(OriginalWidth,OriginalHeight);
return NewSize; }
Write Code at Click Event
Paste the following code in the click event of the Command Button(cmdUpload).
private void cmdUpload_Click(object sender, System.EventArgs e) { string FileExtension=Path.GetExtension(File1.PostedFile.FileName); string ThumbnailPath=Server.MapPath("SomeFolderinYourApplication"+FileName+FileExtension);
System.Drawing.Image Img=System.Drawing.Image.FromFile(File1.PostedFile.FileName);
Size ThumbNailSize=NewImageSize(Img.Height,Img.Width);
System.Drawing.Image ImgThnail=new Bitmap(Img,ThumbNailSize.Width,ThumbNailSize.Height);
ImgThnail.Save(ThumbnailPath,Img.RawFormat); File1.PostedFile.SaveAs(ImagePath);
Image1.ImageUrl=ThumbnailPath;
Img.Dispose(); ImgThnail.Dispose();
}
Towards the End Thats it. Compile the application and run the web form. Upload the image throught File Field and click on the Upload button.
You can see the thumbnail size image in the folder you specified at 'SomeFolderinYourApplication'.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|