【问题标题】:Changing the image displayed at runtime [duplicate]更改运行时显示的图像 [重复]
【发布时间】:2016-10-08 01:54:19
【问题描述】:

如何更改运行时显示的图像?

我的代码如下:

<Image Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Image.Source>
        <BitmapImage x:Name="Profile"
            DecodePixelWidth="300"
            DecodePixelHeight="300"
            UriSource="/MyAssembly;component/Resources/Profile1.png" />
    </Image.Source>
</Image>

我要使用的图像基本上是线条艺术,因此由于像素调整,直接使用 Image 元素创建这种缩略图是不行的。

我需要更改显示的图像,但更改UriSource 似乎没有任何作用,如果我使用图像的来源,它似乎要求我取消整个 Image.Source 部分。

【问题讨论】:

  • 如何以编程方式更改UriSource?你能展示你不断变化的 C# 代码吗?
  • 我发现我实际上可以将 BitmapImage 传递给 Image 元素的 Source。
  • public ImageSource FileToImageSource(Uri File, int DecodeSize = 300) { BitmapImage bitmap = new BitmapImage(File); bitmap.DecodePixelWidth = 解码大小;位图。冻结();返回位图; }

标签: c# wpf image xaml


【解决方案1】:

我认为您的 UriSource 的这种格式可能会更好。

<Image Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Image.Source>
        <BitmapImage x:Name="Profile"
            DecodePixelWidth="300"
            DecodePixelHeight="300"
            UriSource="pack://application:,,,/MyAssembly;component/Resources/Profile1.png" />
    </Image.Source>
</Image>

【讨论】:

  • 我在 Uri 上仍然出现错误,当我传入它时它不想识别它。
  • 你是否将镜像的构建动作设置为“资源”?
【解决方案2】:

使用IValueConverter 从路径字符串创建并返回ImageSource。例如;您的图像(desert.jpeg、penguins.jpg)位于根文件夹的图像文件夹中。确保为图像设置了Build Action Type to Content, and Copy To Output Directory to Always

<local:UriToImgConverter x:Key="UriToImgCnvKey"/>

<Image x:Name="ImgCtrl" Source="{Binding Img, Mode=OneWay,Converter={StaticResource UriToImgCnvKey}}"/>

代码:

 ImgViewModel vm1 = new ImgViewModel ( ) ; 
 ImgCtrl.DataContext = vm1;
 vm1.Img = "Images/Desert.jpg";

ViewModel/转换器代码:

    public class ImgViewModel:INotifyPropertyChanged
    {    
        string _path ;
        public string Img {

            get { return _path; }
            set { _path = value; OnPropertyChanged("Img"); }        
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public void OnPropertyChanged(string prop)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

    public class UriToImgConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage img = new BitmapImage(new Uri(value.ToString(), UriKind.Relative));
            BitmapFrame frame = BitmapFrame.Create(img);

            return frame;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

【讨论】:

  • 您不需要绑定转换器即可将 Uri 或字符串转换为 ImageSource。这已经由 ImageSourceConverter 类完成,该类注册为 ImageSource 类的 TypeConverter。除此之外,您最好将 Build Action 设置为 Resource 以将图像文件嵌入程序集中。因此,您不会有任何额外的外部图像文件。
  • @Clemens 内置转换器需要打包 uri。相对 uri 不起作用。
  • 而 OP 使用 Pack URI,那么你的意思是什么?除此之外,内置类型转换适用于任何有效的 URI。
猜你喜欢
  • 1970-01-01
  • 2015-01-15
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
相关资源
最近更新 更多