Image quality is not good
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:
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;
}
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.
下一篇: 图像质量不好