Assume that image is the variable that is a handle to the bitmap (hBitmap).Helgef wrote:
Code: Select all
; struct BITMAP - https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/ns-wingdi-tagbitmap
DllCall("GetObject"
, "ptr", image
, "int", VarSetCapacity(dib, 76+2*(A_PtrSize=8?4:0)+2*A_PtrSize)
, "ptr", &dib) ; sizeof(DIBSECTION) = x86:84, x64:104
width := NumGet(dib, 4, "uint")
height := NumGet(dib, 8, "uint")
bpp := NumGet(dib, 18, "ushort")
; Fallback to built-in method if pixels are not ARGB.
if (bpp != 32)
return Gdip_CreateBitmapFromHBITMAP(image)
; Create a handle to a device context and associate the image.
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, image)
; Buffer the image with a top-down device independent bitmap via negative height.
; Note that a DIB is an hBitmap, pixels are formatted as pARGB, and has a pointer to the bits.
cdc := CreateCompatibleDC(hdc)
hbm := CreateDIBSection(width, -height, hdc, 32, pBits)
ob2 := SelectObject(cdc, hbm)
; Create a new Bitmap (different from an hBitmap) which holds ARGB pixel values.
pBitmap := Gdip_CreateBitmap(width, height)
; Create a Scan0 buffer pointing to pBits. The buffer has pixel format pARGB.
VarSetCapacity(Rect, 16, 0)
, NumPut( width, Rect, 8, "uint")
, NumPut(height, Rect, 12, "uint")
VarSetCapacity(BitmapData, 16+2*(A_PtrSize ? A_PtrSize : 4), 0)
, NumPut( width, BitmapData, 0, "uint") ; Width
, NumPut( height, BitmapData, 4, "uint") ; Height
, NumPut( 4 * width, BitmapData, 8, "int") ; Stride
, NumPut( 0xE200B, BitmapData, 12, "int") ; PixelFormat
, NumPut( pBits, BitmapData, 16, "ptr") ; Scan0
DllCall("gdiplus\GdipBitmapLockBits"
, "ptr", pBitmap
, "ptr", &Rect
, "uint", 7 ; ImageLockMode.UserInputBuffer | ImageLockMode.ReadWrite
, "int", 0xE200B ; Format32bppPArgb
, "ptr", &BitmapData)
; Ensure that our hBitmap (image) is top-down by copying it to a top-down bitmap.
BitBlt(cdc, 0, 0, width, height, hdc, 0, 0)
; Convert the pARGB pixels copied into the device independent bitmap (hbm) to ARGB.
DllCall("gdiplus\GdipBitmapUnlockBits", "ptr",pBitmap, "ptr",&BitmapData)
; Cleanup the buffer and device contexts.
SelectObject(cdc, ob2)
DeleteObject(hbm)
DeleteDC(cdc)
SelectObject(hdc, obm)
DeleteDC(hdc)
return pBitmap