Ech*_*lon 30 c# wpf embedded-resource
我在C#WPF应用程序中有一个图像,其构建操作设置为"资源".它只是源目录中的一个文件,尚未通过拖放属性对话框添加到应用程序的资源集合中.我试图把它写成一个流,但我无法打开它,尽管尝试了很多点,斜线,命名空间和其他一切的变化.
我可以访问它以在xaml中使用"pack:// application:,,,/Resources/images/flags/tr.png",但我无法获取包含它的流.
大多数地方似乎都在说使用
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();
}
Run Code Online (Sandbox Code Playgroud)
哪个我没有运气.
Tho*_*que 27
你可能正在寻找 Application.GetResourceStream
StreamResourceInfo sri = Application.GetResourceStream(new Uri("Images/foo.png"));
if (sri != null)
{
using (Stream s = sri.Stream)
{
// Do something with the stream...
}
}
Run Code Online (Sandbox Code Playgroud)
Phi*_*ney 26
GetManifestResourceStream用于传统的.NET资源,即RESX文件中引用的资源.这些与WPF资源不同,即那些添加了Resource的构建操作的资源.要访问这些,您应该使用Application.GetResourceStream,传入适当的pack:URI.这将返回一个StreamResourceInfo对象,该对象具有Stream属性以访问资源的数据.
如果我说得对,那么打开资源流会有问题,因为你不知道它的确切名称?如果是这样,你可以使用
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
Run Code Online (Sandbox Code Playgroud)
获取所有包含资源的名称列表.这样,您就可以找到分配给图像的资源名称.