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.
I’m assuming that
imgis a standardImagecontrol.Your
Imageproperty is of typeBitmapImage, which is a specific kind ofImageSource.CreateBitmapSourceFromHIconreturns an instance of an internal class calledInteropBitmap, which cannot be converted toBitmapImage, resulting in an error.You need to change you property to
ImageSource(orBitmapSource, whichCreateBitmapSourceFromHIconreturns, and inheritsImageSource), like this: