skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    1. Accept a lower quality
    2. Accept the larger file size
    3. Use a smarter lossy compression algorithm. Jpeg is old, and there are many newer, better, algorithms that provide better subjective quality for a given file size. Here is a comparison between a few newer formats. But do not expect any magic. You likely need to find appropriate libraries to encode and decode the images, since .net only include a basic set of encoders.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search