C# 来自程序集的资源作为流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1388052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:55:48  来源:igfitidea点击:

Resource from assembly as a stream

c#wpfembedded-resource

提问by Echilon

I have an image in a C# WPF app whose build action is set to 'Resource'. It's just a file in the source directory, it hasn't been added to the app's resource collection through the drag/drop properties dialog. I'm trying to write it as a stream, but I can't open it despite trying quite a few variations of dots, slashes, namespaces and seemingly everything else.

我在 C# WPF 应用程序中有一个图像,其构建操作设置为“资源”。它只是源目录中的一个文件,尚未通过拖放属性对话框添加到应用程序的资源集合中。我试图把它写成一个流,但尽管尝试了很多点、斜线、命名空间和其他似乎所有的变体,我还是无法打开它。

I can access it to use elsewhere either in xaml with "pack://application:,,,/Resources/images/flags/tr.png", but I can't get at a stream containing it.

我可以使用“pack://application:,,,/Resources/images/flags/tr.png”在xaml中访问它以在其他地方使用,但我无法获得包含它的流。

Most places seem to say use

大多数地方似乎都说使用

using(BinaryReader reader = new BinaryReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ResourceBlenderExpress.Resources.images.flags.tr.png"))) {
    using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(imageFile))) {
        while((read = reader.Read(buffer, 0, buffer.Length)) > 0) {
            writer.Write(buffer, 0, read);
        }
        writer.Close();
    }
    reader.Close();
}

Which I haven't had any luck with.

我没有任何运气。

采纳答案by Phil Devaney

GetManifestResourceStream is for traditional .NET resources i.e. those referenced in RESX files. These are not the same as WPF resources i.e. those added with a build action of Resource. To access these you should use Application.GetResourceStream, passing in the appropriate pack: URI. This returns a StreamResourceInfo object, which has a Stream property to access the resource's data.

GetManifestResourceStream 适用于传统的 .NET 资源,即在 RESX 文件中引用的资源。这些与 WPF 资源不同,即通过资源的构建操作添加的资源。要访问这些,您应该使用Application.GetResourceStream,传入适当的包:URI。这将返回一个 StreamResourceInfo 对象,该对象具有用于访问资源数据的 Stream 属性。

回答by Darin Dimitrov

There's no need to call the Close() method, it will be automatically called by Dispose() at the end of the using clause. So your code might look like this:

不需要调用 Close() 方法,它会在 using 子句的末尾由 Dispose() 自动调用。所以你的代码可能是这样的:

using(BinaryReader reader = new BinaryReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ResourceBlenderExpress.Resources.images.flags.tr.png")))
using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(imageFile))) 
{
    while((read = reader.Read(buffer, 0, buffer.Length)) > 0) 
    {
        writer.Write(buffer, 0, read);
    }
}

回答by Frank Bollack

If I get you right, you have a problem to open the resource stream, because you do not know its exact name? If so, you could use

如果我没听错,您在打开资源流时会遇到问题,因为您不知道它的确切名称?如果是这样,你可以使用

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()

to get a list of names of all included resources. This way you can find the resource name that was assignd to your image.

获取所有包含资源的名称列表。这样您就可以找到分配给您的图像的资源名称。

回答by Thomas Levesque

You're probably looking for Application.GetResourceStream

你可能正在寻找Application.GetResourceStream

StreamResourceInfo sri = Application.GetResourceStream(new Uri("Images/foo.png"));
if (sri != null)
{
    using (Stream s = sri.Stream)
    {
        // Do something with the stream...
    }
}