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

Dbloom picture Dbloom · Jun 12, 2013 · Viewed 18.5k times · Source

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?

Answer

Dbloom picture Dbloom · Jun 12, 2013

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;