Geeks With Blogs
Mayank Sharma

Found a nice post by Mr.Polite for compressing JPEG images. Here's the link:

http://www.vbforums.com/showthread.php?p=2038463

Posting the code here for my reference in case the original post gets deleted or changed.

Image myImage = //... load the image somehow
// Save the image with a quality of 50%
SaveJpeg (destImagePath, myImage, 50);
//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 path, Image img, int 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.Quality, quality);
            
// Jpeg image codec
            
ImageCodecInfo   jpegCodec = GetEncoderInfo("image/jpeg");

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

            
img.Save (path, jpegCodec, encoderParams);
        }

        
/// <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=0; i<codecs.Length; i++)
                if(
codecs[i].MimeType == mimeType)
                    return
codecs[i];
            return
null;
        }

 

Posted on Wednesday, April 29, 2009 8:15 AM C# , ASP.NET | Back to top


Comments on this post: Compress Image files using C#

# re: Compress Image files using C#
Requesting Gravatar...
quasi valido!!
Left by ennio on Jan 08, 2011 11:44 AM

# re: Compress Image files using C#
Requesting Gravatar...
Very usefull Code snippet, Thanks.
Left by Shibby Development on Aug 30, 2011 10:23 AM

# re: Compress Image files using C#
Requesting Gravatar...
Hello. I am using this code for compress the image, but i face an exception like this "A generic error occurred in GDI+". Please tell me about solution for this. Thanks.
Left by Anshu sharma on Oct 15, 2011 11:13 AM

# re: Compress Image files using C#
Requesting Gravatar...
Hello. I am using this code for compress the image, but i face an exception like this "A generic error occurred in GDI+". Please tell me about solution for this.
Left by Anshu sharma on Oct 15, 2011 11:14 AM

# re: Compress Image files using C#
Requesting Gravatar...
I am not sure what exactly is the problem but a quick internet search pointed me towards permission issues for ASP.NET worker process. See if changing permissions helps.
Left by Mayank Sharma on Oct 16, 2011 8:49 AM

# re: Compress Image files using C#
Requesting Gravatar...
Thanks for the code! Works great
Left by Keyword tool reviews on Mar 26, 2012 10:14 AM

# re: Compress Image files using C#
Requesting Gravatar...
thank you so much,it is so helpful code
god bless you
Left by Dany Wehbe on May 06, 2012 2:29 AM

# re: Compress Image files using C#
Requesting Gravatar...
May i know any idea how about decompress image file?
thanks
Left by nhy on May 28, 2012 1:48 AM

# re: Compress Image files using C#
Requesting Gravatar...
Thanks for posting this code and helping us code
Left by HighFlying on Jun 07, 2012 6:36 AM

# How to find the Compression type of Image files using C#
Requesting Gravatar...
I have extracted all images from PDF files and now I want to check the compression and resolution of every individual image one by one. Whatever I have extracted from PDF earlyer
Left by Sat Kumar on Jun 19, 2012 6:33 AM

# re: Compress Image files using C#
Requesting Gravatar...
gadhi kuch accha dal,
ka article dala h
Pagaliya......

kuch accha dal wait kr rhi hu.......
Left by tinki on Jul 10, 2012 3:58 PM

# re: Compress Image files using C#
Requesting Gravatar...
maaf kr de gadhi kahaa,
gadha h tu....
pagla............

Left by tinki on Jul 10, 2012 4:00 PM

Your comment:
 (will show your gravatar)


Copyright © bullpit | Powered by: GeeksWithBlogs.net | Join free