从程序集资源作为流 [英] Resource from assembly as a stream

查看:88
本文介绍了从程序集资源作为流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#WPF应用程序的构建操作设置为资源的图像。这只是在源目录中的文件,它并没有被通过拖/放属性对话框添加到应用程序的资源集合。我试图写一个流,但我不能,尽管尝试点了不少变化打开它,斜线,命名空间,看似一切。

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.

我可以访问它与其他地方无论是在XAML中使用包://应用:,,, /资源/图像/标志/ tr.png,但我不能在包含它的流得到。

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.

大多数地方好像说用

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.

推荐答案

GetManifestResourceStream是传统的.NET资源,即那些RESX文件中引用。这些都是不一样的WPF资源,即那些有资源的生成操作增加。要访问这些,你应该使用 Application.GetResourceStream ,传递适当的包装:URI。这会返​​回一个对象StreamResourceInfo,其中有一个Stream属性访问资源的数据。

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.

这篇关于从程序集资源作为流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆