Use .png files as tree node Icons

4 posts, 1 answers
  1. Joel Palmer
    Joel Palmer avatar
    170 posts
    Member since:
    May 2009

    Posted 31 Dec 2009 Link to this post

    I have the following hierarchy structure

    Item (object)
    - List<Item>Children (property)
    - ItemType (property)

    ItemType (object)
    - ImageSource Icon (property)

    My Icons are stored as .png resource files.  So, I can't load them directly.  Instead, I need to convert them from a Bitmap over to a BitmapSource.  They can then be used as the Icon (ImageSource).

    Question is, does anyone have a good way in managed code to do this conversion?  I currently do this using interop but I would think there is a better way.  My current conversion method:

            public static BitmapSource LoadBitmap(Bitmap source)  
            {  
                return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
                    source.GetHbitmap(),  
                    IntPtr.Zero, Int32Rect.Empty,  
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());  
            } 
  2. Answer
    Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 04 Jan 2010 Link to this post

    Hi Joel Palmer,

    You don't need to do a conversion. The ImageSource type can be used with images that are inside an assembly. Please check this blog post for more info about the format of the uri that is used:
    http://blogs.telerik.com/valentinstoychev/posts/08-10-02/loading_images_in_silverlight2_applications.aspx

    Let us know if you need more info.

    Sincerely yours,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Joel Palmer
    Joel Palmer avatar
    170 posts
    Member since:
    May 2009

    Posted 04 Jan 2010 Link to this post

    Okay.  Then here is where I get stuck.

    I update the ItemType.Icon to an Image instead of an image source:

            public S88 Name { get; set; }  
            public Image Icon { get; set; } 

    I previously had the HierarchicalDataTemplate as follows:

        <UserControl.Resources> 
            <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate"   
                DataType="{x:Type this:Model.Item}" 
                ItemsSource="{Binding Children, Mode=OneTime}">  
                <StackPanel Orientation="Horizontal">  
                    <Image Source="{Binding Path=ItemType.Icon}" /> 
                    <ContentPresenter   
                            Content="{Binding Name, Mode=OneTime}"   
                            Margin="2,0" 
                            /> 
                </StackPanel> 
            </HierarchicalDataTemplate> 
        </UserControl.Resources> 

    So, now how do I set up the Template now that my Source is actually an Image instead of an image source?  As it stands, I currently get no image with my TreeViewItem
  4. Joel Palmer
    Joel Palmer avatar
    170 posts
    Member since:
    May 2009

    Posted 04 Jan 2010 Link to this post

    I get to answer my own question.  I went another direction and used the BitmapImage instead of the Image:

            S88 _name;  
            public S88 Name   
            {  
                get { return _name; }  
                set  
                {  
                    _name = value;  
                    setIcon();  
                }  
            }  
     
            public BitmapImage Icon { get; set; } 

    Using the Template above, I now get images.  Thanks for your help.
Back to Top