Skip to main content

Image Resize in C#

The below code snippet (adapted from Stackoverflow) will do a image resize to a desired width in pixels. I hope it helps.

/// <summary>
/// Resize image to desired dimension based on width
/// </summary>
/// <param name="strSrcPath">Full absolute path of image source</param>
/// <param name="strDestPath">Full absolute path for resized image to be saved to</param>
/// <param name="iNewWidth">New desired width in pixels</param>
/// <param name="blnIgnoreIfNewWidthLarger">Do not resize image should new desired width is larger than width of original image if set to true</param>
public void Resize(string strSrcPath, string strDestPath, int iNewWidth, bool blnIgnoreIfNewWidthLarger)
 bool blnDoNotResize = false;
 System.Drawing.Image objSrcImage;
 System.Drawing.Bitmap objDestImage;
 System.Drawing.Graphics objGrapics;

  objSrcImage = System.Drawing.Image.FromFile(strSrcPath);

  int iSrcWidth = objSrcImage.Width;
  int iSrcHeight = objSrcImage.Height;

  blnDoNotResize = ((iSrcWidth == iNewWidth) || (blnIgnoreIfNewWidthLarger && iNewWidth > iSrcWidth));

  if (!blnDoNotResize)
   int iNewHeight = iSrcHeight * iNewWidth / iSrcWidth;

   int iSrcX = 0, iSrcY = 0, iDestX = 0, iDestY = 0;
   float fPercent = 0, fPercentW = 0, fPercentH = 0;

   fPercentW = ((float)iNewWidth / (float)iSrcWidth);
   fPercentH = ((float)iNewHeight / (float)iSrcHeight);
   if (fPercentH < fPercentW)
    fPercent = fPercentH;
    iDestX = System.Convert.ToInt16((iNewWidth - (iSrcWidth * fPercent)) / 2);
    fPercent = fPercentW;
    iDestY = System.Convert.ToInt16((iNewHeight - (iSrcHeight * fPercent)) / 2);

   int iDestWidth = (int)(iSrcWidth * fPercent);
   int iDestHeight = (int)(iSrcHeight * fPercent);

   objDestImage = new System.Drawing.Bitmap(iNewWidth, iNewHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
   objDestImage.SetResolution(objSrcImage.HorizontalResolution, objSrcImage.VerticalResolution);

   objGrapics = System.Drawing.Graphics.FromImage(objDestImage);
   objGrapics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    new System.Drawing.Rectangle(iDestX, iDestY, iDestWidth, iDestHeight),
    new System.Drawing.Rectangle(iSrcX, iSrcY, iSrcWidth, iSrcHeight),



   System.IO.File.Copy(strSrcPath, strDestPath, true);
 catch (Exception ex)
  throw ex;
  objSrcImage = null;
  objDestImage = null;
  objGrapics = null;

Popular Posts

Ho Ching named 5th most powerful and is mistaken as first lady by Forbes

Forbes named Singapore Prime Minister wife and CEO of Temasek Holdings, Ho Ching, as the 5th most powerful woman in the world. Ho Ching is mistaken as Singapore's first lady! OMG!

I wonder how can Forbes makes such a blunder. For a complete list, refer to here.

How to stop FortiClient from starting automatically?

Installed FortiClient recently but the challenge in disabling the application/service from running automatically on every start-up annoyed me. Attempt to stop 'FortiClient Service Scheduler' only return 'Parameter is incorrect' error message.

An article on Technet help solve my trouble. To stop FortiClient from starting automatically, try the following:
Shut down FortiClient from the system tray.
Run net stop fortishield on command prompt.
Run msconfig.
On msconfig, switch to the Services tab. Clear the FortiClient Service Scheduler check box and click Apply.Run services.msc on command prompt to open up show all available services.Look for FortiClient Service Scheduler. Switch Startup type to Manual.Restart your computer. FortiClient should not be running automatically the next time round. Hope it helps.

President Elect Dr Tony Tan post election interview

I feel Dr Tony Tan can handle the reporters quite well.