Home > WPF > WPF: sample of loading an icon to an image control

WPF: sample of loading an icon to an image control

The following sample codes shows how to load an icon in to a image control. It use URI package to load the embed icon from the current assembly. For more information about URI refer to my previous post or following MSDN articles:

 

Image image1 = new Image();
Uri iconUri = new Uri(“pack://application:,,,/MyIcon.ico”, UriKind.Absolute);      
try
{
  Stream iconStream = System.Windows.Application.GetResourceStream(iconUri).Stream;
  IconBitmapDecoder decoder = new IconBitmapDecoder(iconStream,
     BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
  image1.Source = decoder.Frames[0] ;  
}
catch (System.IO.IOException)
{
  MessageBox.Show(“Icon file not found”);         
}

Advertisements
Categories: WPF
  1. April 14, 2008 at 4:26 pm

    works if you can know the URI in time to put it in your XAML.
    Your code works well from code.
    Thanks, Rob

  2. April 14, 2008 at 4:26 pm

    &lt’Image Source=”MyIcon.ico” /> should have showed up in my last comment…but was treated as an html tag?

  3. AVEbrahimi
    January 11, 2009 at 4:29 pm

    This works, but when try to load another image in the same control it says file is locked, bla bla bla

  4. January 12, 2009 at 11:19 am

    I have not faced the similar problem. My only guess is the permission issue for the file that you try to read.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: