views:

376

answers:

2

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!

Edit:

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";
+2  A: 

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;....

If your question was more about how do you know what the "typical" image formats are (and their conventional file extensions), please leave a comment and I'll update or delete this answer.

itowlson
A: 

For images, you could get the available codecs from GDI (System.Drawing) and build your list from that with a little work. This would be the most flexible way to go.

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
Muad'Dib