在WPF中更改BitMapImage的尺寸,以及可以在<img/>元素中放置哪种对象?

时间:2013-06-12 18:32:31

标签: c# wpf treeview bitmapimage

我正在尝试使用TreeView元素创建一个资源管理器应用,并为树的每个级别设置不同的图标,并按照此处的文章进行操作:http://www.codeproject.com/Articles/21248/A-Simple-WPF-Explorer-Tree

除了我想要拥有不同的大小的图标外,这一切都很有效。

Image元素的XAML位于:

<Image Name="img"
       Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
       AncestorType={x:Type TreeViewItem}},
       Path=Header,
       Converter={x:Static local:HeaderToImageConverter.Instance}}"
/>

决定返回哪个图标的代码片段在这里:

if ((value as string).Contains(@"\""))
{
    Uri uri = new Uri ("pack://application:,,,/Images/DeployWiz_Network.png");
    BitmapImage source = new BitmapImage(uri);

    return source;
}

如何更改返回图像的尺寸?更改bitmapimage对象的尺寸似乎不起作用。我可以将哪些其他图像对象作为源返回?

1 个答案:

答案 0 :(得分:13)

好的,我想出了我的问题。 Jeez,真是个假人。下面是我改变的代码,它让我得到了我想要的结果:

Uri uri = new Uri("pack://application:,,,/Images/DeployWiz_Network.png");
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = uri;
source.DecodePixelHeight = 10;
source.DecodePixelWidth = 10;
source.EndInit();

return source;