mirror of
https://github.com/ckaczor/Common.Wpf.git
synced 2026-01-25 09:35:37 -05:00
Add generic category window to WPF library
This commit is contained in:
30
Extensions/IconExtensions.cs
Normal file
30
Extensions/IconExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user