Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Compressing a jpg file

Compressing a jpg file

Scheduled Pinned Locked Moved C#
csharp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GazzaJ
    wrote on last edited by
    #1

    I am trying to compress a jpg file in the same manner that you would set the quality factor in an image processing package e.g. Open image1.jpg Set quality factor to 50 Save as image2.jpg This is to reduce the size of the jpg files in my application. Does anyone know if there are .NET libraries for this or any good commercial toolkits.

    R 1 Reply Last reply
    0
    • G GazzaJ

      I am trying to compress a jpg file in the same manner that you would set the quality factor in an image processing package e.g. Open image1.jpg Set quality factor to 50 Save as image2.jpg This is to reduce the size of the jpg files in my application. Does anyone know if there are .NET libraries for this or any good commercial toolkits.

      R Offline
      R Offline
      Russell Jones
      wrote on last edited by
      #2

      System.Drawing.Imaging.EncoderParameters encParams = new System.Drawing.Imaging.EncoderParameters(); encParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality); MemoryStream ms = new MemoryStream(); _reformattedImage.Save(ms, System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1], encParams); _reformattedImage = Image.FromStream(ms); That's raw code straight out of an app i've got infront of me it's got the quality methods you'll need though. Quality is the parameter from 1 to 100 that sets the quality level Things like Bitmap should have load and save methods on them (you can load jpegs into bitmap objects). HTH Russ

      G 1 Reply Last reply
      0
      • R Russell Jones

        System.Drawing.Imaging.EncoderParameters encParams = new System.Drawing.Imaging.EncoderParameters(); encParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality); MemoryStream ms = new MemoryStream(); _reformattedImage.Save(ms, System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1], encParams); _reformattedImage = Image.FromStream(ms); That's raw code straight out of an app i've got infront of me it's got the quality methods you'll need though. Quality is the parameter from 1 to 100 that sets the quality level Things like Bitmap should have load and save methods on them (you can load jpegs into bitmap objects). HTH Russ

        G Offline
        G Offline
        GazzaJ
        wrote on last edited by
        #3

        Thanks Russell. For completeness my final solution was: // Method to get the apprpriate encoder private ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for(j = 0; j < encoders.Length; ++j) { if(encoders[j].MimeType == mimeType) return encoders[j]; } return null; } // Compress file code Image image = Image.FromFile(sourceFile); string mimeType = "image/jpeg"; ImageCodecInfo codecInfo = GetEncoderInfo(mimeType); if (codecInfo == null) raise error; // Set the quality (must be a long) Encoder qualityEncoder = Encoder.Quality; EncoderParameter ratio = new EncoderParameter(qualityEncoder, 40L); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = ratio; image.Save(destFile, codecInfo, encoderParams); image.Dispose();

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups