;이미지 파일에서 메모리로 이미지 로딩하기

bmpFile = %A_ScriptDir%\Image\IL_Other.bmp

hBitmap := DllCall("USER32\LoadImage", Ptr,0, Str,bmpFile, Uint,0, Int,0, Int,0, Uint,0x10 | 0x2000, Ptr)


;----------------------------------------------------------------------------------
;클립보드에서 비트맵 복사 후 HBitmap 핸들로 반환

;www.autohotkey.com/forum/viewtopic.php?t=35242
CreateHBitmapFromClipboard()
{
  If DllCall("IsClipboardFormatAvailable", UInt,2 )
    DllCall( "OpenClipboard", UInt,0 ), tBM := DllCall("GetClipboardData", UInt,2 )
    , hBM := DllCall( "CopyImage", UInt,tBM, UInt,0, Int,0,Int,0, UInt,0x2000, UInt )
    , DllCall( "CloseClipboard" )
  return hBM ? hBM : 0
}

 

;----------------------------------------------------------------------------------

;메모리에 로딩된 BMP파일을 읽어 HBitmap핸들로 반환
CreateHBitmapFromVar(ByRef bmpVar)
{
  WinGet, hwnd, ID, ahk_class Progman
  hDC := DllCall("USER32\GetDC", Ptr,hwnd, Ptr)
  hBMP := DllCall( "CreateDIBitmap", UInt,hDC, UInt,(bmiHAddr:=&bmpVar+14)
       ,UInt,(CBM_INIT:=4), UInt,&bmpVar+NumGet(&bmpVar,10), UInt,&bmpVar+14, UInt,(DIB_PAL_COLORS:=1) )
  DllCall("ReleaseDC", Ptr,hwnd, Ptr,hDC, Uint)
  return hBMP
}

 

;----------------------------------------------------------------------------------

;메모리에 로딩된 BMP파일을 읽어 pBitmap핸들로 반환 (Gdip 라이브러리 필요)
CreateBitmapFromVar(ByRef bmpVar)
{
  WinGet, hwnd, ID, ahk_class Progman
  hDC := DllCall("USER32\GetDC", Ptr,hwnd, Ptr)
  hBMP := DllCall( "CreateDIBitmap", UInt,hDC, UInt,(bmiHAddr:=&bmpVar+14)
       ,UInt,(CBM_INIT:=4), UInt,&bmpVar+NumGet(&bmpVar,10), UInt,&bmpVar+14, UInt,(DIB_PAL_COLORS:=1) )
  pBitmap := Gdip_CreateBitmapFromHBITMAP(hBMP)
  DllCall("ReleaseDC", Ptr,hwnd, Ptr,hDC, Uint)
  DllCall("DeleteObject", Ptr,hBMP, Uint)
  return pBitmap
}

 

;----------------------------------------------------------------------------------

;비트맵을 파일로 저장
SaveHBITMAPToFile(hBitmap, sFile)
{
  VarSetCapacity(DIBSECTION, A_PtrSize=8? 104:84, 0)
  NumPut(40, DIBSECTION, A_PtrSize=8? 32:24,"UInt") ;dsBmih.biSize
  DllCall("GetObject", "UPTR", hBitmap, "int", A_PtrSize=8? 104:84, "UPTR", &DIBSECTION)
  hFile:= DllCall("CreateFile", "UPTR", &sFile, "Uint", 0x40000000, "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 0, "Uint", 0)
  DllCall("WriteFile", "UPTR", hFile, "int64P", 0x4D42|14+40+(biSizeImage:=NumGet(DIBSECTION, A_PtrSize=8? 52:44, "UInt"))<<16, "Uint", 6, "UintP", 0, "Uint", 0)
  DllCall("WriteFile", "UPTR", hFile, "int64P", 54<<32, "Uint", 8, "UintP", 0, "Uint", 0)
  DllCall("WriteFile", "UPTR", hFile, "UPTR", &DIBSECTION + (A_PtrSize=8? 32:24), "Uint", 40, "UintP", 0, "Uint", 0)
  DllCall("WriteFile", "UPTR", hFile, "Uint", NumGet(DIBSECTION, A_PtrSize=8? 24:20, "UPtr"), "Uint", biSizeImage, "UintP", 0, "Uint", 0)
  DllCall("CloseHandle", "UPTR", hFile)
}

 

Posted by 와이로