OpenFileDialog extensions similar to MS Paint SaveFileDialog

I am working on a WPF application where users will be able to upload photos. I wrote the following code for file extensions.

    OpenFIleDialog.Filter = "JPEG Images|*.jpg|PNG Images|*.png|GIF Images|*.gif|BITMAPS|*.bmp|TIFF Images|*.tiff|TIFF Images|*.tif";

When saving the file in the ms mask, we have the parameters as shown below. enter image description here

here we see that the same format (.bmp and .dib) is used for 4 options.

My question is: this can be done using OpenFileDialog. If so, how?

+3
source share
1 answer

Its pretty simple, just add your filter like this

openFileDialog.Filter = "Office Files(Document or Excel)|*.doc;*.docx;*.xlsx;*.xls|Word Document(*.doc *.docx)|*.doc;*.docx";
var result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                var selectedFile = openFileDialog.FileName;
                var filterIndex = openFileDialog.FilterIndex;
                if(filterIndex == 1)
                { 
                   /* Code to perform if first filter (Office files in this case) is selected */ 
                }
                else if (filterIndex == 2)
                { 
                   /* Code to perform if second filter (Word Document in this case) is selected */
                }

Here you can see that * .doc and * .docx are repeated. Therefore, based on the selected value, you can decide which encoding (in your case) to apply.

+5
source

All Articles