How to Resize an Image C#

Resizing an image with c#

You can do this:

        public void ResizeImage(string fileName, int width, int height)
{
using (Image image = Image.FromFile(fileName))
{
new Bitmap(image, width, height).Save(fileName);
}
}

If its a new file just replace with this or with a custom path of your choosing:

new Bitmap(image, width, height).Save(fileName.Insert(fileName.LastIndexOf('.'),"A"));

Image resizing using C#

Image resizing is functionality is built right into the .NET framework. There are a couple of different approaches:

  • GDI+
  • WIC
  • WPF

Here's a nice blog post covering the differences between them.

Here's an example with GDI+:

public void Resize(string imageFile, string outputFile, double scaleFactor)
{
using (var srcImage = Image.FromFile(imageFile))
{
var newWidth = (int)(srcImage.Width * scaleFactor);
var newHeight = (int)(srcImage.Height * scaleFactor);
using (var newImage = new Bitmap(newWidth, newHeight))
using (var graphics = Graphics.FromImage(newImage))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
newImage.Save(outputFile);
}
}
}

Resizing image in C#

Instead of copying the bitmaps in two steps, make it one step. That way you reduce the memory usage quite a bit as you don't have two copies of the oringal image in memory at once.

foreach (string strJPGImagePath in strarrFileList) {
Bitmap bmpDest;
using(Bitmap bmpOrig = new Bitmap(strJPGImagePath)) {
bmpDest = new Bitmap(bmpOrig, new Size(100, 200));
}
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
bmpDest.Dispose();
}

How to resize image without losing quality

I'm using the following method for thousands of images and it never loses significant quality or results in a dotted image.

public static Image ScaleImage(Image image, int height)
{
double ratio = (double)height/ image.Height;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
image.Dispose();
return newImage;
}

I've taken the liberty of scaling the image you posted using this code to 128px (Like the thumbnail you posted).

Result:

Sample Image

How do I resize an image to fill the new dimensions?

Although none of the answers worked for me, I appreciate all the help and suggestions on which I could get to a solution pretty fast, here is the piece of code that worked for me:

private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);

destImage.SetResolution(HorizontalResolution, VerticalResolution);

using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;


//drawImage must be set this way to get the desired outcome
graphics.DrawImage(imageBox.Image, new Rectangle(0, 0, image.Width, image.Height), destRect, GraphicsUnit.Pixel);
}

return destImage;
}

Basically I removed the wrapMode code and changed my drawImage method.

How to resize an image with System Drawing and lower the image size

In my original answer I posted this code assuming that it was not having the same issue you were with file size inflation... I was wrong. So after some tinkering, I realized that my JPG images were being saved with a JPG extension... but encoded as PNG, thus the increase in file size. Here is an updated codebase, tested reliable with PNG, GIF, and JPG. The file size will be lower when the image is smaller than the original.

First a basic method that takes a Bitmap and resizes it.

public static Bitmap Resize(Bitmap imgPhoto, Size objSize, ImageFormat enuType)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = objSize.Width;
int destHeight = objSize.Height;

Bitmap bmPhoto;
if (enuType == ImageFormat.Png)
bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format32bppArgb);
else if (enuType == ImageFormat.Gif)
bmPhoto = new Bitmap(destWidth, destHeight); //PixelFormat.Format8bppIndexed should be the right value for a GIF, but will throw an error with some GIF images so it's not safe to specify.
else
bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);

//For some reason the resolution properties will be 96, even when the source image is different, so this matching does not appear to be reliable.
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//If you want to override the default 96dpi resolution do it here
//bmPhoto.SetResolution(72, 72);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}

Here's how to use the Resize method...

   String strImageFile = Server.MapPath("/Images/ImageFile.jpg");
System.Drawing.Bitmap objImage = new System.Drawing.Bitmap(strImageFile);
System.Drawing.Size objNewSize = new System.Drawing.Size(100, 50);
System.Drawing.Bitmap objNewImage = Resize(objImage, objNewSize, ImageFormat.Jpeg);
objNewImage.Save(Server.MapPath("/Images/FileName_Resized.jpg"), ImageFormat.Jpeg);
objNewImage.Dispose();

This method adds a layer of complexity, you can define a maximum size constraint, and the method will Resize the image to stay in proportion... but be no larger than... the maximum size. It will leave the image alone if it is less than or equal to the max size, returning the original Bitmap instead.

public static Bitmap SmartResize(string strImageFile, Size objMaxSize, ImageFormat enuType)
{
Bitmap objImage = null;
try
{
objImage = new Bitmap(strImageFile);
}
catch (Exception ex)
{
throw ex;
}

if (objImage.Width > objMaxSize.Width || objImage.Height > objMaxSize.Height)
{
Size objSize;
int intWidthOverrun = 0;
int intHeightOverrun = 0;
if (objImage.Width > objMaxSize.Width)
intWidthOverrun = objImage.Width - objMaxSize.Width;
if (objImage.Height > objMaxSize.Height)
intHeightOverrun = objImage.Height - objMaxSize.Height;

double dblRatio;
double dblWidthRatio = (double)objMaxSize.Width / (double)objImage.Width;
double dblHeightRatio = (double)objMaxSize.Height / (double)objImage.Height;
if (dblWidthRatio < dblHeightRatio)
dblRatio = dblWidthRatio;
else
dblRatio = dblHeightRatio;
objSize = new Size((int)((double)objImage.Width * dblRatio), (int)((double)objImage.Height * dblRatio));

Bitmap objNewImage = Resize(objImage, objSize, enuType);

objImage.Dispose();
return objNewImage;
}
else
{
return objImage;
}
}

Here's how to implement it...

String strImageFile = Server.MapPath("/Images/ImageFile.png");
System.Drawing.Size objMaxSize = new System.Drawing.Size(100, 100);
System.Drawing.Bitmap objNewImage = SmartResize(strImageFile, objMaxSize, ImageFormat.Png);
objNewImage.Save(Server.MapPath("/Images/FileName_Resized.png"), ImageFormat.Png);
objNewImage.Dispose();


Related Topics



Leave a reply



Submit