“无法将字符串转换为ImageSource."我怎样才能做到这一点? [英] "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;
}
}
我只是想让一个事件根据鼠标进入的位置更改图像.但是我无法完成这项工作.
我在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;
}
}
在您的GetGlowingImage()
方法中,您需要生成一个新的ImageSource
此链接可能有帮助:在代码中设置WPF图像源 >
这里看到的区别是,在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;
}
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;
}
}
In your GetGlowingImage()
method you need to generate a new ImageSource
This link might help: 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;
}
这篇关于“无法将字符串转换为ImageSource."我怎样才能做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!