Jump to content
Sign in to follow this  

Get pixel color from bmp

Recommended Posts


I would like to know is there a command in AutoIT to do Bitmap.GetPixel? I can only found PixelGetColor() which is for onscreen pixel but not bmp file. I need to read the color directly without using methods like _GDIPlus_BitmapCreateFromFile() then Draw it on screen to read the pixel.

Here is the .Net method I'm refering:


Share this post

Link to post
Share on other sites

Hi, something written quick and dirty...

#include <GDIPlus.au3>
#include <GDIPlusConstants.au3>
#include <ScreenCapture.au3>


$t = TimerInit()
$color=_GetPixelFromBMP("pic.jpg",100,100) ;Rückgabe: Farbe in BGR  Input: file, umd die Koordinaten x und y
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $color = ' & $color & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
$m = TimerDiff($t)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : time = ' & $m & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

Func _GetPixelFromBMP($sImage,$x,$y );git die farbe an $x,$y einer 24bpp-bitmap aus
    Local $pbitmap, $Bmp
    Local $tBD, $iWidth, $iHeight, $iStride
    Local $pData, $color

    $pbitmap = _GDIPlus_ImageLoadFromFile($sImage)       ;bild laden
    If @error Then Return SetError(1, 2, 0)
    $tBD = _GDIPlus_BitmapLockBits($pbitmap, 0, 0, _GDIPlus_ImageGetWidth($pBitmap), _GDIPlus_ImageGetHeight($pBitmap), $GDIP_ILMREAD, $GDIP_PXF24RGB)
    If @error Then MsgBox(0, "", "Error locking region " & @error)
    $iWidth = DllStructGetData($tBD, 1)      ;breite der bitmap
    $iHeight = DllStructGetData($tBD, 2)    ;hoehe der bitmap
    $iStride = DllStructGetData($tBD, 3)    ;Erklärung : http://www.autoitscript.com/forum/index.php?showtopic=106673&view=findpost&p=753169
    $pData = DllStructGetData($tBD, 5)      ;pointer auf die bitmapdaten
    $Bmp = DllStructCreate("byte[" & $iStride * $iHeight & "]", $pData)  ;struct gefüllt mit den Daten der Bitmap

    $position    = $istride * ($y - 1) + 3 * ($x-1)      ;Umrechnung der Position von x- und y- Koordinaten
    $substruct     = DllStructCreate("ubyte[3]", DllStructGetPtr($Bmp) + $position)  ;eine 3-byte struct an die position vom pixel schreiben
    $Color        = DllStructgetData($substruct, 1)  ;Daten aus der struct lesen

    _GDIPlus_BitmapUnlockBits($pbitmap, $tBD)
    return $color
EndFunc   ;==>_GetImagePixels

If you know the bpp of your bitmap, there is a much faster way. Open the file as binary and read the BGR-bytes(=colour) via stringmid(). This takes only a view milliseconds...

Share this post

Link to post
Share on other sites

Thanks for the quick respond. I was also thinking of using Dllstruct, but I'm not familiar with it. I will try to study on it....ThankyouPosted Image

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.