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.
To add an icon to a project, right click on the project name in Solution Explorer. Right click will open Add Items menu. Select Add >> New Item. Now on Installed Templates, select Icon File (see Figure 1) and click Add.
When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.
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; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With