Jump to content
Sign in to follow this  

GetPixel Color from BITMAP

Recommended Posts


Hello everyone.

I'm truing to create simple map converter for my game,

but I did't find eny examples, or at least functions, that allow Pixel search from bitmap.

Only this:

PixelSearch ( left, top, right, bottom, color [, shade-variation [, step [, hwnd]]] )

But this isn't what I need. I don't wnat to create eny window.

Simple: load bitmap, search black pixels and write their positions in to the text file.

Maybe somehow with GDI Plus UDF ?...

Eny Ideas how to do this ?


And sorry for my English :)


[RU] Zone

Share this post

Link to post
Share on other sites


quick and dirty....

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <GDIPlusConstants.au3>
#include <StructureConstants.au3>
#include <Array.au3>

$bitmapfile = "pic2.bmp" ;some image with black pixels

$t = TimerInit()
$array = _blackpixel2array_file($bitmapfile, "test.txt") ;write pixelcolours from an image-file into an array and black pixel koordinates into a file
$m = TimerDiff($t)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $m = ' & $m & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

Func _blackpixel2array_file($bmpfile, $textfile) ;returns a 2d array with the colours BGR
    $hfile = FileOpen($textfile, 2)
    $pBitmap = _GDIPlus_BitmapCreateFromFile($bmpfile)
    If @error Then MsgBox(0, "", "Fehler BitmapCreateFromFile")
    ;_GDIPlus_BitmapLockBits gibt $tagGDIPBITMAPDATA-Struktur zurück
    $BitmapData = _GDIPlus_BitmapLockBits($pBitmap, 0, 0, _GDIPlus_ImageGetWidth($pBitmap), _GDIPlus_ImageGetHeight($pBitmap), $GDIP_ILMREAD, $GDIP_PXF24RGB)
    If @error Then MsgBox(0, "", "Error locking region " & @error)
    $stride = DllStructGetData($BitmapData, "Stride");Stride - Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
    $Width = DllStructGetData($BitmapData, "Width");Image width - Number of pixels in one scan line of the bitmap.
    $Height = DllStructGetData($BitmapData, "Height");Image height - Number of scan lines in the bitmap.
    ;$pixelFormat = DllStructGetData($BitmapData, "PixelFormat");Pixel format - Integer that specifies the pixel format of the bitmap
    $Scan0 = DllStructGetData($BitmapData, "Scan0");Scan0 - Pointer to the first (index 0) scan line of the bitmap.
    $pixeldata = DllStructCreate("byte[" & (Abs($stride) * ($Height)) & "]", $Scan0)
    $BMPData = StringTrimLeft(DllStructGetData($pixeldata, 1), 2) ;string im Struct-format, d.h. Byte+nulByte, in dem die pixeldaten im BGR-Format stehen

    Dim $array[$Height + 1][$Width + 1]

    For $y = 0 To $Height - 1 ;to 1 step -1   ;bottomup bitmap, first line in image is last line in data
        For $i = 0 To $stride * 2 Step 6 ;every 3 byte are one pixel
            $array[$y + 1][$i / 6] = StringMid($BMPData, ($y * $stride * 2) + $i + 1, 6) ;colour of the pixel into Array[x][y] with x and y are the koordinates
            If $array[$y + 1][$i / 6] = "000000" Then FileWriteLine($hfile, $i / 6+1 & ";" & $y + 1) ;if colour is black, write x and y-coordinates into file
    ;    _arraydisplay($array)
    $BMPDataStart = ""
    $pixeldata = 0
    _GDIPlus_BitmapUnlockBits($pBitmap, $BitmapData)
    Return $array
EndFunc   ;==>_blackpixel2array_file

There are very fast solutions in several dll´s. If you need a faster script, i´ll do my very best^^

/*EDIT*/ coordinates in file correct now

Edited by AndyG

Share this post

Link to post
Share on other sites

Thanx for Great Example THIS WILL REALY HELP !!! =)

Verry good Example Works PERFECT ! :);)

[RU] Zone

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.