I created an application in C# that gets icon images from window handles using user32.dll like this:
[DllImport("user32.dll", EntryPoint = "GetClassLong")]
private static extern int GetClassLongPtr32(IntPtr hWnd, int nIndex);
public static IntPtr GetAppIcon(IntPtr hwnd)
{
return WI.GetClassLongPtr32(hwnd, WI.ICON_SMALL);
}
And I want to create a BitmapSource from this icon pointer. Usually for WPF I would use
Imaging.CreateBitmapSourceFromHIcon(handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
But since I need the BitmapSource to draw it in a Direct2D render target I would need to use DirectX's BitmapSource
Microsoft.WindowsAPICodePack.DirectX.WindowImagingComponent.BitmapSource
How can I create this kind of BitmapSource using the icon handle or can I transfer one BitmapSource type to the other?
The
ID2D1DeviceContext
has a methodCreateBitmapFromWicBitmap
.With its help you can create an
ID2D1Bitmap
. The only thing you have to do is to create anIWICBitmap
from yourHICON
and then create anIWICFormatConverter
, so you can keep the alpha channel. You can do it this way (The snippet from below is a delphi one but in C# should be very similar):