c# - How to bind images from Properties.Resources in Xaml? -


i have images added properties.resources, can access them like:

properties.resources.layericon; 

and want use in xaml, don't know how this.

i know there different ways images added wpf project, need use properties.resources, because that's way found images show up, when application launched via reflection.

the images in properties.resources of type system.drawing.bitmap, wpf uses system.windows.media.imagesource. can create converter:

[valueconversion(typeof(system.drawing.bitmap), typeof(imagesource))] public class bitmaptoimagesourceconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         var bmp = value system.drawing.bitmap;         if (bmp == null)             return null;         return system.windows.interop.imaging.createbitmapsourcefromhbitmap(                     bmp.gethbitmap(),                     intptr.zero,                     int32rect.empty,                     bitmapsizeoptions.fromemptyoptions());     }      public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         throw new notsupportedexception();     } } 

and use follows:

<image source="{binding source={x:static prop:resources.layericon}, converter={staticresource bitmaptoimagesourceconverter}}" /> 

make sure resource set public , not internal.


Comments