GDI+ Bilder umfärben mit ColorMatrix

  • Mit diesem kleinen Tool lassen sich schnell und einfach kleinere Farbanpassungen an Bildern mittels ColorMatrix vornehmen.
    Die Slider auf der linken Seite der GUI reichen von -255 bis 255 und wirken sich auf das untere der beiden Bilder aus.

    Hotkeys:
    -Strg + S = Speichern: Speichert eine Datei unter dem gewünschten Dateinamen (Nur wenn als Dateityp png angegeben wird kann mit Transparenz gespeichert werden, sonst werden alle Transparenten Bereiche mit 0xFFFFFFFF ausgefüllt.)
    -Strg + O = Öffnen: Öffnet eine Bilddatei und lädt sie in das Programm

    Für das Script wird die GDIP.au3 benötigt (Anhang).
    Ich hoffe es gefällt euch :) .

    Spoiler anzeigen
    [autoit]

    #include <WindowsConstants.au3>
    #include <GUIConstants.au3>
    #include <GDIP.au3>

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

    $hWnd = GUICreate(@ScriptName, 300, 400)
    $cSliderRed = GUICtrlCreateSlider(0, 20, 32, 355, 0x02)
    GUICtrlSetLimit(-1, 255, -255)
    GUICtrlSetData(-1, 0)
    $cSliderGreen = GUICtrlCreateSlider(32, 20, 32, 355, 0x02)
    GUICtrlSetLimit(-1, 255, -255)
    GUICtrlSetData(-1, 0)
    $cSliderBlue = GUICtrlCreateSlider(64, 20, 32, 355, 0x02)
    GUICtrlSetLimit(-1, 255, -255)
    GUICtrlSetData(-1, 0)
    GUICtrlCreateLabel("Rot Grün Blau", 12, 5)
    $cInputRed = GUICtrlCreateInput("0", 5, 380, 25, 18, 0x2000)
    $cInputGreen = GUICtrlCreateInput("0", 35, 380, 25, 18, 0x2000)
    $cInputBlue = GUICtrlCreateInput("0", 65, 380, 25, 18, 0x2000)
    GUISetState()

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

    _GDIPlus_Startup()

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

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics(200, 400, $hGraphic)
    $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hBuffer, 4)

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

    $hBrushBG = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)

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

    $hIA = _GDIPlus_ImageAttributesCreate()
    $tColorMatrix = _GDIPlus_ColorMatrixCreate()
    $pColorMatrix = DllStructGetPtr($tColorMatrix)

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

    $iOffRed = 0
    $iOffGreen = 0
    $iOffBlue = 0

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

    Global $hImage = "", $iWidth, $iHeight, $iImageWidthOld, $iImageHeightOld

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

    Do
    Until _Open()

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

    GUIRegisterMsg($WM_PAINT, "WM_PAINT")

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

    HotKeySet("^s", "_Save")
    HotKeySet("^o", "_Open")

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

    While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    _Exit()
    Case $cSliderRed
    $iOffRed = GUICtrlRead($cSliderRed) / 255
    GUICtrlSetData($cInputRed, GUICtrlRead($cSliderRed))
    _ReDraw()
    Case $cSliderGreen
    $iOffGreen = GUICtrlRead($cSliderGreen) / 255
    GUICtrlSetData($cInputGreen, GUICtrlRead($cSliderGreen))
    _ReDraw()
    Case $cSliderBlue
    $iOffBlue = GUICtrlRead($cSliderBlue) / 255
    GUICtrlSetData($cInputBlue, GUICtrlRead($cSliderBlue))
    _ReDraw()
    Case $cInputRed
    $iOffRed = GUICtrlRead($cInputRed) / 255
    GUICtrlSetData($cSliderRed, GUICtrlRead($cInputRed))
    _ReDraw()
    Case $cInputGreen
    $iOffGreen = GUICtrlRead($cInputGreen) / 255
    GUICtrlSetData($cSliderGreen, GUICtrlRead($cInputGreen))
    _ReDraw()
    Case $cInputBlue
    $iOffBlue = GUICtrlRead($cInputBlue) / 255
    GUICtrlSetData($cSliderBlue, GUICtrlRead($cInputBlue))
    _ReDraw()
    EndSwitch
    WEnd

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

    Func _ReDraw()
    _GDIPlus_ColorMatrixTranslate($tColorMatrix, $iOffRed, $iOffGreen, $iOffBlue)
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pColorMatrix)

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

    _GDIPlus_GraphicsClear($hBuffer, 0xFFA0A0A0)
    _GDIPlus_GraphicsDrawImageRect($hBuffer, $hImage, 100 - $iWidth / 2, 100 - $iHeight / 2, $iWidth, $iHeight)
    _GDIPlus_GraphicsDrawImageRectRectIA($hBuffer, $hImage, 0, 0, $iImageWidthOld, $iImageHeightOld, 100 - $iWidth / 2, 300 - $iHeight / 2, $iWidth, $iHeight, $hIA)
    WM_PAINT()

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

    _GDIPlus_ColorMatrixTranslate($tColorMatrix, $iOffRed * - 1, $iOffGreen * - 1, $iOffBlue * - 1)
    EndFunc ;==>_ReDraw

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

    Func _Open()
    $sFilePath = FileOpenDialog("Bild auswählen", @ScriptDir, "Bilder (*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.ico)")
    If @error Or $sFilePath = "" Then Return -1

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

    If $hImage <> "" Then _GDIPlus_ImageDispose($hImage)
    $hImage = _GDIPlus_ImageLoadFromFile($sFilePath)
    $iImageWidthOld = _GDIPlus_ImageGetWidth($hImage)
    $iImageHeightOld = _GDIPlus_ImageGetHeight($hImage)
    $iWidth = $iImageWidthOld
    $iHeight = $iImageHeightOld

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

    Select
    Case $iImageWidthOld > $iHeight
    If $iImageWidthOld >= 200 Then
    $iHeight = 200 * $iImageHeightOld / $iImageWidthOld
    $iWidth = 200
    EndIf
    Case Else
    If $iImageHeightOld >= 200 Then
    $iWidth = 200 * $iImageWidthOld / $iImageHeightOld
    $iHeight = 200
    EndIf
    EndSelect

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

    _ReDraw()
    Return 1
    EndFunc ;==>_Open

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

    Func _Save()
    $sFilePath = FileSaveDialog("Bild speichern unter", @ScriptDir, "Bilder (*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.ico)")
    If @error Or $sFilePath = "" Then Return -1
    $sExt = StringRight($sFilePath, StringLen($sFilePath) - StringInStr($sFilePath, "."))

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

    If $sExt = "jpg" Or $sExt = "jpeg" Or $sExt = "bmp" Or $sExt = "gif" Or $sExt = "ico" Or $sExt = "png" Then
    $hGraphicsTmp = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle()))
    $hBitmapTmp = _GDIPlus_BitmapCreateFromGraphics($iImageWidthOld, $iImageHeightOld, $hGraphicsTmp)
    $hBitmapGraphicsTmp = _GDIPlus_ImageGetGraphicsContext($hBitmapTmp)

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

    If $sExt <> "png" Then _GDIPlus_GraphicsClear($hBitmapGraphicsTmp, 0xFFFFFFFF)
    _GDIPlus_GraphicsDrawImageRectRectIA($hBitmapGraphicsTmp, $hImage, 0, 0, $iImageWidthOld, $iImageHeightOld, 0, 0, $iImageWidthOld, $iImageHeightOld, $hIA)
    _GDIPlus_ImageSaveToFile($hBitmapTmp, $sFilePath)

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

    _GDIPlus_GraphicsDispose($hGraphicsTmp)
    _GDIPlus_GraphicsDispose($hBitmapGraphicsTmp)
    _GDIPlus_BitmapDispose($hBitmapTmp)
    EndIf
    Return 1
    EndFunc ;==>_Save

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

    Func _Exit()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_BrushDispose($hBrushBG)
    _GDIPlus_ImageAttributesDispose($hIA)
    _GDIPlus_Shutdown()
    Exit
    EndFunc ;==>_Exit

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

    Func WM_PAINT()
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 100, 0, 200, 400)
    EndFunc ;==>WM_PAINT

    [/autoit]

  • Das mir das nicht aufgefallen ist... :rolleyes:
    Ok, da ich keine Lust habe alles upzudaten müsst ihr euch angewöhnen mit diesem Tool kein .ico Dateien zu öffnen oder zu speichern. :D

    Ach Schade, genau dafür wollte ich es gerade verwenden ;(