【问题标题】:"Cannot convert string to ImageSource." How can I do this?“无法将字符串转换为 ImageSource。”我怎样才能做到这一点?
【发布时间】:2019-02-24 06:56:28
【问题描述】:
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;
        }
    }

【问题讨论】:

    标签: c# wpf image mouseevent


    【解决方案1】:

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

    此链接可能会有所帮助:Setting WPF image source in code

    编辑:

    看到这里的不同之处在于,在 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;
        }
    

    【讨论】:

    • 我没有关注。我在方法中创建了新的 ImageSource,但我仍然无法添加字符串值,所以我回到了第一格。 :D
    • 在我看到的关于您的代码中,您返回的是一个字符串,而不是一个新的 ImageSource。您是否没有包含创建新 ImageSource 的代码部分?返回“HeroGlowIcons/64px-Andromeda.gif”; //
    • 嗯...我在 Windows 窗体中测试了一个与此非常相似的代码。我将编辑我的问题,希望我的意图会更加清晰。 :D
    • 不幸的是,WindowsForms 是有意义的,并且可以像您期望的那样工作。另一方面,WPF 没有 ;-) 当然,使用 WPF 我可以制作一个漂亮的 UI,但在大多数情况下,我发现我需要跳过障碍来完成一些简单的事情。
    【解决方案2】:

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

    <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】:

      类似:

      BitmapImage myBitmapImage = new BitmapImage(new Uri("../../../../Images/folder.png", UriKind.Relative));
      myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
      Image.Source = myBitmapImage;
      

      【讨论】:

      • 但如果我这样做,我将创建一个新位图并将其复制到一个新变量中。我只想通过类似于我在布局 Image.Source="blablabla" 中所做的字符串来引用它,对吧?有没有办法将字符串转换为 ImageSource?
      • 布局在运行时做了类似的事情。
      • Papuccino,这是在做同样的事情。您正在将现有图像加载到 BitmapImage 中。无论哪种方式,都必须在运行时从文件中创建 C# 对象。
      • 如果他的图像在 ResourceDictionary 中,他可以使用 FindResource 按名称访问它们,而无需实例化新的 BitmapImage。 (当然 WPF 在加载资源时仍会实例化所有这些图像,但这是一次性成本——不必在每次切换时支付。)
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        This link would help

        【讨论】:

          【解决方案6】:

          如果您打算通过单击按钮更改支持 System.Windows.Media.Brush 的 Grid 或其他面板的背景,请了解更多信息

          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】:
            var converter = new Xamarin.Forms.ImageSourceConverter();
            var imgSource = (Xamarin.Forms.ImageSource) converter.ConvertFromInvariantString(item.Poster);
            

            【讨论】:

            • 在您的代码中添加一些描述会很棒,以便其他用户可以理解。
            猜你喜欢
            • 2022-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-05
            • 2015-05-01
            • 1970-01-01
            • 2012-08-18
            • 1970-01-01
            相关资源
            最近更新 更多