Reduce any image size

Marco picture Marco · Sep 14, 2012 · Viewed 20.4k times · Source

The Plan

I need to reduce image sizes, while keeping some quality. However, I am accepting GIF, JPEG, JPG and PNG.


The Current Situation

The uploading of the image is compleetly functional, and I have been able to scale its width and/or height with sucess.

I found an awesome post here on Stackoverflow, that gave me a very good sample of code:

Image myImage = //... load the image somehow 

// Save the image with a quality of 50% SaveJpeg (destImagePath, myImage, 50); //add this! using System.Drawing.Imaging;

                      /// <summary> 
    /// Saves an image as a jpeg image, with the given quality 
    /// </summary> 
    /// <param name="path">Path to which the image would be saved.</param> 
    // <param name="quality">An integer from 0 to 100, with 100 being the 
    /// highest quality</param> 
    public static void SaveJpeg (string path, Image img, int quality) 
    { 
        if (quality<0  ||  quality>100) 
            throw new ArgumentOutOfRangeException("quality must be between 0 and 100."); 


        // Encoder parameter for image quality 
        EncoderParameter qualityParam = 
            new EncoderParameter (Encoder.Quality, quality); 
        // Jpeg image codec 
        ImageCodecInfo   jpegCodec = GetEncoderInfo("image/jpeg"); 

        EncoderParameters encoderParams = new EncoderParameters(1); 
        encoderParams.Param[0] = qualityParam; 

        img.Save (path, jpegCodec, encoderParams); 
    } 

    /// <summary> 
    /// Returns the image codec with the given mime type 
    /// </summary> 
    private static ImageCodecInfo GetEncoderInfo(string mimeType) 
    { 
        // Get image codecs for all image formats 
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 

        // Find the correct image codec 
        for(int i=0; i<codecs.Length; i++) 
            if(codecs[i].MimeType == mimeType) 
                return codecs[i]; 
        return null; 
    } 

However, this code seems to work with JPEG images only.


The Questions

  1. Is JPEG format lighter than PNG or GIF? And if so, should I convert and save all images in JPEG?
    • In this case, the sample code should work just fine right?
    • If not, given the sample code, what can we do use other formats?
  2. What tools can we use in C# to reduce the image weight? (besides reducing the dimensions)
  3. What is the recommended "quality loss" for an image, in order to preserve a reasonable status of readability, color, sharpness and definition?
  4. Is image processing "heavy" on the server?

Answer

bmm6o picture bmm6o · Sep 14, 2012

First of all, JPEG uses a "lossy" compression algorithm, which is why you can tune the quality. Lower quality will result better compression at the cost of higher data loss. Also realize that the JPEG format is designed for photograph-like images, and applying JPEG to other types of images can be disappointing (google "jpeg artifacts").

PNG and GIF are lossless, so they can't support a quality parameter. This isn't to say that you can't get better compression out of them - there's a variety of tools to reduce the size of your PNG's (pngcrush, punypng, etc), but these tools (mostly) work in a fundamentally different way than JPEG compressors do. One thing to note is that GIF supports a smaller palette than PNG - only up to 256 colors.

So to answer your questions in order:

  1. Can't really compare, since compression in JPEG comes with a tradeoff while compression in PNG and GIF doesn't, and how tolerable the tradeoff is depends on the type of image. That is, JPEG will often give better compression, but if the image isn't a photograph the result will be noticeably worse.

  2. Your code for JPEG is fine. Google for C#-compatible tools for PNG.

  3. You'll need to experiment with the types of images you expect.

  4. Yes.