In this article, we will learn how to resize an image in C#. In this article, we will see how to resize an image with or without losing its image quality and with or without maintaining its aspect ratio.
Here, we are using three methods to resize an image as Follow:
Example 1: In this example, we only just resize the image without maintaining its aspect ratio and image quality.
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Tutorialsrack
{
class Program
{
/* How to Resize a Bitmap Image in C# */
static void Main(string[] args)
{
// Resolution of Bitmap Image 512x512
//Replace Image Path With your image Path
Bitmap bmp = new Bitmap(@"C:\Users\GOD\Desktop\testImage.bmp");
//Enter your desired resolution for image
Bitmap bitmap = ResizeBitmap(bmp, 256, 256);
//You can Save image as per your Format such as PNG,JPG,BMP, etc
//Replace Image Path With your image Path
bitmap.Save(@"C:\Users\GOD\Desktop\OutputImage.png", ImageFormat.Png);
Console.WriteLine("Image Resolution has been Successfully Resized!!");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static Bitmap ResizeBitmap(Bitmap bmp, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawImage(bmp, 0, 0, width, height);
}
return bitmap;
}
}
}
Example 2: In this example, we will resize the image by maintaining its image quality.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Tutorialsrack
{
class Program
{
/* How to Resize an Image in C# */
static void Main(string[] args)
{
// Resolution of Bitmap Image 512x512
//Replace Image Path With your image Path
string path = @"C:\Users\GOD\Desktop\testImage.bmp";
//Enter your desired resolution for image
Resize(path, 300, 300);
Resize(path, 150, 150);
Resize(path, 225, 225);
Resize(path, 188, 188);
Console.WriteLine("Image has been successfully Resized!!");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static void Resize(string srcPath, int width, int height)
{
Image image = Image.FromFile(srcPath);
Bitmap resultImage = ResizeImage(image, width, height);
resultImage.Save(@"C:\Users\GOD\Desktop\"+width+"x"+height+".png",ImageFormat.Png);
}
public static Bitmap ResizeImage(Image image, int width, int height)
{
var dest_Rect = new Rectangle(0, 0, width, height);
var dest_Image = new Bitmap(width, height);
dest_Image.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(dest_Image))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, dest_Rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return dest_Image;
}
}
}
Example 3: In this example, we will resize an image with maintaining its image quality and aspect ratio.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Tutorialsrack
{
class Program
{
/* How to Resize an Image with Maintaining its Image Quality and Aspect Ratio in C# */
static void Main(string[] args)
{
// Resolution of Bitmap Image 512x512
//Replace Image Path With your image Path
string path = @"C:\Users\GOD\Desktop\testImage.bmp";
//Enter your desired resolution for image
Resize(path, 1024, 768);
Resize(path, 300, 300);
Resize(path, 150, 150);
Resize(path, 225, 225);
Resize(path, 188, 188);
Console.WriteLine("Image has been successfully Resized!!");
//Hit ENTER to exit the program
Console.ReadKey();
}
public static void Resize(string srcPath, int width, int height)
{
Image image = Image.FromFile(srcPath);
// if you want to Preserve aspect ratio ,then fourth parameter must be true
Bitmap resultImage = ResizeImage(image, width, height, true);
resultImage.Save(@"C:\Users\GOD\Desktop\" + width + "x" + height + ".png", ImageFormat.Png);
}
/* Resize image and if width and height are different ratio, keep it in center.*/
public static Bitmap ResizeImage(Image image, int width, int height, bool preserveAspectRatio)
{
int drawWidth = width;
int drawHeight = height;
if (preserveAspectRatio)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = (float)width / (float)originalWidth;
float percentHeight = (float)height / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
drawWidth = (int)(originalWidth * percent);
drawHeight = (int)(originalHeight * percent);
}
else
{
drawWidth = width;
drawHeight = height;
}
var ResizeImage = new Rectangle((width - drawWidth) / 2, (height - drawHeight) / 2, drawWidth, drawHeight);
var dest_Image = new Bitmap(width, height);
dest_Image.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(dest_Image))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, ResizeImage, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return dest_Image;
}
}
}
I hope this article will help you to understand how to resize an image in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments