tags:

views:

1708

answers:

5

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?

+2  A: 

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

Firas Assaad
A: 

Hi,

there is an article on CodeProject describing an improved antialiasing method:

http://www.codeproject.com/KB/GDI-plus/AntiAliasingIssues.aspx

Regards, divo

0xA3
A: 

The problem might be another place. I use similar code to resize images and it works ok, but the biggest difference is that when you save the image you have to specify quality (jpeg):

ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo codec = null;
for (int i = 0; i<codecs.Length;i++)
{
  if(codecs[i].MimeType.Equals("image/jpeg"))
    codec = codecs[i];
}

EncoderParameters encoderParametersInstance = null;

if (codec!=null)
{
  Encoder encoderInstance=Encoder.Quality;
  encoderParametersInstance = new EncoderParameters(2);
  //100% quality, try different values, around 80-90 gives good results.
  EncoderParameter encoderParameterInstance=new EncoderParameter(encoderInstance, 100L);
  encoderParametersInstance.Param[0]=encoderParameterInstance;
  encoderInstance=Encoder.ColorDepth;
  encoderParameterInstance=new EncoderParameter(encoderInstance, 24L);
  encoderParametersInstance.Param[1]=encoderParameterInstance;
}

MemoryStream ms = new MemoryStream();
resizedImage.Save(ms, codec, encoderParametersInstance);
martinlund
+2  A: 

Anti-aliasing has nothing to do with raster graphics. It's only applicable to vector graphics. Obviously, an image is a raster graphic.

You need to look at InterpolationMode.

leppie
Hi, imho your definition of anti-aliasing is not totally correct. It's not a raster vs. vector graphic thing.
0xA3
<continued/>From Wikipedia: "In digital signal processing, anti-aliasing is the technique of minimizing the distortion artifacts known as aliasing when representing a high-resolution signal at a lower resolution."
0xA3
Sorry, I was wrong in the case of .Net Graphics.SmoothingMode. That property is only relevant for vector graphics.
0xA3
+5  A: 

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));

Thanks.

Nick Higgs