meta info

Image Resizing Methods

By Ziya Mollamahmut

- Scale : Auto scales image by width or height till both borders are fitted, and keeps aspect ratio same as the original image

using(var img = Image.FromFile("my-image.jpg"))
{
    img.Scale(800, 600)
       .SaveAs("resized-image.jpg");
}

- Scale by width : Scales image by provided width value, auto adjusts new height according to aspect ratio.

img.ScaleByWidth(800);

- Scale by height : Scales image by provided height value, auto adjusts new width according to aspect ratio.

img.ScaleByHeight(600);

- Scale and crop : Scalesthe image to fit new width or new height (which fits first), then crops out the rest of the image.

img.ScaleAndCrop(800, 600);

// or
img.ScaleAndCrop(800, 600, TargetSpot.Center);

- Crop : Directly crop a specified spot of the image, without scaling.

img.Crop(800, 600);

// or
img.Crop(800, 600, TargetSpot.Center);
  • All resizing methods will return a System.Drawing.Image file that can be saved in any supported image format (JPG, PNG, etc.)
  • All methods can be overloaded with an optional parameter: GraphicOptions.
  • TargtSpot can be used to quickly select a predefined region of the image.

Live demos:

http://demo.ziyad.info/en/