Changing the dimensions of a BitMapImage in WPF, and what kind of objects can I put in an <Image> element?

我的梦境 提交于 2019-12-04 04:22:26

问题


I am trying to create an explorer app with a TreeView element, 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

It is all working great, except that I want to have different sized icons as well.

My XAML for the Image element is here:

<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?


回答1:


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;


来源:https://stackoverflow.com/questions/17072775/changing-the-dimensions-of-a-bitmapimage-in-wpf-and-what-kind-of-objects-can-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!