将System.Drawing.Icon转换为System.Windows.Controls.Image

时间:2013-10-28 07:04:10

标签: c# wpf

我想在Image控件中显示一个图标,我该怎么做。

System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(@"C:\Users\bgh32648\Desktop\Filename.extension");

Image Img = new Image();

我想将图标分配给img

1 个答案:

答案 0 :(得分:3)

public static ImageSource ToImageSource(this Icon icon)
{
    ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
        icon.Handle,
        Int32Rect.Empty,
        BitmapSizeOptions.FromEmptyOptions());

    return imageSource;
}

然后image.Source = icon.ToImageSource();