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.