【问题标题】:XAML ImageBrush using a BitmapImage without a URIXAML ImageBrush 使用没有 URI 的 BitmapImage
【发布时间】:2009-08-18 16:50:56
【问题描述】:

我有以下 XAML,它使用 URI 显示一本书的封面图片:

<Rectangle.Fill>
    <ImageBrush ImageSource="{Binding CoverUrl}" />
</Rectangle.Fill>

但是,我想使用的图像不在磁盘上的任何位置,也不能通过 URI 访问;它来自我解析成BitmapImage 对象的二进制文件。

当我通过代码创建BitmapImage 对象时,生成的对象的BaseUriUriSource 属性为空。如何让ImageBrush 使用驻留在内存中的BitmapImage 而不是从URI 中读取它?

【问题讨论】:

    标签: c# .net xaml


    【解决方案1】:

    ImageSource 属性的类型是 ImageSource,而不是 Uri 或字符串...实际上,当您将 Uri 分配给它时,就会发生转换。您可以将 ImageBrush 直接绑定到返回 ImageSource 的属性

    <Rectangle.Fill>
        <ImageBrush ImageSource="{Binding Cover}" />
    </Rectangle.Fill>
    
    
    private ImageSource _cover;
    public ImageSource Cover
    {
        get
        {
            if (_cover == null)
            {
                _cover = LoadImage();
            }
            return _cover;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多