To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > VBForums CodeBank > CodeBank - C#

Reply Post New Thread
 
Thread Tools Display Modes
Old Jun 5th, 2005, 05:30 PM   #1
MrPolite
l33t!
 
MrPolite's Avatar
 
Join Date: Sep 01
Posts: 4,428
MrPolite is a jewel in the rough (300+)MrPolite is a jewel in the rough (300+)MrPolite is a jewel in the rough (300+)MrPolite is a jewel in the rough (300+)
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 (destImagePath, myImage, 50);
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 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;
        }

hope it's helpful
__________________
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!!

Last edited by MrPolite; Jun 5th, 2005 at 05:44 PM.
MrPolite is offline   Reply With Quote
Old Oct 25th, 2005, 03:58 AM   #2
Odanez
New Member
 
Join Date: Oct 05
Location: Germany
Posts: 1
Odanez is an unknown quantity at this point (<10)
Re: JPEG Compression -> save JPEG images with any quality

Hi,
a small problem is though, that you should use long as quality, not int. Anyway that's the only way I got it to work, got an error the way you used it...
For EncoderParameter qualityParam = new EncoderParameter (Encoder.Quality, quality); quality can be something like 50L, or casting: (long)50, or you could use (long)quality, should also work... for me that does the trick anyway
Odanez is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - C#


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:16 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.