Resize Image at Runtime


Image resizing is widely used in web and windows application. In dot net we have System.Drawing.Image class which can be used for image resizing at run time (code behind). To better understand these please go through the below information:

  1. Create a new Console Application.
  2.  
  3. Add a class named ImageResizer.cs and create a method ResizeImage().
     
  4. In Main method of Program.cs call the method ResizeImage() by passing the appropriate parameter.
Below is the code walkthrough:

Code snippet of ImageResizer.cs class

    /// <summary>
    /// class contains the method which resize the image
    /// </summary>
    public class ImageResizer
    {
        /// <summary>
        /// method use to resize the image at run time.
        /// </summary>
        /// <param name="OriginalFile">orginal image file path</param>
        /// <param name="NewFile">target image file path</param>
        /// <param name="NewWidth">new resized width of image</param>
        /// <param name="MaxHeight">new resized height of image</param>
        /// <param name="OnlyResizeIfWider">true/false if true than it will resize the image if the original width is higher than the new width</param>
        public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }
 
            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            // Save resized picture
            NewImage.Save(NewFile);
        }
    }

Code snippet of Main method (Program.cs)

static void Main(string[] args)
        {
            ImageResizer imgRe = new ImageResizer();
            string originalImageFilePath = System.Text.RegularExpressions.Regex.Replace(Environment.CurrentDirectory, @"\\([^\\])+\\([^\\])+(\\)?$", "") + "\\sampleImg\\1_300_300.jpg";
            string newImageFilePath = System.Text.RegularExpressions.Regex.Replace(Environment.CurrentDirectory, @"\\([^\\])+\\([^\\])+(\\)?$", "") + "\\sampleImg\\" + DateTime.Now.ToShortDateString().Replace("/", "").Replace("-", "") + "_" + DateTime.Now.Ticks.ToString() + ".jpg";

            imgRe.ResizeImage(originalImageFilePath, newImageFilePath, 155, 155, true);

            Console.WriteLine("Press any Key to exit");
            Console.ReadLine();

        }


Similar Articles