dotToString
dotToString

Reputation: 230

How do you retrieve an embedded image from a dll for wpf Image control?

 public partial class MainWindow : Window
    {
        System.Reflection.AssemblyName aName;
        System.Reflection.Assembly asm;
        public MainWindow()
        {
            InitializeComponent();

            aName = System.Reflection.AssemblyName.GetAssemblyName(
            @"C:\Users\Adam\Documents\Visual Studio 2012\Projects\WPFResources\WPFResources\bin\Release\WPFResources.dll");

            asm = System.Reflection.Assembly.Load(aName);

            string[] manifest = asm.GetManifestResourceNames();
            image1 =
        }

this is where I'm stuck as to how to get it working. In Winforms I would just create a new bitmap and in the bitmap's constructor pass in the stream from the DLL with its manifest location...

Upvotes: 1

Views: 2810

Answers (3)

RevitArkitek
RevitArkitek

Reputation: 161

I found a simple way using a combination of other answers.

var dllAssembly = Assembly.LoadFile(dllPath);
var resMan = new ResourceManager(namespace, dllAssembly);
var image = resMan.GetObject(imageName) as Image;

dllPath = full path to the dll to get the image from

namespace = this is the location of your resources in the dll, for example "Apps.Properties.Resources"

imageName = the name of the image resource

Note: Make sure all images are set to 'Resource' in its properties

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

You can use the BitmapImage.StreamSource it will allow you to create an Image from a System.IO.Stream.

See if this works for you

asm = System.Reflection.Assembly.Load(aName)
string[] manifest = asm.GetManifestResourceNames();

using (UnmanagedMemoryStream stream = (UnmanagedMemoryStream)asm.GetManifestResourceStream(manifest[0]))//The Index of the Image you want to use
{
    using (MemoryStream ms1 = new MemoryStream())
    {
        stream.CopyTo(ms1);
        BitmapImage bmi = new BitmapImage();
        bmi.BeginInit();
        bmi.StreamSource = new MemoryStream(ms1.ToArray());
        bmi.EndInit();
        image1.Source  = bmi; //The name of your Image Control
    }

}

Upvotes: 1

RoadBump
RoadBump

Reputation: 751

Create a ResourceManager, give him in the ctor the 'base name'- name of your resources file (an assembly can contain many resources sets), and the assembly where to look for resources.

ResourceManager resMan= new ResourceManager("MainWindowResources",asm);
Stream imageStream= resMan.GetStream("Image1");

Upvotes: 0

Related Questions