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

19,780

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;
Share:
19,780

Related videos on Youtube

Dbloom
Author by

Dbloom

By day I do Windows programming in the government sector. At night I play videogames and work on house projects.

Updated on May 25, 2022

Comments

  • Dbloom
    Dbloom almost 2 years

    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?

    • Clemens
      Clemens almost 11 years
      Why not simply set the Width and Height of the Image control?
    • Dbloom
      Dbloom almost 11 years
      Because depending on what level of the tree you are in, I want that to be different.
  • Clemens
    Clemens almost 11 years
    You should not set both of DecodePixelWidth and DecodePixelHeight as that may spoil the image's aspect ratio.
  • secretgenes
    secretgenes about 8 years
    @Dbloom: decodePixelHeight and Decodepixelwidth property doesn't work in windows 8 though it work in windows 7 and windows 10.
  • Hendra Anggrian
    Hendra Anggrian over 7 years
    newer version of BitmapImage no longer have BeginInit() and EndInit().