来自String的ImageSource无法正常工作? [英] ImageSource from String not working?
问题描述
我的项目文件夹中有一堆* .tif图像.我也已将其添加到Visual Studio项目的"Templates \ Team Logos"文件夹中
I have a bunch of *.tif images in a folder in my project..which i've also added to my visual studio project in a folder located "Templates\Team Logos"
现在我是否将图像源设置为:
now if i set an image source to say:
<Image Name="UL_Team1_Image" Grid.Row="1" Grid.Column="1" Margin="5" Source="Team Logos\ARI.tif"></Image>
那行得通.但是现在,如果我尝试:
That works. But now if i try:
UL_ImageArr[a].Source = (ImageSource)new ImageSourceConverter().ConvertFromString("Team Logos\\ARI.tif");
那是行不通的.是什么赋予了?我收到了NullReferenceException ...但是对我来说没有意义?
that doesn't work. What gives? I get a NullReferenceException... but it doesn't make sense to me?
推荐答案
您可以在代码隐藏中使用它,我认为它比使用ImageSourceConverter
更快.
You can use this in your code-behind, I think, it is more faster than using ImageSourceConverter
.
BitmapImage bimage = new BitmapImage();
bimage.BeginInit();
bimage.UriSource = new Uri("Team Logos\\ARI.tif", UriKind.Relative);
bimage.EndInit();
UL_ImageArr[a].Source = bimage;
如果要使用ImageSourceConverter
,则必须通过pack-uri引用图像文件:
If you want to use ImageSourceConverter
you must refer to image file by pack-uri:
var converter = new ImageSourceConverter();
UL_ImageArr[a].Source =
(ImageSource)converter.ConvertFromString("pack://application:,,,/Team Logos/ARI.tif");
这篇关于来自String的ImageSource无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!