How do I construct a pack URI to an image that is in a resource file?
I have an assembly called MyAssembly.Resources.dll, it has a folder called Ima
As Walt has correctly stated, what you get out of a resx file is a System.Drawing.Bitmap. So this needs to be converted to a System.Windows.Media.ImageSource or subtype.
I'm not sure if this falls under time wasters for you because it does not employ an URI, but here is how I get images from resx files in another library. I use a simple proxy because the resx designer file only exposes an internal constructor (even if the class is public), then define a ValueConverter that will provide the ImageSource.

AssetsProxy:
namespace MyAssembly.Resources
{
public class AssetsProxy : Images.Assets
{
public AssetsProxy() : base() { }
}
}
Bitmap to ImageSource conversion:
using System;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace MyAssembly.Resources
{
///
/// Converts a BitmapImage, as provided by a resx resource, into an ImageSource/BitmapImage
///
public class BitmapToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapImage bitmapImage = null;
if (value is System.Drawing.Image)
{
bitmapImage = ((System.Drawing.Image)value).ToBitmapImage();
}
return bitmapImage;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public static class BitmapExtensions
{
///
/// Converts the System.Drawing.Image to a System.Windows.Media.Imaging.BitmapImage
///
public static BitmapImage ToBitmapImage(this System.Drawing.Image bitmap)
{
BitmapImage bitmapImage = null;
if (bitmap != null)
{
using (MemoryStream memory = new MemoryStream())
{
bitmapImage = new BitmapImage();
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
}
return bitmapImage;
}
}
}