Scale Image in C#

This tutorial covers how to scale image in C#. It contains the system configuration, the stepwise algorithm, and a runnable sample code to upscale image in C#. It also covers custom properties and methods to customize the process to meet your requirements.

Steps to Scale Photo in C#

  1. Configure the system to work with Aspose.Drawing for .NET to scale images
  2. Initiate an instance of the Bitmap class
  3. Create an object of the Graphics class and set the interpolation mode
  4. Set the image coordinates and the scaling factor
  5. Scale the input and write the generated image

These steps elaborate the algorithm to scale image without losing quality in C#. Create a bitmap class object with the specified format and size. Next, set the scale of the image and render the generated image in JPG or PNG image format.

Code to Upscale Image in C#

using System;
using Aspose.Drawing;
class Program
{
static void Main(string[] args)
{
License lic = new License();
lic.SetLicense("license.lic");
Bitmap image = new Bitmap("aspose-logo.png");
int newWidth = image.Width * 5;
int newHeight = image.Height * 5;
Bitmap bitmap = new Bitmap(newWidth, newHeight, Aspose.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = Aspose.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
// Scale the image 5x
Rectangle expansionRectangle = new Rectangle(0, 0, newWidth, newHeight);
graphics.DrawImage(image, expansionRectangle);
bitmap.Save("Scale.png");
System.Console.WriteLine("Image Scaled successfully");
}
}

This sample code is a basic version to showcase how to up scale image in C#. Improvise this code by setting different pixel interpolation modes such as bicubic, bilinear, default, etc. Similarly, you can change the scaling factor to adjust the image size to your required height and width.

This guide presents the quick approach to scale photo in C#. Besides, if you are interested in clipping images, refer to the article on Image Clipping in C#.

 English