Sign in to follow this  
Followers 0
StAbb

Get Pixel color of an Image without opening it.

3 posts in this topic

Hey guys, is there a way I can get the color of a pixel in a specified image location without having to open that image? Thanks!

-Steve

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <Constants.au3>

$test = _GetBMPPixel("C:\ofLight\Ball-Fullsize.bmp",150,150)
MsgBox(0,"",$test)

Func _GetBMPPixel($File,$X,$Y)
    
    $imagesize = _ImageGetSize($File)
    $imgwidth = $imagesize[0]
    $imgheight = $imagesize[1]
    $nofpixels = ($imgwidth+1) * ($imgheight+1)

    Dim $hInst, $hBmp, $hMemDC, $hwnd
    
    Opt("WinWaitDelay",1)
    AutoItWinSetTitle("_GetBMPPixel_Temp_Window")
    $hwnd = WinGetHandle("_GetBMPPixel_Temp_Window")

    $hInst = DllCall("user32.dll","int","GetWindowLong","hWnd",$hwnd, "int",$GWL_HINSTANCE)
    $hInst = $hInst[0]

    $hBmp = DllCall("user32.dll","hwnd","LoadImage","hwnd",$hInst,"str",$File,"int",$IMAGE_BITMAP, _
            "int",0,"int",0,"int",$LR_LOADFROMFILE)
    $hBmp = $hBmp[0]

    $hMemDC = DllCall("gdi32.dll", "int", "CreateCompatibleDC", "int", 0)
    $hMemDC = $hMemDC[0]

    DllCall("gdi32.dll", "hwnd", "SelectObject", "int", $hMemDC, "hwnd", $hBmp)

    $ret = DLLCall("gdi32.dll","int","GetPixel","int",$hMemDC,"int",$X,"int",$Y)
    $ret = Hex( $ret[0], 6)
    $ret = StringRight($ret,2) & StringMid($ret,3,2) & StringLeft($ret,2)

    DllCall("gdi32.dll", "int", "DeleteDC", "hwnd", $hMemDC)
    DllCall("gdi32.dll", "int", "DeleteObject", "hwnd", $hBmp)

    Return $ret
EndFunc

Func _ImageGetSize($sFile);SUB Function
    Local $sHeader = _FileReadAtOffsetHEX($sFile, 1, 24); Get header bytes
    Local $asIdent = StringSplit("FFD8 424D 89504E470D0A1A 4749463839 4749463837 4949 4D4D", " ")
    Local $anSize = ""
    For $i = 1 To $asIdent[0]
        If StringInStr($sHeader, $asIdent[$i]) = 1 Then
            Select
                Case $i = 2; BMP
                    $anSize = _ImageGetSizeSimple($sHeader, 19, 23, 0)
                    ExitLoop
            EndSelect
        EndIf
    Next
    If Not IsArray($anSize) Then SetError(1)
    Return ($anSize)
EndFunc

Func _FileReadAtOffsetHEX($sFile, $nOffset, $nBytes);SUB Function
    Local $hFile = FileOpen($sFile, 0)
    Local $sTempStr = ""
    FileRead($hFile, $nOffset - 1)
    For $i = $nOffset To $nOffset + $nBytes - 1
        $sTempStr = $sTempStr & Hex(Asc(FileRead($hFile, 1)), 2)
    Next
    FileClose($hFile)
    Return ($sTempStr)
EndFunc  

Func _ImageGetSizeSimple($sHeader, $nXoff, $nYoff, $nByteOrder);SUB Function
    Local $anSize[2]
    $anSize[0] = _Dec(StringMid($sHeader, $nXoff * 2 - 1, 4), $nByteOrder)
    $anSize[1] = _Dec(StringMid($sHeader, $nYoff * 2 - 1, 4), $nByteOrder)
    Return ($anSize)
EndFunc  

Func _Dec($sHexStr, $nByteOrder);SUB Function
    If $nByteOrder Then Return (Dec($sHexStr))
    Local $sTempStr = ""
    While StringLen($sHexStr) > 0
        $sTempStr = $sTempStr & StringRight($sHexStr, 2)
        $sHexStr = StringTrimRight($sHexStr, 2)
    WEnd
    Return (Dec($sTempStr))
EndFunc

EDIT: ripped out some useless stuff i didnt see when i transferred this from my source.

Edited by ofLight

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Share this post


Link to post
Share on other sites

Thanks a lot man!

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  
Followers 0