在WPF中更改BitMapImage的尺寸,以及我可以在Image>元素

本文关键字:我可以 Image 元素 WPF BitMapImage | 更新日期: 2023-09-27 18:18:00

我正在尝试创建一个具有TreeView元素的资源管理器应用程序,并为树的每个级别具有不同的图标,并在这里遵循文章:http://www.codeproject.com/Articles/21248/A-Simple-WPF-Explorer-Tree

一切都很好,除了我想有不同的大小的图标。

我的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对象的尺寸似乎不起作用。我还可以返回哪些图像对象作为源?

在WPF中更改BitMapImage的尺寸,以及我可以在Image>元素

好了,我明白我的问题了。天啊,真是个笨蛋。下面是我修改的代码,得到了我想要的结果:

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;