【问题标题】:Display image from resources by binding -WPF通过绑定 -WPF 从资源中显示图像
【发布时间】:2022-05-05 23:52:44
【问题描述】:

我在资源上有一个图标,它的关键是:xxx

我想将它绑定到 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>

以上方法都行不通,我该怎么办?

【问题讨论】:

标签: wpf image resources


【解决方案1】:

首先,您必须将图像添加到解决方案资源管理器中的资源文件中。接下来,您必须将 Image 的 Build Action 设置为 Resource,然后您可以在 XAML 代码中使用它,如下所示:

<UserControl>
<UserControl.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="name" UriSource="Resources/yourimage.bmp" />
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <Image  Source="{StaticResource name}"/>
</Grid>
</UserControl>

【讨论】:

    【解决方案2】:

    首先: 添加资源 rsx 然后: 将图像作为图像添加到资源文件中,并将图像构建操作设置为 Resource。 现在您可以像这样访问图像:

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

    【讨论】:

      【解决方案3】:

      您可以实现自己的标记扩展并按如下方式使用它:

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

      MarkupExtension ImageSource 可能如下所示:

      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;
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-09-21
        • 1970-01-01
        • 2011-09-12
        • 1970-01-01
        • 1970-01-01
        • 2015-01-20
        • 1970-01-01
        • 2010-10-19
        • 1970-01-01
        相关资源
        最近更新 更多