ASP.NET - Resize image size using code - Asked By Neethu on 10-Aug-12 01:40 AM

hi all,

How to resize image to a particular height & width saved in a folder when displaying .

Danasegarane Arunachalam replied to Neethu on 10-Aug-12 02:45 AM
Refer this link.

IT has the working sample with explanation

http://thefrozencoder.ca/post/2009/02/01/Upload-and-Resize-Images-using-ASPNET.aspx


Hope that helps
Peter Bromberg replied to Neethu on 10-Aug-12 08:37 AM
Aliya Stan replied to Neethu on 10-Aug-12 12:09 PM

Hi Neethu,
U can use the following code to resize the image


// Create a bitmap of the content of the fileUpload control in memory

      Bitmap originalBMP = new Bitmap(FileUpload1.FileContent);

 

      // Calculate the new image dimensions

      int origWidth = originalBMP.Width;

      int origHeight = originalBMP.Height;

      //int sngRatio = origWidth / origHeight;

      int newWidth = 117;

      int newHeight = 28;

 

      // Create a new bitmap which will hold the previous resized bitmap

      Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);

      // Create a graphic based on the new bitmap

      Graphics oGraphics = Graphics.FromImage(newBMP);

 

      // Set the properties for the new graphic file

      oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

      // Draw the new graphic based on the resized bitmap

      oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

 

      // Save the new graphic file to the server

      newBMP.Save(directory + "tn_" + filename);

 

      // Once finished with the bitmap objects, we deallocate them.

      originalBMP.Dispose();

      newBMP.Dispose();

      oGraphics.Dispose();

 

      Image1.ImageUrl = @"/Uploaded-Files/tn_" + filename;

      String filename = Image1.ImageUrl;