Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image resizing in .Net with Antialiasing

Tags:

.net

graphics

I've got some C# code that resizes images that I think is pretty typical:

Bitmap bmp = new Bitmap(image, new Size(width, height));
Graphics graphics = Graphics.FromImage(bmp);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawImage(bmp, width, height);

The problem is that the resultant images are clearly aliased and changes to the InterpolationMode and SmoothingMode properties seem to make no difference.

Any pointers?

like image 365
Nick Higgs Avatar asked Dec 02 '08 10:12

Nick Higgs


2 Answers

It turns the code was just wrong. It was actually resizing the image without interpolation in the Bitmap constructor, and then attempting to smoothly resize that version to the size it was already at. Here is the amended code:

Bitmap bmp = new Bitmap(width, height);
Graphics graph = Graphics.FromImage(bmp);
graph.InterpolationMode = InterpolationMode.High;
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.AntiAlias;
graph.DrawImage(image, new Rectangle(0, 0, width, height));

As far as anti-aliasing goes, the most important parameter is graph.InterpolationMode.

Thanks.

like image 178
Nick Higgs Avatar answered Nov 21 '22 02:11

Nick Higgs


Try graphics.DrawImage(bmp, 0, 0, width, height); Also check this MSDN Article on interpolation.

like image 29
Firas Assaad Avatar answered Nov 21 '22 03:11

Firas Assaad