Showing posts with label bitmap. Show all posts
Showing posts with label bitmap. Show all posts

Tuesday, January 10, 2012

Filter for all supported Image file types

Nifty little trick: for the OpenFileDialog, I want to set a filter for all Image file types supported by the PC, including ones that haven’t been invented yet. My code will load it into some sort of Image control for displaying. I could manually set my filter to something like “*.jpg;*.png”, etc., but then I’d have to update it whenever a new format was introduced, and some PCs may support types not supported by others, due to what has been installed on the PC.

This code below solves this problem.  It uses the MIME types that are configured for the PC, as defined in the Windows Registry.  Nice and simple solution.




  public static string GetImageFormatsFileFilter()
        {
            string retVal= "*" + string.Join(";*", SupportedImageFormats());
            return retVal;
        }
        public static string[] SupportedImageFormats()
        {
            List retVal = new List();
            RegistryKey BaseKey = Registry.ClassesRoot.OpenSubKey("MIME").OpenSubKey("Database").OpenSubKey("Content Type");
            foreach (string subkey in BaseKey.GetSubKeyNames())
            {
                if (subkey.ToUpperInvariant().StartsWith("IMAGE/"))
                {
                    string ext = (string)BaseKey.OpenSubKey(subkey).GetValue("Extension");
                    if (ext != null && !retVal.Contains(ext.ToUpperInvariant()))
                    {
                        retVal.Add(ext.ToUpperInvariant());
                    }
                }
            }
            return retVal.ToArray();
        }

Tuesday, October 26, 2010

Referencing bitmap image file in XAML.

It was driving me nuts.  I wanted to put a bitmap file into a project as a resource and reference it from XAML in another project.  I Googled the web and followed all the directions, but nothing worked.  Everything always referred to a local bitmap or a XAML file.


I followed all the directions, but nothing worked.  Basically, this is what I had: Image.png was in Resource.csproj.  UI.csproj (in the same solution) had Page.xaml that needed to display Image.png.

I could get the solution to compile, but the image would never display.  I followed all instructions to the letter, but nothing worked.  I think they're all liers, out to get me.

I finally figured it out on my own.  What I figured out even goes against some explicit instruction from one of the sites.

First, in your resource project (Resource.csproj above), add a resource file (a file ending in ".resx"--I called mine "Images.resx").  Do not use the Resources that are part of the project properties, but instead add a separate resource file.

Now add your image to that resource file.  Select the image in the resources and on the properties, change the Persistence to "Embedded in .resx".

Finally, add the ImageSource in the Resources of your XAML. In my case, my example is:

<ImageSource x:key="MyImage">pack://application:,,,/Resource;component/Images/Image.png</ImageSource>

Note that the path to the image is the resource file I added.

[EDIT]
Looks like I spoke too soon.  I still had my image in a folder that matched the same path, located in the UI project ("/Images/Image.png").  When I deleted that image, it failed.  Well, when I figure it out--if I figure it out--I'll update this with the correct information.

Too bad no one else can give good information on this--I'd really like to know.