"Cannot convert string to ImageSource." How can i do this?

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; } } 

I'm just trying to make an event to change the image according to where the mouse was inserted. But I can not do this job.

Edit: I did this on Windows Forms, and it works 100% the way I want. How can I translate something like this in 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; } } 
+4
c # image wpf mouseevent
source share
7 answers

In your GetGlowingImage() method, you need to generate a new ImageSource

This link may help: Configure WPF image source in code

Edit:

See the difference in the fact that in WindowsForms code you have Properties.Resources._64px_Andromedahero ___ copia - this is the name of the Image variable that contains the image data. In your WPF code, the string "filename ...." is not the source of the image or image, but just a string representing the path to the file. You need to upload the image file using this path.

I know this does not make sense, because during development you can specify the file name, and it creates an ImageSource for you. In the code, you need to create an ImageSource (or a derived object, that is: BitmapSource) and load the corresponding image into it.

Edit: Try this, unverified (and check my link above):

  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; } 
+6
source share

Going through your editing, you are happy to have a static set of images in resources. If this is correct, you can do this:

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

then download 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 for something in a visual tree (like an event handler). If this is not the case, or if you want to load things that are not in the resource dictionary, see Corey's answer . The advantage of using and reusing resources is that you do not need to create BitmapImage every time you use it (although in this case the overhead will be negligible).

+3
source share

You probably want to return a BitmapSource . This MSDN article contains an example of creating a BitmapSource from an image file.

+2
source share

Something like....

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

myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;

Picture. Source = myBitmapImage;

+2
source share

Consider using EventTrigger to do this completely in XAML instead of messing with magic lines in your code.

This link will help

0
source share

if you intend to change the background of the grid or another panel that supports System.Windows.Media.Brush with the 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
source share
 var converter = new Xamarin.Forms.ImageSourceConverter(); var imgSource = (Xamarin.Forms.ImageSource) converter.ConvertFromInvariantString(item.Poster); 
0
source share

All Articles