From 06a42c155c9f109b902450779febd94d8687c321 Mon Sep 17 00:00:00 2001 From: dahall Date: Fri, 29 Jan 2021 14:47:34 -0700 Subject: [PATCH] Reverted HBITMAP.ToBitmap() extension to just call Image.FromHBITMAP to avoid flipping until addressed (#202) --- PInvoke/Gdi32/GdiObjExtensions.cs | 59 ++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/PInvoke/Gdi32/GdiObjExtensions.cs b/PInvoke/Gdi32/GdiObjExtensions.cs index c2a0156f..98dfc0b1 100644 --- a/PInvoke/Gdi32/GdiObjExtensions.cs +++ b/PInvoke/Gdi32/GdiObjExtensions.cs @@ -78,40 +78,41 @@ namespace Vanara.PInvoke /// Creates a from an preserving transparency, if possible. /// The HBITMAP value. /// The Bitmap instance. If is a NULL handle, is returned. - public static Bitmap ToBitmap(this in HBITMAP hbmp) - { - const System.Drawing.Imaging.PixelFormat fmt = System.Drawing.Imaging.PixelFormat.Format32bppArgb; + 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; - // If hbmp is NULL handle, return null - if (hbmp.IsNull) return null; + // // If hbmp is NULL handle, return null + // if (hbmp.IsNull) return null; - // Get detail and bail if not 32bit, empty or an old style BMP - var (bpp, width, height, scanBytes, bits, isdib) = GetInfo(hbmp); - if (bpp != Image.GetPixelFormatSize(fmt) || height == 0 || !isdib) - return Image.FromHbitmap((IntPtr)hbmp); + // // Get detail and bail if not 32bit, empty or an old style BMP + // var (bpp, width, height, scanBytes, bits, isdib) = GetInfo(hbmp); + // if (bpp != Image.GetPixelFormatSize(fmt) || height == 0 || !isdib) + // return Image.FromHbitmap((IntPtr)hbmp); - // Create bitmap from detail and flip if upside-down - var bmp = new Bitmap(width, height, scanBytes, fmt, bits); - if (height < 0) bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); - return bmp; + // // Create bitmap from detail and flip if upside-down + // var bmp = new Bitmap(width, height, scanBytes, fmt, bits); + // if (height < 0) bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); + // return bmp; - static (ushort bpp, int width, int height, int scanBytes, IntPtr bits, bool isdib) GetInfo(in HBITMAP hbmp) - { - var dibSz = Marshal.SizeOf(typeof(DIBSECTION)); - using var mem = GetObject(hbmp, dibSz); - if (mem.Size == dibSz) - { - var dib = mem.ToStructure(); - return (dib.dsBm.bmBitsPixel, dib.dsBmih.biWidth, dib.dsBmih.biHeight, dib.dsBm.bmWidthBytes, dib.dsBm.bmBits, true); - } - else - { - var bmp = mem.ToStructure(); - return (bmp.bmBitsPixel, bmp.bmWidth, bmp.bmHeight, bmp.bmWidthBytes, bmp.bmBits, false); - } - } + // static (ushort bpp, int width, int height, int scanBytes, IntPtr bits, bool isdib) GetInfo(in HBITMAP hbmp) + // { + // var dibSz = Marshal.SizeOf(typeof(DIBSECTION)); + // using var mem = GetObject(hbmp, dibSz); + // if (mem.Size == dibSz) + // { + // var dib = mem.ToStructure(); + // return (dib.dsBm.bmBitsPixel, dib.dsBmih.biWidth, dib.dsBmih.biHeight, dib.dsBm.bmWidthBytes, dib.dsBm.bmBits, true); + // } + // else + // { + // var bmp = mem.ToStructure(); + // return (bmp.bmBitsPixel, bmp.bmWidth, bmp.bmHeight, bmp.bmWidthBytes, bmp.bmBits, false); + // } + // } - } + //} #if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1 /// Creates a from an preserving transparency, if possible.