GDIPlus | Transparenz ermitteln

  • Hallo Community :D

    Ich lade ein Bild per GDIPlus mit _GDIPlus_ImageLoadFromFile. Nun möchte ich jeden Pixel des geladen Bildes nach einer bestimmten Information abfragen:
    -> Wieviel Rot-Anteil hat der Pixel?
    -> Wieviel Blau-Anteil hat der Pixel?
    -> Wieviel Grün-Anteil hat der Pixel?
    -> Wieviel Alpha-Anteil hat der Pixel?

    Ich möchte also auch die Transparenz auslesen :)


    Jemand eine Idee wie man das hinkriegt?

    2 Mal editiert, zuletzt von Yjuq (13. November 2012 um 13:37)

  • Hier mal eine angepasste Version des Beispiels von XovoxKingdom (ungetestet):

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <Color.au3>

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    $Bitmap = _GDIPlus_ImageLoadFromFile("Bild.jpg")
    $CLSID = _GDIPlus_EncodersGetCLSID("JPG")
    $x = 0
    $y = 0
    $Color = _GDIPlus_GetPixel($Bitmap, $x, $y)
    $tARGB = DllStructCreate("BYTE[4]")
    DllStructSetData($tARGB, 1, $Color)
    $A=DllStructGetData($tARGB, 1, 1)
    $R=DllStructGetData($tARGB, 1, 2)
    $G=DllStructGetData($tARGB, 1, 3)
    $B=DllStructGetData($tARGB, 1, 4)

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox(0,"Farbe:","Alpha: "&@TAB&@TAB&$A&@CRLF&"Rot: "&@TAB&@TAB&$R&@CRLF&"Grün: "&@TAB&@TAB&$G&@CRLF&"Blau: "&@TAB&@TAB&$B&@CRLF&"Hex-Gesamtfarbe: "&@TAB&"0x"&Hex($R,2)&Hex($G,2)&Hex($B,2))

    [/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_GetPixel($hBitmap,$X,$Y)
    ; Prog@ndy
    Local $result = DllCall($ghGDIPDLL, "int", "GdipBitmapGetPixel", "ptr", $hBitmap, "int", $X, "int", $Y, "dword*", 0)
    If @error Then Return SetError(1,0,0)
    Return SetError($result[0],1,$result[4])
    EndFunc
    Func _GDIPlus_SetPixel($hBitmap,$X,$Y, $ARGB)
    ; Prog@ndy
    Local $result = DllCall($ghGDIPDLL, "int", "GdipBitmapSetPixel", "ptr", $hBitmap, "int", $X, "int", $Y, "dword", $ARGB)
    If @error Then Return SetError(1,0,0)
    Return SetError($result[0],1,$result[0]=0)
    EndFunc

    [/autoit]


    Sobald ich nach Hause komme kann ich das ganze auch testen, es kann nämlich auch sein, dass ich hier völligen Blödsinn zusammengeschrieben habe.

  • Bei mir funktioniert es prima :huh: ...
    Hier noch mal eine saubere Version (ist aber fast der selbe Code):

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <Color.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $iX = 0
    $iY = 0

    [/autoit] [autoit][/autoit] [autoit]

    $sFile = FileOpenDialog("Bild auswählen", "", "Images (*.jpg;*.bmp;*.png)")
    If @error Then Exit

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    $hBitmap = _GDIPlus_ImageLoadFromFile($sFile)
    $iColor = _GDIPlus_GetPixel($hBitmap, $iX, $iY)
    $tARGB = DllStructCreate("BYTE[4]")
    DllStructSetData($tARGB, 1, $iColor)

    [/autoit] [autoit][/autoit] [autoit]

    $iA = DllStructGetData($tARGB, 1, 4)
    $iR = DllStructGetData($tARGB, 1, 3)
    $iG = DllStructGetData($tARGB, 1, 2)
    $iB = DllStructGetData($tARGB, 1, 1)

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox(0, "Farbe:", "Alpha: " & @TAB & $iA & @CRLF & "Rot: " & @TAB & $iR & @CRLF & "Grün: " & @TAB & $iG & @CRLF & "Blau: " & @TAB & $iB & @CRLF & @CRLF & "Gesamt: 0x" & Hex($iA, 2) & Hex($iR, 2) & Hex($iG, 2) & Hex($iB, 2))

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_Shutdown()

    [/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_GetPixel($hBitmap, $x, $y)
    ; Prog@ndy
    Local $result = DllCall($ghGDIPDLL, "int", "GdipBitmapGetPixel", "ptr", $hBitmap, "int", $x, "int", $y, "dword*", 0)
    If @error Then Return SetError(1, 0, 0)
    Return SetError($result[0], 1, $result[4])
    EndFunc ;==>_GDIPlus_GetPixel

    [/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_SetPixel($hBitmap, $x, $y, $ARGB)
    ; Prog@ndy
    Local $result = DllCall($ghGDIPDLL, "int", "GdipBitmapSetPixel", "ptr", $hBitmap, "int", $x, "int", $y, "dword", $ARGB)
    If @error Then Return SetError(1, 0, 0)
    Return SetError($result[0], 1, $result[0] = 0)
    EndFunc ;==>_GDIPlus_SetPixel

    [/autoit]


    Und als Funktion:

    Spoiler anzeigen
    [autoit]

    Func _ColorGetARGB($iColorARGB)
    ;Author: name22 (http://www.autoit.de)
    Local $aRet_ARGB[4], $tARGB_Tmp = DllStructCreate("BYTE[4]")

    [/autoit] [autoit][/autoit] [autoit]

    DllStructSetData($tARGB_Tmp, 1, $iColorARGB)
    $aRet_ARGB[0] = DllStructGetData($tARGB_Tmp, 1, 4) ;Alpha
    $aRet_ARGB[1] = DllStructGetData($tARGB_Tmp, 1, 3) ;Red
    $aRet_ARGB[2] = DllStructGetData($tARGB_Tmp, 1, 2) ;Green
    $aRet_ARGB[3] = DllStructGetData($tARGB_Tmp, 1, 1) ;Blue

    [/autoit] [autoit][/autoit] [autoit]

    Return $aRet_ARGB
    EndFunc

    [/autoit]