彩色图片转成灰阶.ahk

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

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

hBM1 := LoadPicture("C:\Users\Dianbo\Desktop\QQ截图20221213172001.png")
计时()
hBM2 := ToGrayscale(hBM1)
MsgBox % "耗时:" 计时() " 毫秒"

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


计时() {
  Static
  if 开始=
    DllCall("QueryPerformanceFrequency", "Int64*", 频率), DllCall("QueryPerformanceCounter", "Int64*", 开始)
   else
    DllCall("QueryPerformanceCounter", "Int64*", 结束), 耗时:=(结束 - 开始)/频率*1000, 开始:=""
  Return 耗时
}

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))
}

 

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