Files
Common.Wpf/Extensions/IconExtensions.cs

31 lines
872 B
C#

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Common.Wpf.Extensions
{
public static class IconExtensions
{
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static ImageSource ToImageSource(this Icon icon)
{
var bitmap = icon.ToBitmap();
var hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
if (!DeleteObject(hBitmap))
throw new Win32Exception();
return wpfBitmap;
}
}
}