Nel codice per caricare una risorsa nell'assembly in esecuzione in cui la mia immagine Freq.png
era nella cartella Icons
e definita come Resource
:
this.Icon = new BitmapImage(new Uri(@"pack://application:,,,/"
+ Assembly.GetExecutingAssembly().GetName().Name
+ ";component/"
+ "Icons/Freq.png", UriKind.Absolute));
Ho anche fatto una funzione:
/// <summary>
/// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'.
/// </summary>
/// <param name="pathInApplication">Path without starting slash</param>
/// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>
/// <returns></returns>
public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null)
{
if (assembly == null)
{
assembly = Assembly.GetCallingAssembly();
}
if (pathInApplication[0] == '/')
{
pathInApplication = pathInApplication.Substring(1);
}
return new BitmapImage(new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute));
}
Utilizzo (si supponga di inserire la funzione in una classe ResourceHelper):
this.Icon = ResourceHelper.LoadBitmapFromResource("Icons/Freq.png");
Nota : vedi URI MSDN Pack in WPF :
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml