0

I have a C# desktop application.

I constantly get jpeg images of the same size and resolution.

Before I upload to my web server I compress the image by reducting the quality.

This is the code the reduces the quality:

     public byte[] SaveJpeg(Image img, int quality)
    {
        byte[] data = null;
        // Encoder parameter for image quality 
        EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        // Jpeg image codec 
        ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, jpegCodec, encoderParams);
            data = ms.ToArray();
            img.Dispose();
        }
        return data;
    }


    private  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;
    } 

     //I call it thus...
     byte[] reducedSize = SaveJpeg(LatestImage, 50L)

What I have found that the size of the bytes/images can greatly vary. I have noticed when there is a lot of green in the image and the day is sunny that the compression returns twice as many bytes as a darker less green image.

Is there a better way to do what I am trying to achieve? Perhaps using a different colour space?

Any advice would be welcome.

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • 1
    The more sun the more information is in a photograph; (without overblowing..) I doubt that different colorspaces would help, but you could play around with it. If you need to keep all files below a threshold you may have to check on the resulting size and if necessary lower the quality further.. – TaW Apr 03 '14 at 09:59
  • @TaW Hi, thanks for your comment. This was the conclusion I was reaching myself. I thought I would pose the question here to see if my education was lacking - thanks – Andrew Simpson Apr 03 '14 at 10:00
  • 1
    If image has increased contrast jpeg algorithm must use more bits to convey the information. Actually not so much contrast as color difference between neighboring pixels. – Dialecticus Apr 03 '14 at 10:28

0 Answers0