Results 1 to 2 of 2

Thread: JPEG Compression -> save JPEG images with any quality

Threaded View

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    JPEG Compression -> save JPEG images with any quality

    (VB version of this article is here )

    Here's the code! Too many people ask for this
    Using this code you can save an image (System.Drawing.Image) item as a jpeg image, specifying the quality of it. Jpeg quality is the same as compression so the lower the quality, the smaller the file size. It's simple to use, ie just do this:

    PHP Code:
    Image myImage //... load the image somehow
    // Save the image with a quality of 50%
    SaveJpeg (destImagePathmyImage50); 
    PHP Code:
    // Please do not remove :)
    // Written by Kourosh Derakshan
    //

    //add this!
    using System.Drawing.Imaging;

                              
    /// <summary>
            /// Saves an image as a jpeg image, with the given quality
            /// </summary>
            /// <param name="path">Path to which the image would be saved.</param>
            // <param name="quality">An integer from 0 to 100, with 100 being the
            /// highest quality</param>
            
    public static void SaveJpeg (string pathImage imgint quality)
            {
                if (
    quality<0  ||  quality>100)
                    throw new 
    ArgumentOutOfRangeException("quality must be between 0 and 100.");

                
                
    // Encoder parameter for image quality
                
    EncoderParameter qualityParam 
                    new 
    EncoderParameter (Encoder.Qualityquality);
                
    // Jpeg image codec
                
    ImageCodecInfo   jpegCodec GetEncoderInfo("image/jpeg");

                
    EncoderParameters encoderParams = new EncoderParameters(1);
                
    encoderParams.Param[0] = qualityParam;

                
    img.Save (pathjpegCodecencoderParams);
            }

            
    /// <summary>
            /// Returns the image codec with the given mime type
            /// </summary>
            
    private static ImageCodecInfo GetEncoderInfo(string mimeType)
            {
                
    // Get image codecs for all image formats
                
    ImageCodecInfo[] codecs ImageCodecInfo.GetImageEncoders();

                
    // Find the correct image codec
                
    for(int i=0i<codecs.Lengthi++)
                    if(
    codecs[i].MimeType == mimeType)
                        return 
    codecs[i];
                return 
    null;
            } 

    hope it's helpful
    Last edited by MrPolite; Jun 5th, 2005 at 05:44 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width