1、获取颜色不成功的原因是用_GDIPlus_BitmapLockBits锁定了,另获取颜色在UDF中本来就有,不需要自己写。
2、_GDIPlus_BitmapLockBits我也不清楚,应该是锁定图像数据到内存,这时可能不能用_GDIPlus_BitmapGetPixel颜色。
因为这时图像的数据已经返回在 $tagGDIPBITMAPDATA 结构,要读取数据要对$tagGDIPBITMAPDATA才有效。
#include <GDIPlus.au3>
;Load image
Local $imgFile = FileOpenDialog("Choose Picture", @ScriptDir, "Pictures (*.jpg)", 1)
If @error Then Exit
ConsoleWrite("Image file: " & $imgFile & @CRLF)
_GDIPlus_Startup()
Local $hBitmap = _GDIPlus_BitmapCreateFromFile($imgFile)
Local $iWidth = _GDIPlus_ImageGetWidth($hBitmap)
Local $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
ConsoleWrite("Resolution: " & $iWidth & "x" & $iHeight & @CRLF)
;~ Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iWidth, $iHeight, $GDIP_ILMREAD, $GDIP_PXF24RGB)
Local $grayValue1 = get_luma_jpg($hBitmap, 309, 17)
Local $grayValue2 = get_luma_jpg($hBitmap, 279, 5)
ConsoleWrite("$grayValue1: " & $grayValue1 & ", $grayValue2: " & $grayValue2 & @CRLF)
;~ _GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()
;~ Luma = 0.299 * Red + 0.587 * Green + 0.114 * Blue
Func get_luma_jpg($hBitmap, $iX, $iY)
Local $rgb = _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
$b = BitAND(0xFF, BitShift($rgb, 16))
$g = BitAND(0xFF, BitShift($rgb, 8))
$r = BitAND(0xFF, $rgb)
ConsoleWrite($r & @TAB & $g & @TAB & $b & @CR)
$ret = Int((30 * $r + $g * 59 + $b * 11) / 100)
Return $ret
EndFunc ;==>get_luma_jpg
|