LossLess JPG compression with C#

Hi,

I use the code below to compress JPG images. This code works fine, only I noticed that I lost all the EXIF info. I understand that that is a part of the compression, but how can I make it save some EXIF info I need to handle the images later? I need a few EXIF properties to stay in the compressed image.

THx for any help.
public void CompressImg(FileInfo file, bool copyDir)
    {
      Bitmap _bmp = null;

      // original folder
      _folder = Path.GetDirectoryName(file.FullName);

      try
      {
        // copy directory structure
        if (copyDir)
        {
          // mirror folder
          _mirrorFolder = Path.GetDirectoryName(file.FullName).Replace(_inputFolder, _outputFolder);
        }
        // do not copy directory structure
        else
        {
          // mirror folder
          _mirrorFolder = _outputFolder;
        }

        if (!Directory.Exists(_mirrorFolder))
        {
          try
          {
            Directory.CreateDirectory(_mirrorFolder);
            _logger.Info("Created directory: " + _mirrorFolder);
          }
          catch (Exception exDir)
          {
            throw new ImageModifierException("\n\rThe directory to create excess 248 chars: " + _mirrorFolder + "\n\r \n\r"
              + exDir.Message);
          }
        }

        if (file.Extension.ToLower() == ".jpg")
        {

          // Start compression
          if (_bmp != null)
          {
            _bmp.Dispose();
            _bmp = null;
          }
           _bmp = new Bitmap(file.FullName) ;

          ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
          System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
          EncoderParameters myEncoderParameters = new EncoderParameters(1);
          EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, _imageQuality);
          myEncoderParameters.Param[0] = myEncoderParameter;

          string _fileName = file.Name;
          string _newPath = Path.Combine(_mirrorFolder, file.Name);

          if (_newPath.Length < 260)
          {
            // save compressed image
            _bmp.Save(_newPath, jgpEncoder, myEncoderParameters);
            // log
            _logger.Info("Compressed: " + _newPath);
            _bmp.Dispose();
          }
          else
          {
            // log
            _logger.Warn("Skipped, path and/or file name too long: " + _newPath);
          }
        }
      }
      catch (Exception ex)
      {
        throw new ImageModifierException("ImageModifierException CompressImg(): " + ex.Message);
      }
    }

Open in new window

Pit76Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

8080_DiverCommented:
How about reading and capturing the EXIF data and then adding it back when you complete your process?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pit76Author Commented:
Yes, that was what I was looking into in the mean time because I couldn't find any solution to this on the Net.
And judging from the few anwers I got here I think it's not so simple.
i found this http://code.google.com/p/exiflibrary/wiki/ExifLibrary on the Net so I think I'll give it a try. Or is there an other way?
8080_DiverCommented:
Just out of curiosity, do you have any sort of database associated with this activity?  (If so, you could store the EXIF data in there along with a link to the resultant JPEG.)
I don't think that there is really much of an alternative though.
Pit76Author Commented:
It's a small program to compress JPG files, without a db.
Just select input and output folder, set the compress value and check the preview box if you wish. Then press Start :)
I use this a few months, but now I needed the EXIF info to check something and II only noticed now that I lost the EXIF info in the process.

I have attached a screenshot of my program.
Image-Compressor.jpg
Pit76Author Commented:
I did it like this because I couldn't find any other solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.