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:
- http://msdn2.microsoft.com/en-au/library/aa970069.aspx
- http://msdn2.microsoft.com/en-au/library/aa970494.aspx
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”);
}
Categories: WPF
works if you can know the URI in time to put it in your XAML.
Your code works well from code.
Thanks, Rob
<’Image Source=”MyIcon.ico” /> should have showed up in my last comment…but was treated as an html tag?
This works, but when try to load another image in the same control it says file is locked, bla bla bla
I have not faced the similar problem. My only guess is the permission issue for the file that you try to read.