2009-12-15 72 views
4
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; 
     } 
    } 

我只是试图根据鼠标进入的位置来改变图像。但我无法完成这项工作。”无法将字符串转换为ImageSource。“我怎样才能做到这一点?

编辑:我在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; 
     } 
    } 

回答

6

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

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

编辑:

看到这里的区别是,在WindowsForms代码你有Properties.Resources._64px_Andromedahero ___ copia是一个Image变量的名称,它包含了ima ge数据。在你的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; 
    } 
+0

我的点击没有跟随。我在该方法内创建了新的ImageSource,但仍然无法添加字符串值,所以我回到了原来的位置。 :D – 2009-12-15 03:21:30

+2

在代码中我看到你正在返回一个字符串,而不是一个新的ImageSource。你没有包含创建新ImageSource的代码部分吗? 返回“HeroGlowIcons/64px-Andromeda.gif”; // < - - 返回一个字符串 – 2009-12-15 03:24:14

+0

嗯...我在Windows窗体中测试了一个非常类似于此的代码。我要编辑我的问题,希望我的意图会更清晰。 :D – 2009-12-15 03:27:38

2

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

2

喜欢的东西....

的BitmapImage myBitmapImage =新的BitmapImage(新URI( “../../../../图像/ folder.png”,UriKind.Relative));

myBitmapImage.CacheOption = BitmapCacheOption.OnLoad;

Image.Source = myBitmapImage;

+0

但是,如果我这样做,我会创建一个新的位图并将其复制到一个新的变量。我只想通过类似于我在布局过程中所做的字符串来引用它。Image.Source =“blablabla”,对不对?有没有办法让我把字符串转换成ImageSource? – 2009-12-15 03:25:01

+0

布局在运行时正在做类似的事情。 – 2009-12-15 03:27:06

+0

Papuccino,这是做同样的事情。您正在将现有图像加载到BitmapImage中。无论哪种方式,都必须在运行时从文件创建一个C#对象。 – 2009-12-15 03:30:55

3

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

<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(尽管在这种情况下,这样做的开销成本将是微不足道的)。

0

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

This link would help

0

的更多,如果你意图改变电网或其它面板的背景支持System.Windows.Media.Brush一个按钮

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

将一些描述添加到您的代码,所以其他用户可以理解它。 – beerwin 2016-11-29 14:42:35

相关问题