mirror of
https://github.com/ckaczor/Common.Wpf.git
synced 2026-01-13 17:22:47 -05:00
31 lines
872 B
C#
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;
|
|
}
|
|
}
|
|
}
|