Sometimes, scaling down a bitmap generates a bigger file. Why?
Posted
by Matías
on Stack Overflow
See other posts from Stack Overflow
or by Matías
Published on 2010-03-24T13:10:35Z
Indexed on
2010/03/24
13:13 UTC
Read the original article
Hit count: 252
Hello,
I'm trying to write a method to reduce the size of any image 50% each time is called but I've found a problem. Sometimes, I end up with a bigger filesize while the image is really just half of what it was. I'm taking care of DPI and PixelFormat. What else am I missing?
Thank you for your time.
public Bitmap ResizeBitmap(Bitmap origBitmap, int nWidth, int nHeight)
{
Bitmap newBitmap = new Bitmap(nWidth, nHeight, origBitmap.PixelFormat);
newBitmap.SetResolution(origBitmap.HorizontalResolution, origBitmap.VerticalResolution);
using (Graphics g = Graphics.FromImage((Image)newBitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(origBitmap, 0, 0, nWidth, nHeight);
}
return newBitmap;
}
© Stack Overflow or respective owner