Search
Thursday, March 01, 2001 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Disclosure  
     
  
   TechTidBits (Blog)  

Why is it so much work to put an Icon on a WPF form?

Nov 3

Written by:
Tuesday, November 03, 2009 10:05 PM  RssIcon

I know learning something new always has it's hiccups, but this is crazy.  Try this on for size, try creating a WPF form and making your already created icon the WPF forms icon.  Try it, I dare you.  Frustrated yet?  Read on for the answer.

Learning WPF takes a bit of getting used to.  Everything's in panels, XAML, EF to get data, etc.  But something that struck me odd was the amount of work required to use an icon in a resource.  Check this out, here's a simple WinForms app with a Habs icon.

WinForms and Icon

Check out the code required to do this.

this.Icon = IconsInWinForms.Properties.Resources.Habs;

That's it, one line.  (Yes, I could have used the Form's Icon property as well, but this works better to compare'n'contrast for my example.

Next, we'll look at the same thing in WPF.

WPF and Icon

Aside from the subtle differences in UI and the comments, it's very similar, EXCEPT for the code required to put the same icon on the form.

      System.Drawing.Icon formIcon = IconsInWPF.Properties.Resources.Habs;
      MemoryStream strm = new MemoryStream();
      formIcon.Save( strm );
      IconBitmapDecoder ibd = new IconBitmapDecoder( strm, BitmapCreateOptions.None, BitmapCacheOption.Default );
      Icon = ibd.Frames[0];

Two things to note here, the Icon is fully qualified cause WPF has it's own version of that and therefore to use the old ICO files you have to fully qualify these, AND it's litteraly five times longer to do the exact samething in WPF as it did in WinForms.  HUH?

Something else to note here, if you google this, one of the entries you'll find is from stackoverflow.  I highly respect the people over at stackoverflow and love it when their links come up in my searching, but this time I think they're off the mark.  My fear here is going out to unmanaged code (using Interop code) to something I think should be as simple as setting a property.

What's the heart of this?  Icon vs ImageSource.  Windows Forms have a Form.Icon property while WPF's Window.Icon property is an ImageSource.

 After poking around a bit, I've been able to cut things down to four Lines, not perfect but it's an improvement, here's the updated code (included in source).

      System.Drawing.Icon formIcon = IconsInWPF.Properties.Resources.Habs;
      MemoryStream strm = new MemoryStream();
      formIcon.Save( strm );
      this.Icon = BitmapFrame.Create( strm );

Now, I completely agree WPF is most certainly a step in the right and forward direction.  BUT come on, REALLY?  Making us do all this work for a simple form's icon?  REALLY?

Now that's off my chest, it's time to grab a coffee and get coding!

 

Code: http://www.pchenry.com:8080/svn/blog/trunk/2009/IconsInWinFormsAndWPF

 

BONUS: Need a place to get free icons?  Try these out for size (at the time of writting this, these were free, but this could change in time, who knows?)

iconbuffet: http://www.iconbuffet.com/

iconaholic: http://www.iconaholic.com/

 

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

1 comment(s) so far...


Re: Why is it so much work to put an Icon on a WPF form?

David Bitton found this link and even tweeted about it. :>
http://twitter.com/davidbbitton
Thanks David! Glad I could help, hope you come back soon. :>

By phenry on   Tuesday, April 06, 2010 12:18 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement