1

I had set up to access an embedded resource and return an System.Drawing.Image but i cant use this to set the background of a canvas.

Could someone please show me how to access an embedded image file and create a System.Windows.Controls.Image. Code I have so far is :

public static Image Load(Type classType, string resourcePath)
        {
            Assembly asm = Assembly.GetAssembly(classType);
            Stream imgStream = asm.GetManifestResourceStream(resourcePath);
            Image img = Image.FromStream(imgStream);
            imgStream.Close();

            return img;
        }

Please let me know if you require anymore information

user589195
  • 4,180
  • 13
  • 53
  • 81
  • 2
    Have you tried this? http://stackoverflow.com/questions/7346282/creating-images-from-an-embedded-resource – paul Jun 28 '12 at 11:14
  • Im struggling to relate that to my situation as the resource is not in the same dll but embedded in the class type parameter. A code example would help me hugely – user589195 Jun 28 '12 at 12:58
  • can I ask why you need a `System.Drawing.Image` if all you want to do is set the background of a wpf canvas, then there are easier ways – paul Jun 28 '12 at 13:30
  • I dont... The code I have is what I had to get an Image out of a dll containing an embedded resource. Instead of teh method returning a System.Drawing.Image I want it to return a System.Windows.Controls.Image but I cant work out how to. – user589195 Jun 28 '12 at 13:48

2 Answers2

2

If you don't specifically need to use a `System.Drawing.Image', then you could try something like:

        Assembly asm = Assembly.GetCallingAssembly();
        var res = asm.GetManifestResourceNames();
        Stream imgStream = asm.GetManifestResourceStream("path.to.resource");

        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = imgStream;
        image.EndInit();

        ImageBrush brush = new ImageBrush();
        brush.ImageSource = image;

        imageCanvas.Background = brush;
paul
  • 21,653
  • 1
  • 53
  • 54
1

Bitmap bm = new Bitmap(@"...."); img = bm;