3

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?

4

1 回答 1

5

当您将“SampleDoc.emf”标记为资源时,它仅驻留在已编译的程序集中(简单地说)。请参阅我提出的在 LightSwitch中获取其他文件并回答了可以回答您的问题的类似问题。

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

使用此代码,您可以访问您的资源。

另一种方法是将文件的 BuildOption 标记为“内容”并将“复制操作”设置为“始终复制”或“仅当更新时”,然后在构建项目时将文件复制到输出目录。

于 2012-04-24T20:45:52.553 回答