Click here to Skip to main content
Click here to Skip to main content

A FileDialog.Filter generator for all supported images

, 18 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Recently, I had to write a quick app to load images into a database, and wanted to restrict the file type to just images. You can do this with the OpenFileDialog.Filter property, but it is a pain working out what file extensions to permit.
Recently, I had to write a quick app to load images into a database, and wanted to restrict the file type to just images. You can do this with the OpenFileDialog.Filter property, but it is a pain working out what file extensions to permit. This method returns a Filter compatible string built from the image formats the current system understands.
 
        /// <summary>
        /// Get the Filter string for all supported image types.
        /// This can be used directly to the FileDialog class Filter Property.
        /// </summary>
        /// <returns></returns>
        public string GetImageFilter()
            {
            StringBuilder allImageExtensions = new StringBuilder();
            string separator = "";
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            Dictionary<string, string> images = new Dictionary<string, string>();
            foreach (ImageCodecInfo codec in codecs)
                {
                allImageExtensions.Append(separator);
                allImageExtensions.Append(codec.FilenameExtension);
                separator = ";";
                images.Add(string.Format("{0} Files: ({1})", codec.FormatDescription, codec.FilenameExtension),
                           codec.FilenameExtension);
                }
            StringBuilder sb = new StringBuilder();
            if (allImageExtensions.Length > 0)
                {
                sb.AppendFormat("{0}|{1}", "All Images", allImageExtensions.ToString());
                }
            images.Add("All Files", "*.*");
            foreach (KeyValuePair<string, string> image in images)
                {
                sb.AppendFormat("|{0}|{1}", image.Key, image.Value);
                }
            return sb.ToString();
            }
 
This returns a string that (on my system) is:
 
All Images|*.BMP;*.DIB;*.RLE;*.JPG;*.JPEG;*.JPE;*.JFIF;*.GIF;*.TIF;*.TIFF;*.PNG|
BMP Files: (*.BMP;*.DIB;*.RLE)|*.BMP;*.DIB;*.RLE|
JPEG Files: (*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.JFIF|
GIF Files: (*.GIF)|*.GIF|
TIFF Files: (*.TIF;*.TIFF)|*.TIF;*.TIFF|
PNG Files: (*.PNG)|*.PNG|
All Files|*.*
 
Suitable for loading directly into the OpenFileDialog or SaveFileDialog Filter property.
 
[edit]Typos, and abstract trucated by the system. Abstract shorted, and added to main text body - OriginalGriff[/edit]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

OriginalGriff
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?
Follow on   Google+

Comments and Discussions

 
GeneralReason for my vote of 5 Nice helper app. Pinmembersashaw220-Sep-11 5:13 
GeneralReason for my vote of 5 Cool, handy little helper-routine :) PinmemberEddy Vluggen16-Sep-11 23:45 
GeneralReason for my vote of 5 Nice reference PinsubeditorWalt Fair, Jr.16-Sep-11 18:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Sep 2011
Article Copyright 2011 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid