Lightstar
Lightstar

Reputation: 193

Load image from resource and apply to Image.source

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?

Upvotes: 4

Views: 6553

Answers (1)

Amit
Amit

Reputation: 890

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

Upvotes: 5

Related Questions