Sometimes when scaling a bitmap, a larger file is created. What for?

I am trying to write a method to reduce the size of any image that is called 50% every time, but I found a problem. Sometimes I get a larger file size, while the image is actually only half what it was. I care about 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;
}

http://imgur.com/lY9BN.png

http://imgur.com/KSka0.png

Edit: The code is missing here:

int width = (int)(bitmap.Width * 0.5f);
int height = (int)(bitmap.Height * 0.5f);
Bitmap resizedBitmap = ResizeBitmap(bitmap, width, height);
resizedBitmap.Save(newFilename);

Edit 2: Based on your comments, this is the solution I found:

private void saveAsJPEG(string savingPath, Bitmap bitmap, long quality)
{
    EncoderParameter parameter = new EncoderParameter(Encoder.Compression, quality);
    ImageCodecInfo encoder = getEncoder(ImageFormat.Jpeg);
    if (encoder != null)
    {
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = parameter;
        bitmap.Save(savingPath, encoder, encoderParams);
    }
}

private ImageCodecInfo getEncoder(ImageFormat format)
{

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    foreach (ImageCodecInfo codec in codecs)
        if (codec.FormatID == format.Guid)
            return codec;

    return null;
}
+5
source share
7 answers

Most likely, you save a jpeg image with a low compression ratio (high quality).

+6

JPEG - , . - InterpolationMode, , . InterpolationMode.NearestNeighbor , .

+4

, , JPEG. ?

+2

, jpeg ( == )

+1

, jpegs, , # - , , . jpeg . , , jpeg, , fluke, .

, , , 100 , , 16- (2-) , 200 , .

5 5, 50 , .

+1

, JPEG. . , :

private void saveJpeg(string path, Bitmap img, long quality)
{
    EncoderParameter parameter = new EncoderParameter(Encoder.Quality, quality);
    ImageCodecInfo encoder = this.getEncoderInfo("image/jpeg");
    if (encoder != null)
    {
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = parameter;
        img.Save(path, encoder, encoderParams);
    }
}

private ImageCodecInfo getEncoderInfo(string mimeType)
{
    ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
    for (int i = 0; i < imageEncoders.Length; i++)
    {
        if (imageEncoders[i].MimeType == mimeType)
        {
            return imageEncoders[i];
        }
    }
    return null;
}
+1

, - . , , . (, , , , , .)

, (, "" ) . , , .

, , jpg algoritm , , "", ( ).

(The camera compression algorithm can also be optimized so that the input is always a photograph, while the general purpose algorithm may do some less efficient things to work well enough for both photos and line art mixed images containing both photographs , and created graphic elements.)

+1
source

All Articles