tags:

views:

235

answers:

1

I have a tree that displays the directory and another panel that displays the files. Right now the files displayed have no icons. All i know is the path to the file. What i woudl like to do is get that files icon to display in that panel. I need the output to be and Image.source. Currently this is what i have

    private ImageSource GetIcon(string filename)
    {
        System.Drawing.Icon extractedIcon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
        ImageSource imgs;

        using (System.Drawing.Icon i = System.Drawing.Icon.FromHandle(extractedIcon.ToBitmap().GetHicon()))
            {
                imgs = Imaging.CreateBitmapSourceFromHIcon(
                                        i.Handle,
                                        new Int32Rect(0, 0, 16, 16),
                                        BitmapSizeOptions.FromEmptyOptions());
            }

        return imgs;

From there i call my itme and try to change its default icon with:

ImageSource i = GetIcon(f.fullname)
ic.image = i

ic is the given item to the list, f.fullname contains the path here is the get and set of image

        public BitmapImage Image
        {
            get { return (BitmapImage)img.Source; }
            set { img.Source = value; }
        }

It doesn't work and this is one of many ways I've tried it says it cant cast the different types. Does anyone have a way to do this?
I'm completely lost.

+1  A: 

I'm assuming that img is a standard Image control.

Your Image property is of type BitmapImage, which is a specific kind of ImageSource. CreateBitmapSourceFromHIcon returns an instance of an internal class called InteropBitmap, which cannot be converted to BitmapImage, resulting in an error.

You need to change you property to ImageSource (or BitmapSource, which CreateBitmapSourceFromHIcon returns, and inherits ImageSource), like this:

public ImageSource Image
{
    get { return img.Source; }
    set { img.Source = value; }
}
SLaks