将 GDI 位图转换为灰度.ahk

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=81203
#NoEnv
#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%

If !FileExist("bluebottlefly.jpg")
   URLDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=10624
                    , bluebottlefly.jpg

hBM1 := LoadPicture("bluebottlefly.jpg")
hBM2 := ToGrayscale(hBM1)

; hBM2 := FsGrayscale()  ; 全屏截图转灰色写法

Gui, Margin, 24, 24
Gui, Add, Picture,,    HBITMAP:%hBM1%
Gui, Add, Picture,x+m, HBITMAP:%hBM2%
Gui, Show,, ToGrayscale()
Return

; hBM转灰度
ToGrayscale(sBM) {  ; By SKAN on CR7J/D39L @ tiny.cc/tograyscale
  Local  ; Original ver. GDI_GrayscaleBitmap() @ https://autohotkey.com/board/topic/82794-

  P8:=(A_PtrSize=8),  VarSetCapacity(BM,P8? 32:24, 0)
  DllCall("GetObject", "Ptr",sBM, "Int",P8? 32:24, "Ptr",&BM)
  W := NumGet(BM,4,"Int"), H := NumGet(BM,8,"Int")
  sDC := DllCall( "CreateCompatibleDC", "Ptr",0, "Ptr")

  DllCall("DeleteObject", "Ptr",DllCall("SelectObject", "Ptr",sDC, "Ptr",sBM, "Ptr"))

  tBM := DllCall( "CopyImage", "Ptr"
       , DllCall( "CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",8, "Ptr",0, "Ptr")
       , "Int",0, "Int",W, "Int",H, "Int",0x2008, "Ptr")

  tDC := DllCall("CreateCompatibleDC", "Ptr",0, "Ptr")
  DllCall("DeleteObject", "Ptr",DllCall("SelectObject", "Ptr",tDC, "Ptr",tBM, "Ptr"))

  Loop % (255, n:=0x000000, VarSetCapacity(RGBQUAD256,256*4,0))
    Numput(n+=0x010101, RGBQUAD256, A_Index*4, "Int")
  DllCall("SetDIBColorTable", "Ptr",tDC, "Int",0, "Int",256, "Ptr",&RGBQUAD256)

  DllCall("BitBlt",   "Ptr",tDC, "Int",0, "Int",0, "Int",W, "Int",H
                    , "Ptr",sDC, "Int",0, "Int",0, "Int",0x00CC0020)

  Return % (tBM, DllCall("DeleteDC", "Ptr",sDC), DllCall("DeleteDC", "Ptr",tDC))
}

; 截图转灰度
FsGrayscale(ByRef X1:=0, ByRef Y1:=0, ByRef X2:=0, ByRef Y2:=0, ByRef W:=0, ByRef H:=0) {
  Local ; Fullscreen grayscale capture.      Original: ToGrayscale() @ tiny.cc/tograyscale
  VarSetCapacity(WININFO,62), NumPut(62,WININFO,"Int"), PW:=&WININFO
  DllCall("GetWindowInfo", "Ptr",hWnd:=DllCall("GetDesktopWindow", "Ptr"), "Ptr",&WININFO)
  Loop, Parse, % "X1.Y1.X2.Y2.W.H",.
    %A_LoopField% := (A_Index<5 ? NumGet(PW+0,A_Index*4,"Int") : A_Index=5 ? X2-X1 : Y2-Y1)

  tBM := DllCall( "CopyImage", "Ptr"
       , DllCall( "CreateBitmap", "Int",1, "Int",1, "Int",0x1, "Int",8, "Ptr",0, "Ptr")
       , "Int",0, "Int",W, "Int",H, "Int",0x2008, "Ptr")

  tDC := DllCall("CreateCompatibleDC", "Ptr",0, "Ptr")
  DllCall("DeleteObject", "Ptr",DllCall("SelectObject", "Ptr",tDC, "Ptr",tBM, "Ptr"))
  Loop % (255, n:=0x000000, VarSetCapacity(RGBQUAD256,256*4,0))
        Numput(n+=0x010101, RGBQUAD256, A_Index*4, "Int")
  DllCall("SetDIBColorTable", "Ptr",tDC, "Int",0, "Int",256, "Ptr",&RGBQUAD256)

  sDC := DllCall("GetDC", "Ptr",hWnd, "Ptr")
  DllCall("BitBlt", "Ptr",tDC, "Int",0,  "Int",0,  "Int",W, "Int",H
                  , "Ptr",sDC, "Int",X1, "Int",Y1, "Int",0x00CC0020)

  Return (tBM, DllCall("ReleaseDC", "Ptr",hWnd, "Ptr",sDC),  DllCall("DeleteDC", "Ptr",tDC))
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。