7

I have an icon on resources that it key is: xxx

I want to bind it to an image in xaml..

1:

  <Image Source="{x:Static p:Resources.xxx}"></Image>

2:

  <Image>
     <Image.Source>
         <BitmapImage UriSource="{Binding x:Static p:Resources.xxx}"/>
        </Image.Source>
   </Image>

3:

 <Image Source=" {Binding x:Static p:Resources.xxx,Converter={StaticResource IconToBitmap_Converter}}"></Image>

4:

 <Image>
    <Image.Source>
        <BitmapImage UriSource="{Binding x:Static p:Resources.xxx,Converter={StaticResource IconToBitmap_Converter}}"/>
    </Image.Source>
 </Image>

The above ways does not work, how am I supposed to do that?

1

3 Answers 3

21

First you must add your Image into a Resource File in the Solution Explorer. Next you must set the Build Action of your Image to Resource and then you can use it in the XAML Code like this:

<UserControl>
<UserControl.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="name" UriSource="Resources/yourimage.bmp" />
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <Image  Source="{StaticResource name}"/>
</Grid>
</UserControl>
Sign up to request clarification or add additional context in comments.

Comments

3

First: Add resources rsx then: add images as images to the resource file and set the image build action to Resource. Now you can access the images like this:

<Image Source="pack://application:,,,/Resources/image.png"/>

Comments

0

You may implement your own Markup extension and use it as follows:

<Image Source="{ex:ImageSource {x:Static p:Resources.xxx}}"></Image>

The MarkupExtension ImageSource may look like this:

public class ImageSource : MarkupExtension
{
    private readonly object source;

    public ImageSource(object source) => this.source = source;

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        Binding sourceBinding = source is Binding binding ? binding : new Binding() { Source = source };
        MultiBinding converterBinding = new MultiBinding() { Converter = new SourceConverter() };
        converterBinding.Bindings.Add(sourceBinding);

        return converterBinding.ProvideValue(serviceProvider);
    }

    private class SourceConverter : IMultiValueConverter
    {
        public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture) => BitmapToSource(value[0] as Bitmap);

        public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();

        private BitmapImage BitmapToSource(Bitmap bitmap)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                memory.Position = 0;
                BitmapImage bitmapimage = new BitmapImage();
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();
                return bitmapimage;
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.