10

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?

Nick Higgs
  • 1,712
  • 1
  • 18
  • 21

4 Answers4

21

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.

anon
  • 4,578
  • 3
  • 35
  • 54
Nick Higgs
  • 1,712
  • 1
  • 18
  • 21
2

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

Firas Assaad
  • 25,006
  • 16
  • 61
  • 78
2

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
  • 115,091
  • 17
  • 196
  • 297
  • 1
    Hi, imho your definition of anti-aliasing is not totally correct. It's not a raster vs. vector graphic thing. – Dirk Vollmar Dec 02 '08 at 11:22
  • 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." – Dirk Vollmar Dec 02 '08 at 11:23
  • 1
    Sorry, I was wrong in the case of .Net Graphics.SmoothingMode. That property is only relevant for vector graphics. – Dirk Vollmar Dec 02 '08 at 11:25
0

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
  • 206
  • 2
  • 6