WPF Icon Previewer

Download WPFIconPreview.zip (21 KB)

I’ve begun using Xaml-based icons in my WPF applications, but I’ve found that I didn’t have any tools to preview them with. There are probably plenty of tools already available, but I couldn’t resist the challenge…

Each icon is saved in its own ResourceDictionary file as either a DrawingBrush or DrawingImage object. My application doesn’t support files with multiple icons, yet.

The window contains a TextBox, Button, ListBox and Image control. The resources are loaded like this:

private void OpenResourceFile(string path)
{
  using (FileStream fs = new FileStream(path, FileMode.Open))
  {
    ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
    Resources.MergedDictionaries.Clear();
    Resources.MergedDictionaries.Add(dic);
    string keyName = GetKey(dic.Keys);
    object res = FindResource(keyName);
    if (res is DrawingImage)
      imgPreview.Source = (DrawingImage)res;
    else
      imgPreview.Source = new DrawingImage(((DrawingBrush)res).Drawing);
  }
}

I’m assuming that only DrawingImage or DrawingBrush objects will be found – something that should be changed. Since I couldn’t find a way to access a specific index on the dic.Keys collection, the following method returns just the first key:

private string GetKey(System.Collections.ICollection iCollection)
{
  foreach (string key in iCollection)
  {
    return key;
  }
  return null;
}

Full source code attached:
Download WPFIconPreview.zip (21 KB)

Advertisements
This entry was posted in Projects and tagged , . Bookmark the permalink.

3 Responses to WPF Icon Previewer

  1. Jesse Chisholm says:

    re: accessing a specific integer index in the dictionary.Keys collection.
    If you can’t find any more direct way, this will work.

    List indexableKeys = new List( dictionary.Keys );
    for(int index=0; index < indexableKeys.Count; ++index)
    {
    Object value = dictionary[ indexableKeys[ index ] ];
    // …
    }

    -Jesse

  2. Jesse Chisholm says:

    Sigh. HTML entity parsing got me. I’ll try again with spaces around the less and greater symbols.

    List ; indexableKeys = new List ( dictionary.Keys );
    for(int index=0; index < indexableKeys.Count; ++index)
    {
    Object value = dictionary[ indexableKeys[ index ] ];
    // …
    }

  3. Jesse Chisholm says:

    Nope. Trying again with HTML entities as replacements.

    List <Object> indexableKeys = new List<Object> ( dictionary.Keys );
    for(int index=0; index < indexableKeys.Count; ++index)
    {
    Object value = dictionary[ indexableKeys[ index ] ];
    // …
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s