I've got the following code below. High level overview is that it is a coverter that takes a .emf file from a file share, and then converts it into something WPF can use for Image.Source:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fileName = (string)value;
if (fileName == null)
return new BitmapImage();
using (var stream = File.Open(fileName, FileMode.Open))
{
return GetImage(stream);
}
}
internal BitmapImage GetImage(Stream fileStream)
{
var img = Image.FromStream(fileStream);
var imgBrush = new BitmapImage();
imgBrush.BeginInit();
imgBrush.StreamSource = ConvertImageToMemoryStream(img);
imgBrush.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
imgBrush.EndInit();
return imgBrush;
}
public MemoryStream ConvertImageToMemoryStream(Image img)
{
var ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms;
}
Now, all is well and good here. The users are going to need a "print calibration" page, so I included a "SampleDoc.emf" file into my application and marked it as a resource.
However, I can't seem to get the File.Open() part right when pointing to that resource file. Any ideas on how I can do this?