Get or Read Pixel from Memory UDF - PixelGetColor .au3


SUBMITTED BY: Guest

DATE: May 28, 2013, 10 a.m.

FORMAT: Text only

SIZE: 7.4 kB

HITS: 1866

  1. #include <ScreenCapture.au3>
  2. #include-once
  3. ; #FUNCTION# ;===============================================================================
  4. ;
  5. ; Name...........: _PixelGetColor_CreateDC
  6. ; Description ...: Creates a DC for use with the other _PixelGetColor functions.
  7. ; Syntax.........: _PixelGetColor_CreateDC()
  8. ; Parameters ....: None.
  9. ; Return values .: Success - Returns the handle to a compatible DC.
  10. ; Failure - Returns 0 and Sets @error according to @error from the DllCall.
  11. ; Author ........: Jos van Egmond
  12. ; Modified.......:
  13. ; Remarks .......:
  14. ; Related .......: _PixelGetColor_CaptureRegion, _PixelGetcolor_GetPixel, _PixelGetColor_GetPixelRaw, _PixelGetColor_ReleaseRegion, _PixelGet_Color_ReleaseDC
  15. ; Example .......; No
  16. ;
  17. ; ;==========================================================================================
  18. Func _PixelGetColor_CreateDC($hDll = "gdi32.dll")
  19. $iPixelGetColor_MemoryContext = DllCall($hDll, "int", "CreateCompatibleDC", "int", 0)
  20. If @error Then Return SetError(@error,0,0)
  21. Return $iPixelGetColor_MemoryContext[0]
  22. EndFunc
  23. ; #FUNCTION# ;===============================================================================
  24. ;
  25. ; Name...........: _PixelGetColor_CaptureRegion
  26. ; Description ...: Captures the user defined region and reads it to a memory DC.
  27. ; Syntax.........: _PixelGetColor_CaptureRegion($iPixelGetColor_MemoryContext, $iLeft = 0, $iTop = 0, $iRight = -1, $iBottom = -1, $fCursor = False)
  28. ; Parameters ....: $iPixelGetColor_MemoryContext - The DC as returned by _PixelGetColor_CreateDC
  29. ; $iLeft - Left side of the screen for use with the region
  30. ; $iTop - Top side of the screen for use with the region
  31. ; $iRight - Right side of the screen for use with the region
  32. ; $iBottom - Bottom side of the screen for use with the region
  33. ; $iCursor - If this is true, then the cursor is also read into memory
  34. ; Return values .: Success - Returns the handle to a region.
  35. ; Failure -
  36. ; Author ........: Jos van Egmond
  37. ; Modified.......:
  38. ; Remarks .......:
  39. ; Related .......: _PixelGet_Color_CreateDC, _PixelGetcolor_GetPixel, _PixelGetColor_GetPixelRaw, _PixelGetColor_ReleaseRegion, _PixelGet_Color_ReleaseDC
  40. ; Example .......; No
  41. ;
  42. ; ;==========================================================================================
  43. Func _PixelGetColor_CaptureRegion($iPixelGetColor_MemoryContext, $iLeft = 0, $iTop = 0, $iRight = -1, $iBottom = -1, $fCursor = False, $hDll = "gdi32.dll")
  44. $HBITMAP = _ScreenCapture_Capture("", $iLeft, $iTop, $iRight, $iBottom, $fCursor)
  45. DllCall($hDll, "hwnd", "SelectObject", "int", $iPixelGetColor_MemoryContext, "hwnd", $HBITMAP)
  46. Return $HBITMAP
  47. EndFunc
  48. ; #FUNCTION# ;===============================================================================
  49. ;
  50. ; Name...........: _PixelGetColor_GetPixel
  51. ; Description ...: Gets a pixel color from the DC in decimal BGR and converts it to RGB in 6 digit hexadecimal.
  52. ; Syntax.........: _PixelGetColor_GetPixel($iPixelGetColor_MemoryContext,$iX,$iY)
  53. ; Parameters ....: $iPixelGetColor_MemoryContext - The DC as returned by _PixelGetColor_CreateDC
  54. ; $iX - The X coordinate in the captured region
  55. ; $iY - The Y coordinate in the captured regoin
  56. ; Return values .: Success - Returns the 6 digit hex BGR color.
  57. ; Failure - Returns -1 and Sets @error to 1.
  58. ; Author ........: Jos van Egmond
  59. ; Modified.......:
  60. ; Remarks .......:
  61. ; Related .......: _PixelGetColor_CreateDC, _PixelGetColor_CaptureRegion, _PixelGetColor_GetPixelRaw, _PixelGetColor_ReleaseRegion, _PixelGet_Color_ReleaseDC
  62. ; Example .......; Yes
  63. ;
  64. ; ;==========================================================================================
  65. Func _PixelGetColor_GetPixel($iPixelGetColor_MemoryContext,$iX,$iY, $hDll = "gdi32.dll")
  66. $iColor = DllCall($hDll,"int","GetPixel","int",$iPixelGetColor_MemoryContext,"int",$iX,"int",$iY)
  67. If $iColor[0] = -1 then Return SetError(1,0,-1)
  68. $sColor = Hex($iColor[0],6)
  69. Return StringRight($sColor,2) & StringMid($sColor,3,2) & StringLeft($sColor,2)
  70. EndFunc
  71. ; #FUNCTION# ;===============================================================================
  72. ;
  73. ; Name...........: _PixelGetColor_GetPixelRaw
  74. ; Description ...: Gets a pixel color from the DC in decimal BGR.
  75. ; Syntax.........: _PixelGetColor_GetPixelRaw($iPixelGetColor_MemoryContext,$iX,$iY)
  76. ; Parameters ....: $iPixelGetColor_MemoryContext - The DC as returned by _PixelGetColor_CreateDC
  77. ; $iX - The X coordinate in the captured region
  78. ; $iY - The Y coordinate in the captured regoin
  79. ; Return values .: Success - Returns the color in decimal BGR.
  80. ; Failure - Returns -1 and Sets @error to 1.
  81. ; Author ........: Jos van Egmond
  82. ; Modified.......:
  83. ; Remarks .......:
  84. ; Related .......: _PixelGetColor_CreateDC, _PixelGetColor_CaptureRegion, _PixelGetColor_GetPixel, _PixelGetColor_ReleaseRegion, _PixelGet_Color_ReleaseDC
  85. ; Example .......; No
  86. ;
  87. ; ;==========================================================================================
  88. Func _PixelGetColor_GetPixelRaw($iPixelGetColor_MemoryContext,$iX,$iY, $hDll = "gdi32.dll")
  89. $iColor = DllCall($hDll,"int","GetPixel","int",$iPixelGetColor_MemoryContext,"int",$iX,"int",$iY)
  90. Return $iColor[0]
  91. EndFunc
  92. ; #FUNCTION# ;===============================================================================
  93. ;
  94. ; Name...........: _PixelGetColor_ReleaseRegion
  95. ; Description ...: Releases a region previously created by calling _PixelGetColor_CaptureRegion
  96. ; Syntax.........: _PixelGetColor_ReleaseRegion($HBITMAP)
  97. ; Parameters ....: $HBITMAP - Previously returned by _PixelGetColor_CaptureRegion
  98. ; Return values .: None.
  99. ; Author ........: Jos van Egmond
  100. ; Modified.......:
  101. ; Remarks .......:
  102. ; Related .......: _PixelGetColor_CreateDC, _PixelGetColor_CaptureRegion, _PixelGetcolor_GetPixel, _PixelGetColor_GetPixelRaw, _PixelGet_Color_ReleaseDC
  103. ; Example .......; No
  104. ;
  105. ; ;==========================================================================================
  106. Func _PixelGetColor_ReleaseRegion($HBITMAP)
  107. _WinAPI_DeleteObject($HBITMAP)
  108. EndFunc
  109. ; #FUNCTION# ;===============================================================================
  110. ;
  111. ; Name...........: _PixelGetColor_ReleaseDC
  112. ; Description ...: Releases a region previously created by calling _PixelGetColor_CreateDC
  113. ; Syntax.........: _PixelGetColor_ReleaseDC($iPixelGetColor_MemoryContext)
  114. ; Parameters ....: $iPixelGetColor_MemoryContext - Previously returned by _PixelGetColor_CreateDC
  115. ; Return values .: None.
  116. ; Author ........: Jos van Egmond
  117. ; Modified.......:
  118. ; Remarks .......:
  119. ; Related .......: _PixelGetColor_CreateDC, _PixelGetColor_CaptureRegion, _PixelGetcolor_GetPixel, _PixelGetColor_GetPixelRaw, _PixelGetColor_ReleaseRegion
  120. ; Example .......; No
  121. ;
  122. ; ;==========================================================================================
  123. Func _PixelGetColor_ReleaseDC($iPixelGetColor_MemoryContext, $hDll = "gdi32.dll")
  124. DllCall($hDll, "int", "DeleteDC", "hwnd", $iPixelGetColor_MemoryContext)
  125. EndFunc

comments powered by Disqus