Convert System.Drawing.Icon to System.Media.ImageSource (将System.Drawing.Icon 转成 System.Media.ImageSource)
WPF
Properties [Resources.resx] 资源
引入 *.Icon文件时 资源为System.Drawing.Icon
TreeView 自定义结点 有Image元素 需要绑定 Source => System.Media.ImageSource
所以需要 将System.Drawing.Icon 转成 System.Media.ImageSource
internal static class IconUtilities
{
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource(this Icon icon)
{
Bitmap bitmap = icon.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
}
return wpfBitmap;
}
}
转载自(http://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource)