Get Image From Resource (WPF)
CodeKeep C# Feed Agosto 5th, 2008
Description: This describe how to get image from resource file and shows usageLink: http://www.codekeep.net/snippets/b4cafe76-0ca7-4548-a70c-2c181d9e4167.aspx
public static T loadResource<T>(string path)
{
T c = default(T);
StreamResourceInfo sri = Application.GetResourceStream(new Uri(path, UriKind.Relative));
if (sri.ContentType == "application/xaml+xml")
c = (T)XamlReader.Load(sri.Stream);
else if (sri.ContentType.IndexOf("image") >= 0)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = sri.Stream;
bi.EndInit();
if (typeof(T) == typeof(ImageSource))
c = (T)((object)bi);
else if (typeof(T) == typeof(Image))
{
Image img = new Image();
img.Source = bi;
c = (T)((object)img);
}
}
sri.Stream.Close();
sri.Stream.Dispose();
return c;
}