iseahound

ImagePut #5 - Does CaptureBLT make it slower? NO.

Sep 8th, 2020
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Load GDI+
  2.  
  3.       DllCall("LoadLibrary", "str", "gdiplus")
  4.       VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0) ; sizeof(GdiplusStartupInput) = 16, 24
  5.          , NumPut(0x1, si, "uint")
  6.       DllCall("gdiplus\GdiplusStartup", "ptr*", pToken:=0, "ptr", &si, "ptr", 0)
  7.  
  8.  
  9. Loop 1000 {
  10.    QPC(1)
  11.    pBitmap := from_screenshot0()
  12.    old += QPC()
  13.    DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
  14.  
  15.  
  16. ; ------------------
  17.    QPC(1)
  18.    pBitmap := from_screenshot1()
  19.    new += QPC()
  20.    DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
  21.  
  22. }
  23.  
  24. if (old > new)
  25.    winner := "New Version wins by " old - new " and is " Abs(old - new)/new*100 "% faster."
  26. else
  27.    winner := "Old Version wins by " new - old " and is " Abs(old - new)/old*100 "% faster."
  28.  
  29. MsgBox % "Old Version:`t" old "`nNew Version:`t" new "`n" winner
  30. return
  31.  
  32. Esc:: ExitApp
  33.  
  34. QPC( R := 0 ) {    ; By SKAN,  http://goo.gl/nf7O4G,  CD:01/Sep/2014 | MD:01/Sep/2014
  35.   Static P := 0,  F := 0,     Q := DllCall( "QueryPerformanceFrequency", "Int64P",F )
  36. Return ! DllCall( "QueryPerformanceCounter","Int64P",Q ) + ( R ? (P:=Q)/F : (Q-P)/F )
  37. }
  38.  
  39. from_screenshot0() {
  40.  
  41.       image := [0,0,A_ScreenWidth,A_ScreenHeight]
  42.  
  43.       ; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
  44.       hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
  45.       VarSetCapacity(bi, 40, 0)                ; sizeof(bi) = 40
  46.          , NumPut(       40, bi,  0,   "uint") ; Size
  47.          , NumPut( image[3], bi,  4,   "uint") ; Width
  48.          , NumPut(-image[4], bi,  8,    "int") ; Height - Negative so (0, 0) is top-left.
  49.          , NumPut(        1, bi, 12, "ushort") ; Planes
  50.          , NumPut(       32, bi, 14, "ushort") ; BitCount / BitsPerPixel
  51.       hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", &bi, "uint", 0, "ptr*", pBits:=0, "ptr", 0, "uint", 0, "ptr")
  52.       obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
  53.  
  54.       ; Retrieve the device context for the screen.
  55.       sdc := DllCall("GetDC", "ptr", 0, "ptr")
  56.  
  57.       ; Copies a portion of the screen to a new device context.
  58.       DllCall("gdi32\BitBlt"
  59.                , "ptr", hdc, "int", 0, "int", 0, "int", image[3], "int", image[4]
  60.                , "ptr", sdc, "int", image[1], "int", image[2], "uint", 0x00CC0020) ; SRCCOPY
  61.  
  62.       ; Release the device context to the screen.
  63.       DllCall("ReleaseDC", "ptr", 0, "ptr", sdc)
  64.  
  65.       ; Convert the hBitmap to a Bitmap using a built in function as there is no transparency.
  66.       DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", hbm, "ptr", 0, "ptr*", pBitmap:=0)
  67.  
  68.       ; Cleanup the hBitmap and device contexts.
  69.       DllCall("SelectObject", "ptr", hdc, "ptr", obm)
  70.       DllCall("DeleteObject", "ptr", hbm)
  71.       DllCall("DeleteDC",     "ptr", hdc)
  72.  
  73.       return pBitmap
  74. }
  75.  
  76. from_screenshot1() {
  77.  
  78.       image := [0,0,A_ScreenWidth,A_ScreenHeight]
  79.  
  80.       ; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
  81.       hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
  82.       VarSetCapacity(bi, 40, 0)                ; sizeof(bi) = 40
  83.          , NumPut(       40, bi,  0,   "uint") ; Size
  84.          , NumPut( image[3], bi,  4,   "uint") ; Width
  85.          , NumPut(-image[4], bi,  8,    "int") ; Height - Negative so (0, 0) is top-left.
  86.          , NumPut(        1, bi, 12, "ushort") ; Planes
  87.          , NumPut(       32, bi, 14, "ushort") ; BitCount / BitsPerPixel
  88.       hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", &bi, "uint", 0, "ptr*", pBits:=0, "ptr", 0, "uint", 0, "ptr")
  89.       obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
  90.  
  91.       ; Retrieve the device context for the screen.
  92.       sdc := DllCall("GetDC", "ptr", 0, "ptr")
  93.  
  94.       ; Copies a portion of the screen to a new device context.
  95.       DllCall("gdi32\BitBlt"
  96.                , "ptr", hdc, "int", 0, "int", 0, "int", image[3], "int", image[4]
  97.                , "ptr", sdc, "int", image[1], "int", image[2], "uint", 0x00CC0020 | 0x40000000) ; SRCCOPY | CAPTUREBLT
  98.  
  99.       ; Release the device context to the screen.
  100.       DllCall("ReleaseDC", "ptr", 0, "ptr", sdc)
  101.  
  102.       ; Convert the hBitmap to a Bitmap using a built in function as there is no transparency.
  103.       DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", hbm, "ptr", 0, "ptr*", pBitmap:=0)
  104.  
  105.       ; Cleanup the hBitmap and device contexts.
  106.       DllCall("SelectObject", "ptr", hdc, "ptr", obm)
  107.       DllCall("DeleteObject", "ptr", hbm)
  108.       DllCall("DeleteDC",     "ptr", hdc)
  109.  
  110.       return pBitmap
  111. }
Add Comment
Please, Sign In to add comment