4

I'm using

BitmapImage  sharerimg;
sharerimg = new BitmapImage(new Uri("F:/workspace/wpf/NetworkMFServer/NetworkMFServer/imageresources/sharer.png"));
Image im = new Image();
im.Source = sharerimg;

But i add image to resources and code this

Assembly assembly = Assembly.GetExecutingAssembly();
            string strBaseName = assembly.GetName().Name + ".Properties.Resources";
            ResourceManager rm = new ResourceManager(strBaseName, assembly);
Image im = new Image();
im=(Image)rm.GetObject("sharer");

However this show error

" Can not cast 'System.Drawing.Bitmap' to 'System.Windows.Controls.Image'"

How can i use image resouce to apply System.Windows.Controls.Image .Source property?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Lightstar
  • 183
  • 3
  • 12
  • My image variable 'im' is System.windows.controls.image type and resource image file 'Resources.sharer' is System.Drawing.Bitmap type. What is fast way using this resource to im – Lightstar Jan 22 '15 at 08:10
  • See [here](http://stackoverflow.com/a/22957974/1136211). – Clemens Jan 22 '15 at 08:51
  • clientimg = new BitmapImage(new Uri("pack://application:,,,/Resources/client.png")); ...And 'resources/client.png' There is no resource – Lightstar Jan 22 '15 at 11:10
  • possible duplicate of [Converting BitmapImage to Bitmap and vice versa](http://stackoverflow.com/questions/6484357/converting-bitmapimage-to-bitmap-and-vice-versa) – Peter Duniho Jan 22 '15 at 20:16

1 Answers1

5

As per MSDN exmaple you should do like this -

Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;

https://msdn.microsoft.com/en-us/library/system.windows.controls.image.source(v=vs.110).aspx

Accessing embedded image and creating a System.Windows.Controls.Image

Community
  • 1
  • 1
Amit
  • 882
  • 6
  • 13
  • Is it use resource? Is 'smiley_stackpanel.PNG' not local file? – Lightstar Jan 22 '15 at 07:36
  • For resource added SO link . Please check second link – Amit Jan 22 '15 at 07:38
  • You mean to say if you try that code asm.GetManifestResourceStream("path.to.resource"); you are getting empty stream? – Amit Jan 22 '15 at 08:10
  • Yes.. I correctly add image file to my project resouces – Lightstar Jan 22 '15 at 08:13
  • 3
    Please note that you can avoid the BeginInit/EndInit sequence when you use the BitmapImage constructor with Uri parameter (as used in the question): `var bi = new BitmapImage(new Uri(...));` – Clemens Jan 22 '15 at 08:55
  • @LightStar- You used GetCallingAssembly or GetExecutingAssembly function? – Amit Jan 22 '15 at 09:38
  • Also note - The GetManifestResourceStream method will always returns NULL if the resource ’built action’ property is not set to ’embedded resource’. – Amit Jan 22 '15 at 09:40