Resize Image – Programmatically

Thinking of resizing an image programmatically? Pretty simple straight forward.
Once done, the process can be use to resize images in bulk without any human intervention & third party software.

Resize Image

Below is the C# implementation to do the same:

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

/// <summary>
/// Resizes an image to custom height & width
/// </summary>
/// <param name="input">Input File Stream to be resized</param>
/// <param name="output">Resized Output File Stream</param>
/// <param name="width">Custom height</param>
/// <param name="height">Custom width</param>
public static void ResizeImage(Stream input, Stream output, int width, int height)
    using (var image = Image.FromStream(input))
        using (var bmp = new Bitmap(width, height))
            using (var gr = Graphics.FromImage(bmp))
                gr.CompositingQuality = CompositingQuality.HighSpeed;
                gr.SmoothingMode = SmoothingMode.HighSpeed;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.DrawImage(image,new Point(0,0));
                bmp.Save(output, ImageFormat.Png);

This implementation can be further improvise to generate various device specific renditions for an image.

(Visited 9 times, 1 visits today)