Title: Use the Filter property to select image files in C#
This example is mostly intended to make it easier to find these filters. I use them a lot and it's a pain to have to recreate them every time I need them.
When you click the Different Types button, the program sets an OpenFileDialog component's Filter property to the following value and then displays the dialog.
Bitmaps|*.bmp|PNG files|*.png|JPEG files|*.jpg|GIF files|*.gif|TIFF files|*.tif|Image files|*.bmp;*.jpg;*.gif;*.png;*.tif|All files|*.*
This Filter property value lets the user look for specific image types such as BMP, PNG, or JPG. It also lets the dialog show all files. (It's usually a good idea to allow the All Files option so the user can at least see all of the files in the dialog's current directory.)
When you click the All Images button, the program sets the OpenFileDialog component's Filter property to the following value and then displays the dialog.
Image files|*.bmp;*.jpg;*.gif;*.png;*.tif|All files|*.*
This Filter property value lets the user look for any image file type or all files.
When you click a button, the program sets the OpenFileDialog's Filter property to the appropriate value and then displays it. For example, the following code shows how the program responds when you click the second button.
In both cases, after the program sets the appropriate Filter property value, it sets the dialog's FilterIndex property to 0 to select the first filter. It then displays the dialog. If you select an image file and click Open, the program loads the image file an displays it.
Download the example to experiment with it and to see additional details.
|