“无法将字符串转换为ImageSource。”我该怎么做?

private void HeroMouseEnter(object sender, MouseEventArgs e) { ((Image)sender).Source = GetGlowingImage(((Image)sender).Name); } public ImageSource GetGlowingImage(string name) { switch (name) { case "Andromeda": return "HeroGlowIcons/64px-Andromeda.gif"; default: return null; } } 

我只是想根据鼠标输入的位置制作一个更改图像的事件。 但我无法做到这一点。

编辑:我在Windows窗体中执行此操作,它100%像我想要的那样工作。 我怎么能在WPF中翻译这样的东西?

 void HeroMouseEnter(object sender, EventArgs e) { ((PictureBox)sender).Image = GetGlowingImage(((PictureBox)sender).Name); } public Image GetGlowingImage(string name) { switch (name) { case "Andromeda": return Properties.Resources._64px_Andromedahero___copia; case "Engineer": return Properties.Resources._64px_Engineerhero___copia; default: return null; } } 

GetGlowingImage()方法中,您需要生成一个新的ImageSource

此链接可能有所帮助: 在代码中设置WPF图像源

编辑:

看到这里的区别是,在WindowsForms代码中,您有Properties.Resources._64px_Andromedahero ___ copia是包含图像数据的Image变量的名称。 在您的WPF代码中,字符串“filename ….”不是图像或图像源,它只是表示文件路径的字符串。 您需要使用该路径加载图像文件。

我知道这没有意义,因为在设计时你可以指定一个文件名并为你构建ImageSource。 在代码中,您需要创建ImageSource(或派生对象,即:BitmapSource)并将适当的图像加载到其中。

编辑:尝试这个,未经测试(并检查上面的链接):

  public ImageSource GetGlowingImage(string name) { string fileName = string.Empty; switch (name) { case "Andromeda": { fileName = "HeroGlowIcons/64px-Andromeda.gif"; break; } } BitmapImage glowIcon = new BitmapImage(); glowIcon.BeginInit(); glowIcon.UriSource = new Uri("pack://application:,,,/ApplicationName;component/" + fileName); glowIcon.EndInit(); return glowIcon; } 

通过编辑,您很乐意在资源中拥有一组静态图像。 如果这是正确的,你可以这样做:

    

然后将其加载为:

 public ImageSource GetGlowingImage(string name) { switch (name) { case "Andromeda": return (ImageSource)FindResource("andromeda64"); default: return null; } } 

FindResource要求您在可视树中使用代码隐藏(例如,事件处理程序)。 如果不是这种情况,或者您希望能够加载不在资源字典中的内容,请参阅Cory的答案 。 使用和重用资源的优点是每次使用时都不需要创建BitmapImage(尽管在这种情况下,这样做的开销成本是微不足道的)。

您可能想要返回一个BitmapSource 。 这篇MSDN文章有一个如何从图像文件创建BitmapSource的示例。

就像是….

BitmapImage myBitmapImage = new BitmapImage(new Uri(“../../../../ Images / folder.png”,UriKind.Relative));

myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;

Image.Source = myBitmapImage;

考虑使用EventTrigger在XAML中完全执行此操作,而不是在后面的代码中弄乱魔术字符串。

这个链接会有所帮助

如果您打算通过单击按钮更改支持System.Windows.Media.Brush的Grid或其他Panel的背景,可获得更多信息

 private void btnBackgroundImage_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Image Files (*.bmp;*.png;*.jpg;)|*.bmp;*.png;*.jpg"; dlg.Multiselect = false; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == true) { txtBackImageName.Text = dlg.FileName; _layoutRoot.Background = new System.Windows.Media.ImageBrush(GetImageSource(dlg.FileName)); } } public ImageSource GetImageSource(string filename) { string _fileName = filename; BitmapImage glowIcon = new BitmapImage(); glowIcon.BeginInit(); glowIcon.UriSource = new Uri(_fileName); glowIcon.EndInit(); return glowIcon; } 
 var converter = new Xamarin.Forms.ImageSourceConverter(); var imgSource = (Xamarin.Forms.ImageSource) converter.ConvertFromInvariantString(item.Poster);