Page 5 of 6
Converting the list
At this point we have an application that loads one of the pictures specified and allows the user to view it at a thumbnail size controlled by the slider. When the user clicks on the Save All button we have to process all of the command line arguments.
The first question is where to store the thumbnails?
They could be stored back in the same directory with changed file names but my preferred solution is to create a “Thumbs” sub-directory and store the thumbnails using the same name as the file that they derived from.
Creating a directory is easy as we already have the path name of the directory the pictures are stored in:
private void button1_Click(
object sender, EventArgs e)
string thumbdir = m_dir + @"\Thumbs\";
The next task might seem a little out of sequence but we have to get ready to save the thumbnails in a suitable compressed format. You might think that saving a bitmap as a JPEG was such a common job that it should be easy. It has been improved in .NET 3.0, but it’s still not easy – not much easier than the original .NET 2.0 solution presented here.
The method is to get a suitable encoder object, customise using an EncoderParamters object and then use it as part of the Bitmap Save method.
The first problem is getting a suitable encoder.
We need to write a function, GetEncoderInfo that will find the Encoder appropriate to the image format we want to use. The GetImageEncoders method returns an array of ImageCodeInfo objects – one for each available encoder. Once we have this it’s easy to scan for the encoder we want:
public static ImageCodecInfo
encoders = ImageCodecInfo.
The GetEncoderInfo function will return an ImageCodecInfo object that represents the encoder that matches the graphics type you specify, this case ImageFormat.Jpeg.
You can search for encoders based on the file extensions that they handle or on the encoders description or on the MimeType but using the ImageFormat seems the most direct in this case.
Notice that the function returns a null if it can’t find an encoder of the correct type.
Now that we have this function we can return to finish the Button click event handler and get the encoder we need:
ImageCodecInfo jpegCodec =
To control the way the encoder works we need to use an EncoderParameters object – this is really just an array of parameters. You can set any number of parameters and fine tune the way the image is saved to disk, but in this case we only need to set one – Quality.This takes values between 0 and 100 with 100 meaning full quality, i.e. no compression, and 0 meaning lowest quality, i.e. full compression. A compression of 50 provides a very small file size (256x256 pixels takes typically 10Kbytes) with reasonable quality:
EncoderParameters eps =
eps.Param = new EncoderParameter(
With the encoder ready to do its job we can now step through each of the files selected by the user and save them to the newly created thumbnail:
for (int i = 1; i < m_args.Length; i++)
temp = getImage(m_args[i]);
thumbnail = thumb(temp, m_w, m_h);
The only thing left now is to code the Cancel button’s event handler which is:
private void button3_Click(
object sender, EventArgs e)