I have been searching in Google and Stack Overflow, and I cannot find a working example.
我一直在谷歌和Stack Overflow中搜索,找不到一个有效的示例。
I need to convert a HBitmap to a Managed .NET bitmap, but the following code does not preserve the alpha channel.
我需要将一个HBitmap转换为一个托管的。net位图,但是下面的代码没有保留alpha通道。
private static Bitmap GetBitmapFromHBitmap(IntPtr nativeHBitmap)
{
Bitmap bmp = Bitmap.FromHbitmap(nativeHBitmap);
return bmp;
}
I found this answer in SO, but it does not work for me, the example preserves the transparency, however it flips my image 180º in Y axis and also rotate it 180º. I don't know why.
所以我找到了这个答案,但它不为我工作,保留了透明的例子中,然而它会翻转我的形象180ºY轴旋转180º。我不知道为什么。
This other example seems to work, but it is C++
另一个示例似乎可以工作,但它是c++
Someone has this working in C#, and very important, without memory leaks?
有人在c#中工作,非常重要,没有内存泄漏?
Thanks in advance.
提前谢谢。
EDIT: Regarding the comment from @Hans Passant, I use the following code to get the HBitmap (it's a shell call to get the thumbnail or icon from the OS (only Vista and Win7).
编辑:关于来自@Hans Passant的评论,我使用以下代码获取HBitmap(这是一个shell调用,从OS获取缩略图或图标(只有Vista和Win7)。
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
{
IShellItem nativeShellItem;
Guid shellItem2Guid = new Guid(IShellItem2Guid);
int retCode = SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out nativeShellItem);
NativeSize nativeSize = new NativeSize();
nativeSize.Width = width;
nativeSize.Height = height;
IntPtr hBitmap;
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);
Marshal.ReleaseComObject(nativeShellItem);
if (hr == HResult.Ok) return hBitmap;
throw Marshal.GetExceptionForHR((int) hr);
}
1 个解决方案
#1
1
Please take a look at Preserving the alpha channel when converting images. It seems that it is your case.
在转换图像时,请注意保存alpha通道。似乎是你的情况。
#1
1
Please take a look at Preserving the alpha channel when converting images. It seems that it is your case.
在转换图像时,请注意保存alpha通道。似乎是你的情况。