代码之家  ›  专栏  ›  技术社区  ›  Sergio Tapia

“无法将字符串转换为ImageSource。”如何执行此操作?

  •  5
  • Sergio Tapia  · 技术社区  · 15 年前
    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;
            }
        }
    
    7 回复  |  直到 13 年前
        1
  •  6
  •   Community CDub    8 年前

    在你的 GetGlowingImage() 方法生成一个新的 ImageSource

    此链接可能有助于: Setting WPF image source in code

    我知道这没有意义,因为在设计时,您可以指定一个文件名,它会为您构建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;
        }
    
        2
  •  3
  •   Community CDub    8 年前

    通过编辑,您很高兴在参考资料中有一组静态图像。如果正确,您可以执行以下操作:

    <Application.Resources>
      <BitmapImage x:Key="andromeda64" UriSource="andromeda_64px.jpg" />
    </Application.Resources>
    

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

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

        3
  •  3
  •   Danny Beckett    6 年前

    比如:

    BitmapImage myBitmapImage = new BitmapImage(new Uri("../../../../Images/folder.png", UriKind.Relative));
    myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    Image.Source = myBitmapImage;
    
        4
  •  2
  •   Andy    15 年前

    BitmapSource MSDN article 有一个如何创建 位图源 从图像文件。

        5
  •  0
  •   vanja.    15 年前

    考虑使用EngEnter触发器完全在XAML中完成此操作,而不是在代码后面使用魔法字符串。

    This link would help

        6
  •  0
  •   P. Frank    9 年前

    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;
    }
    
        7
  •  0
  •   Setar    8 年前
    var converter = new Xamarin.Forms.ImageSourceConverter();
    var imgSource = (Xamarin.Forms.ImageSource) converter.ConvertFromInvariantString(item.Poster);