How I can compress image without sacrificing its quality but i need to reduce the size whenever i copied compressed image to the another folder..?
string compressedFolderPath = @"C:UsersAdministratorDesktopVR TasksCompressiMG";
string compressedImagePath = Path.Combine(compressedFolderPath, DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpg");
using (Image originalImage = Image.FromFile(imagePath))
{
EncoderParameters encoderParameters = new EncoderParameters(0);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
originalImage.Save(compressedImagePath, jpegCodec, encoderParameters);
}
return compressedImagePath;
But here quality is missing And also i have tried As Zipfile and Gzip File and Base64 as text file but size didn’t reduced much.
2
Answers
First you need to change every pixel’s RGBA component to 255. Then you can compress it really well. 😉
And seriously just read documentation for each element you out in the code to understand how exactly it works as you typed in. Then you should yourself find out how to tune it to your needs.
Images have some amount of inherent information, or "quality". We sometimes call this entropy. However, in most images the actual entropy is smaller than the raw data size.
Lossless Compression methods like zip or png tries to reduce the file size as close to this theoretical limit, while retaining all the actual data exactly.
Lossy compression methods throw away data before the compression step. It tries to be smart about what data it throws away, but there is no way to get around the fact that you are throwing away data. Sooner or later you are going to notice a quality degradation. And if you are telling the jpeg encoder to use a low quality value you will almost certainly notice.
So your options are:
If you want to stick with jpeg you might want to experiment with lowering the resolution of your image. Jpeg does not handle high compression levels well, so you will likely get better results by reducing the resolution before compression.