1 Replies - 352 Views - Last Post: 18 November 2008 - 01:45 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Resize an image in C#

Posted 23 May 2008 - 08:24 PM

Description: Need references to System.Drawing, System.Drawing.Drawing2D, System.Drawing.Imaging NamespacesThis is a snippet for resizing an image based on a percentage. If the user wants the image to be 75% of the original then pass the number 75
//Namespace Reference
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

/// 
/// method for resizing an image
/// 
/// the image to resize
/// Percentage of change (i.e for 105% of the original provide 105)
/// 
public Image Resize(Image img, int percentage)
{
    //get the height and width of the image
    int originalW = img.Width;
    int originalH = img.Height;

    //get the new size based on the percentage change
    int resizedW = (int)(originalW * percentage);
    int resizedH = (int)(originalH * percentage);

    //create a new Bitmap the size of the new image
    Bitmap bmp = new Bitmap(resizedW, resizedH);
    //create a new graphic from the Bitmap
    Graphics graphic = Graphics.FromImage((Image)bmp);
    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    //draw the newly resized image
    graphic.DrawImage(img, 0, 0, resizedW, resizedH);
    //dispose and free up the resources
    graphic.Dispose();
    //return the image
    return (Image)bmp;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Resize an image in C#

#2 thomerow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 18-November 08

Re: Resize an image in C#

Posted 18 November 2008 - 01:45 AM

If you only want to downscale images use InterpolationMode.HighQualityLinear. It is slightly faster than bicubic resizing and makes no visible difference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1