Reverted HBITMAP.ToBitmap() extension to just call Image.FromHBITMAP to avoid flipping until addressed (#202)

pull/211/head
dahall 2021-01-29 14:47:34 -07:00
parent a6d225b6b8
commit 06a42c155c
1 changed files with 30 additions and 29 deletions

View File

@ -78,40 +78,41 @@ namespace Vanara.PInvoke
/// <summary>Creates a <see cref="Bitmap"/> from an <see cref="HBITMAP"/> preserving transparency, if possible.</summary> /// <summary>Creates a <see cref="Bitmap"/> from an <see cref="HBITMAP"/> preserving transparency, if possible.</summary>
/// <param name="hbmp">The HBITMAP value.</param> /// <param name="hbmp">The HBITMAP value.</param>
/// <returns>The Bitmap instance. If <paramref name="hbmp"/> is a <c>NULL</c> handle, <see langword="null"/> is returned.</returns> /// <returns>The Bitmap instance. If <paramref name="hbmp"/> is a <c>NULL</c> handle, <see langword="null"/> is returned.</returns>
public static Bitmap ToBitmap(this in HBITMAP hbmp) public static Bitmap ToBitmap(this in HBITMAP hbmp) => Image.FromHbitmap((IntPtr)hbmp);
{ // TODO: Fix code below to process different bpp bitmaps w/o flipping
const System.Drawing.Imaging.PixelFormat fmt = System.Drawing.Imaging.PixelFormat.Format32bppArgb; //{
// const System.Drawing.Imaging.PixelFormat fmt = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
// If hbmp is NULL handle, return null // // If hbmp is NULL handle, return null
if (hbmp.IsNull) return null; // if (hbmp.IsNull) return null;
// Get detail and bail if not 32bit, empty or an old style BMP // // Get detail and bail if not 32bit, empty or an old style BMP
var (bpp, width, height, scanBytes, bits, isdib) = GetInfo(hbmp); // var (bpp, width, height, scanBytes, bits, isdib) = GetInfo(hbmp);
if (bpp != Image.GetPixelFormatSize(fmt) || height == 0 || !isdib) // if (bpp != Image.GetPixelFormatSize(fmt) || height == 0 || !isdib)
return Image.FromHbitmap((IntPtr)hbmp); // return Image.FromHbitmap((IntPtr)hbmp);
// Create bitmap from detail and flip if upside-down // // Create bitmap from detail and flip if upside-down
var bmp = new Bitmap(width, height, scanBytes, fmt, bits); // var bmp = new Bitmap(width, height, scanBytes, fmt, bits);
if (height < 0) bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); // if (height < 0) bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
return bmp; // return bmp;
static (ushort bpp, int width, int height, int scanBytes, IntPtr bits, bool isdib) GetInfo(in HBITMAP hbmp) // static (ushort bpp, int width, int height, int scanBytes, IntPtr bits, bool isdib) GetInfo(in HBITMAP hbmp)
{ // {
var dibSz = Marshal.SizeOf(typeof(DIBSECTION)); // var dibSz = Marshal.SizeOf(typeof(DIBSECTION));
using var mem = GetObject(hbmp, dibSz); // using var mem = GetObject(hbmp, dibSz);
if (mem.Size == dibSz) // if (mem.Size == dibSz)
{ // {
var dib = mem.ToStructure<DIBSECTION>(); // var dib = mem.ToStructure<DIBSECTION>();
return (dib.dsBm.bmBitsPixel, dib.dsBmih.biWidth, dib.dsBmih.biHeight, dib.dsBm.bmWidthBytes, dib.dsBm.bmBits, true); // return (dib.dsBm.bmBitsPixel, dib.dsBmih.biWidth, dib.dsBmih.biHeight, dib.dsBm.bmWidthBytes, dib.dsBm.bmBits, true);
} // }
else // else
{ // {
var bmp = mem.ToStructure<BITMAP>(); // var bmp = mem.ToStructure<BITMAP>();
return (bmp.bmBitsPixel, bmp.bmWidth, bmp.bmHeight, bmp.bmWidthBytes, bmp.bmBits, false); // return (bmp.bmBitsPixel, bmp.bmWidth, bmp.bmHeight, bmp.bmWidthBytes, bmp.bmBits, false);
} // }
} // }
} //}
#if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1 #if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1
/// <summary>Creates a <see cref="System.Windows.Media.Imaging.BitmapSource"/> from an <see cref="HBITMAP"/> preserving transparency, if possible.</summary> /// <summary>Creates a <see cref="System.Windows.Media.Imaging.BitmapSource"/> from an <see cref="HBITMAP"/> preserving transparency, if possible.</summary>