问题描述
我创建了C#处理使用user32.dll中像这样的窗口得到的图标图像的应用程序:
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);
}
和我想创建该图标指针的BitmapSource。通常对于WPF我会用
And I want to create a BitmapSource from this icon pointer. Usually for WPF I would use
Imaging.CreateBitmapSourceFromHIcon(handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
但因为我需要的BitmapSource绘制在一个的Direct2D渲染目标,我需要使用DirectX的的BitmapSource
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
我怎样才能创造这样的BitmapSource的使用图标手柄或者我可以传送一个的BitmapSource类型其他?
How can I create this kind of BitmapSource using the icon handle or can I transfer one BitmapSource type to the other?
推荐答案
在 ID2D1DeviceContext
有一个方法 CreateBitmapFromWicBitmap
。
有了它的帮助,你可以创建一个 ID2D1Bitmap
。你所要做的唯一事情就是创建一个 IWICBitmap
从惠康
,然后创建一个 IWICFormatConverter
,这样可以保持alpha通道。你可以这样来做(从下面的代码片段是一个Delphi之一,但在C#中应该是非常相似的的):
With its help you can create an ID2D1Bitmap
. The only thing you have to do is to create an IWICBitmap
from your HICON
and then create an IWICFormatConverter
, 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):
procedure iconToD2D1Bitmap;
var
hIcon: HICON;
wicBitmap: IWICBitmap;
wicConverter: IWICFormatConverter;
wicFactory: IWICImagingFactory;
bitmapProps: D2D1_BITMAP_PROPERTIES1;
bitmap: ID2D1Bitmap1;
begin
// get a HICON
hIcon := SendMessage(Handle, WM_GETICON, ICON_BIG, 0);
try
// create wic imaging factory
CoCreateInstance(CLSID_WICImagingFactory, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IUnknown, wicFactory);
wicFactory.CreateBitmapFromHICON(hIcon, wicBitmap);
wicFactory.CreateFormatConverter(wicConverter);
wicConverter.Initialize(wicBitmap, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nil, 0, WICBitmapPaletteTypeMedianCut);
bitmapProps.bitmapOptions := D2D1_BITMAP_OPTIONS_NONE;
bitmapProps.pixelFormat.format := DXGI_FORMAT_B8G8R8A8_UNORM;
bitmapProps.pixelFormat.alphaMode := D2D1_ALPHA_MODE_PREMULTIPLIED;
bitmapProps.dpiX := 96;
bitmapProps.dpiY := 96;
bitmapProps.colorContext := nil;
// deviceContext should be a valid D2D1DeviceContext
deviceContext.CreateBitmapFromWicBitmap(wicConverter, @bitmapProps, bitmap);
// the bitmap variable contains your icon
except
//
end;
end;
这篇关于从一个Win32图标指示创建的Direct2D的BitmapSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!