0

Problem:

I found this function here a few days ago but I couldn't find it again. It resizes images but the output quality is not good. It looks like the color depth is 8 bit.

First photo is the original, 2nd is Photoshopped and the last one is resized by the code:

Samples:

Resize samples

Function:

Image ResizeImage(Image original, int targetWidth)
{
    double percent = (double)original.Width / targetWidth;
    int destWidth = (int)(original.Width / percent);
    int destHeight = (int)(original.Height / percent);

    Bitmap b = new Bitmap(destWidth, destHeight);
    Graphics g = Graphics.FromImage((Image)b);
    try
    {

        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        g.DrawImage(original, 0, 0, destWidth, destHeight);
    }
    finally
    {
        g.Dispose();
    }

    return (Image)b;
}
Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
Nime Cloud
  • 6,162
  • 14
  • 43
  • 75
  • 2
    Maybe this question and its answers helps you. http://stackoverflow.com/questions/249587/high-quality-image-scaling-c – Jan K. Sep 13 '11 at 08:02
  • 1
    Shouldn't you be creating your `Graphics` object from `original`? `Graphics g = Graphics.FromImage(original)` – Jodrell Sep 13 '11 at 08:07

1 Answers1

1

Looks like the image being transformed to indexed-color pixel format at some stage. Check this article and try to set up PixelFormat and Resolution properties explicitly.

Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
  • Bitmap b = new Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); would be help? I'll try... – Nime Cloud Sep 13 '11 at 08:08