Sergio Tapia Sergio Tapia - 16 days ago 4x
C# Question

Setting the filter to an OpenFileDialog to allow the typical image formats?

I have this code, how can I allow it to accept all typical image formats? PNG, JPEG, JPG, GIF?

Here's what I have so far:

public void EncryptFile()
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.InitialDirectory = @"C:\";
dialog.Title = "Please select an image file to encrypt.";
if (dialog.ShowDialog() == DialogResult.OK)
//Encrypt the selected file. I'll do this later. :)

Notice that the filter is set to .txt files. I could change to PNG, but what of the other types?

Thanks SO!


I figure it out, this is how you do it:

dialog.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";


From the docs, the filter syntax that you need is as follows:

Office Files|*.doc;*.xls;*.ppt

i.e. separate the multiple extensions with a semicolon -- thus, Image Files|*.jpg;*.jpeg;*.png;....