从给定路径加载图标在WPF窗口中显示 [英] Loading Icons from a given path to display in WPF window

查看:871
本文介绍了从给定路径加载图标在WPF窗口中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示目录和显示文件的另一个面板的树。现在显示的文件都没有图标。我所知道的是文件路径。我woudl喜欢做的就是将文件图标,在该面板中显示。我需要的输出是和Image.source。目前,这是我所

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 是给定项目清单,f.fullname包含路径$ b $这里b是获取和设置图像

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; }
        }



它不工作,这是很多方面我已经1试了一下说,它不能施放不同的类型。有没有人有办法做到这一点?结果
我完全失去了。

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.

推荐答案

我假设 IMG 是一个标准的图片控制。

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

你的图片属性的类型为的BitmapImage ,这是一种特定的的ImageSource CreateBitmapSourceFromHIcon 返回 InteropBitmap 称为内部类,它不能被转换为 BitmapImage的<实例/ code>,从而导致错误。

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.

您需要属性更改你的ImageSource (或的BitmapSource ,其中 CreateBitmapSourceFromHIcon 的回报,并继承了的ImageSource ),像这样的:

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; }
}

这篇关于从给定路径加载图标在WPF窗口中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆