“Cannot convert string to ImageSource.” How can I do this?

前端 未结 7 1544
南旧
南旧 2021-01-13 11:09
private void HeroMouseEnter(object sender, MouseEventArgs e)
    {
        ((Image)sender).Source = GetGlowingImage(((Image)sender).Name);            
    }

    pub         


        
相关标签:
7条回答
  • 2021-01-13 11:38

    for more if you intent to change the background of a Grid or other Panel that support System.Windows.Media.Brush with a click of a button

    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;
    }
    
    0 讨论(0)
  • 2021-01-13 11:42

    Consider using an EventTrigger to do this completely in XAML instead of messing with magic strings in the code behind.

    This link would help

    0 讨论(0)
  • 2021-01-13 11:44

    Something like:

    BitmapImage myBitmapImage = new BitmapImage(new Uri("../../../../Images/folder.png", UriKind.Relative));
    myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    Image.Source = myBitmapImage;
    
    0 讨论(0)
  • 2021-01-13 11:51
    var converter = new Xamarin.Forms.ImageSourceConverter();
    var imgSource = (Xamarin.Forms.ImageSource) converter.ConvertFromInvariantString(item.Poster);
    
    0 讨论(0)
  • 2021-01-13 11:53

    You probably want to return a BitmapSource. This MSDN article has an example of how to create a BitmapSource from an image file.

    0 讨论(0)
  • 2021-01-13 11:56

    Going by your edit, you are happy to have a static set of images in resources. If that's correct, you can do this:

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

    then load it as:

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

    FindResource requires you to be in code-behind for something in the visual tree (e.g. an event handler). If this isn't the case, or if you want to be able to load things that aren't in a resource dictionary, see Cory's answer. The advantage of using and reusing resources is that you don't need to create a BitmapImage each time you use it (though in this case the overhead cost of doing so will be insignificant).

    0 讨论(0)
提交回复
热议问题