37. Compressing images

One of the key pieces of this solution is the following extension method, which saves an image in JPG format with a specified compression level:

// Save a JPG file with the indicated compression level.
public static void SaveCompressed(this Image image, string filename,
int compressionLevel)
{
// Make an object to hold one encoder parameter.
EncoderParameters parameters = new EncoderParameters(1);

// Place the compression level in that parameter.
parameters.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, compressionLevel);

// Get the JPG codec.
ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");

// Create the file, deleting it if it already exists.
if (File.Exists(filename)) File.Delete(filename);
image.Save(filename, codecInfo, parameters);
}

This method first creates an EncoderParameters object to hold a single parameter, which will be used by an image encoder. It sets that parameter's category to Quality and sets its value to the desired compression level.

The Quality parameter uses a fully qualified namespace because without that there would be ambiguity between System.Drawing.Imaging.Encoder and System.Text.Encoder.

The
Quality parameter's value should be between 0 (the most compression) and 100 (the least compression).

Next, the code creates codec information for the image/jpeg mime type.

A codec, which is short for coder decoder, is a program or object that encodes and decodes a stream of data. In this example, it encodes and decodes JPG files.

 

MIME stands for Multipurpose Internet Mail Extensions. As I'm sure you can guess, image/jpg is the mime type for image files in the JPG format.

If the file already exists, the method deletes it. It then calls the image's Save method, passing it the ImageCodecInfo object and EncoderParameter to tell it the compression level.

The following code fragment shows how the program uses the SaveCompressed method:

// The name of the temporary file.
private static string tempfile = "__temp.jpg";
...
// Process a newly loaded file.
private void ProcessFile(Image image)
{
if (image == null) return;

// Save the original image.
OriginalImage = image;

// Save the image in a temporary file with
// the current compression level.
image.SaveCompressed(tempfile, compressionScrollbar.Value);

// Reload the file.
compressedPictureBox.Image = LoadImageWithoutLocking(tempfile);

// Display the file compression level.
compressionLabel.Text = compressionScrollbar.Value.ToString();

// Display the file's size.
FileInfo fileinfo = new FileInfo(tempfile);
fileSizeLabel.Text = fileinfo.Length.ToFileSize();
}

This method stores the image in the OriginalImage variable so that it can retrieve the original image later. It then calls the SaveCompressed method to save the image in a temporary file at the level of compression selected by the compressionScrollbar control. The code then reloads the image from the file and displays it in the compressedPictureBox control. The result shows the image in its compressed form.

The method finishes by displaying the compressed file's size in a label so that you can see how much space the file occupies.

Download the CompressImages example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.164.210