Using File.Open for a WPF Resource

I have the following code below. The high level of review is that it is a close that takes the .emf file from the file share, and then converts it to something that 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. Users will need the "print calibration" page, so I included the "SampleDoc.emf" file in my application and marked it as a resource.

However, it seems that I cannot get the File.Open () part correctly, pointing to this resource file. Any ideas on how I can do this?

+3
source share
1 answer

"SampleDoc.emf" , ( ). LightSwitch, , .

// creates a StreamReader from the TestFile.txt
StreamReader sr = new StreamReader(assembly.GetManifestResourceStream("SomeFile.txt"));

.

BuildOption "" " " " " " ", .

+5

All Articles