在 WPF 中更改 BitMapImage 的尺寸,我可以在 <Image> 元素中放置什么样的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17072775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the dimensions of a BitMapImage in WPF, and what kind of objects can I put in an <Image> element?
提问by Dbloom
I am trying to create an explorer app with a TreeViewelement, and have different icons for each level of the tree, and following the article here: http://www.codeproject.com/Articles/21248/A-Simple-WPF-Explorer-Tree
我正在尝试创建一个带有TreeView元素的资源管理器应用程序,并且树的每个级别都有不同的图标,并按照此处的文章进行操作:http: //www.codeproject.com/Articles/21248/A-Simple-WPF-Explorer -树
It is all working great, except that I want to have different sizedicons as well.
一切都很好,除了我也想要不同大小的图标。
My XAMLfor the Image element is here:
我XAML的 Image 元素在这里:
<Image Name="img"
Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type TreeViewItem}},
Path=Header,
Converter={x:Static local:HeaderToImageConverter.Instance}}"
/>
The piece of code that decides which icon to return is here:
决定返回哪个图标的代码段在这里:
if ((value as string).Contains(@"\""))
{
Uri uri = new Uri ("pack://application:,,,/Images/DeployWiz_Network.png");
BitmapImage source = new BitmapImage(uri);
return source;
}
How would I change the dimensions of the image being returned? Changing the dimensions of a bitmapimage object doesn't seem to work. What other image objects could I return as the source?
我将如何更改返回图像的尺寸?更改位图图像对象的尺寸似乎不起作用。我可以返回哪些其他图像对象作为源?
回答by Dbloom
Ok, I figured out my question. Jeez, what a dummy. Below is the code I changed that got me the results I wanted:
好的,我想出了我的问题。天哪,真是个假人。下面是我更改的代码,它使我得到了我想要的结果:
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;

