Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource from assembly as a stream

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.

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.

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.

like image 486
Echilon Avatar asked Sep 07 '09 07:09

Echilon


3 Answers

You're probably looking for Application.GetResourceStream

StreamResourceInfo sri = Application.GetResourceStream(new Uri("Images/foo.png"));
if (sri != null)
{
    using (Stream s = sri.Stream)
    {
        // Do something with the stream...
    }
}
like image 137
Thomas Levesque Avatar answered Oct 19 '22 17:10

Thomas Levesque


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.

like image 45
Phil Devaney Avatar answered Oct 19 '22 19:10

Phil Devaney


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.

like image 8
Frank Bollack Avatar answered Oct 19 '22 17:10

Frank Bollack