tags:

views:

624

answers:

5
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 entered. But I cannot make this work.

Edit: I did this in Windows Forms and it work 100% like I want it to. How could 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  A: 

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

This link might help: http://stackoverflow.com/questions/350027/setting-wpf-image-source-in-code

Edit:

See the difference here is that in the WindowsForms code you have the Properties.Resources._64px_Andromedahero___copia is the name of an Image variable that contains the image data. In your WPF code the string "filename...." is not an image or image source it's just a string that represents the path to the file. You need to load the image file using that path.

I know it doesn't make sense because at design time you can specify a filename and it builds the ImageSource for you. In code you need to create the ImageSource (or derived object, ie: BitmapSource) and load the appropriate image into it.

Edit: Try this, untested (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;
    }
Cory Charlton
I'm not following. I made the new ImageSource inside the method but I still cannot add the string value, so I'm back to square one. :D
Sergio Tapia
In the code I see about you are returning a string and not a new ImageSource. Did you not include the part of your code that creates the new ImageSource?return "HeroGlowIcons/64px-Andromeda.gif"; // <- Returning a string
Cory Charlton
Um...I tested a code very similar to this in Windows Form. I'm going to edit my question and hopefully my intent will be much clearer. :D
Sergio Tapia
Unfortunately WindowsForms makes sense and works like you would expect it to. WPF on the other hand does not ;-) Sure with WPF I can make a slick UI but under most circumstances I find I need to jump through hoops just to accomplish something simple.
Cory Charlton
+1  A: 

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

Andy
A: 

Something like....

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

myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;

Image.Source = myBitmapImage;

Daniel
But if I do this, I would be creating a new bitmap and copying it to a new variable. I just want to reference it via a string similar to what I did during layout Image.Source="blablabla", right? Is there a way for my to convert a string to a ImageSource?
Sergio Tapia
The layout is doing something similiar to this at runtime.
Cory Charlton
Papuccino, this is doing the same thing. You're loading the existing image into a BitmapImage. Either way, a C# object has to be created at runtime from a file.
Will Eddins
If his images are in a ResourceDictionary, he can access them by name using FindResource, without needing to instantiate a new BitmapImage. (Of course WPF will still be instantiating all those images when it loads the resources, but that's a one-off cost -- doesn't have to be paid each time he switches.)
itowlson
+4  A: 

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).

itowlson
A: 

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

This link would help

vanja.