简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM