Add generic category window to WPF library

This commit is contained in:
2015-11-10 20:28:58 -05:00
parent 8e24cd0b12
commit f7ddae093e
5 changed files with 216 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
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;
}
}
}