Gdi+ Grafik speichern

  • Probiere es mal damit:

    Spoiler anzeigen
    [autoit]


    #include <Array.au3>
    #include <GUIConstantsEx.au3>
    #include <gdiplus.au3>
    #include <misc.au3>

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

    _GDIPlus_Startup()
    $hImage = _GDIPlus_ImageLoadFromFile(FileOpenDialog("Bild auswählen", @ScriptDir, "Bilder (*.jpg)"))
    If Not $hImage Then Exit _GDIPlus_Shutdown()

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

    $x = 140
    $y = 110
    $iW = 625
    $iH = 443

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

    $Form1 = GUICreate("Form1", $iW, $iH)
    GUISetState()

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

    $aFont = _ChooseFont()

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

    $Text = "Das ist Text 1" & @LF & _
    "Das ist Text 2" & @LF & _
    "Das ist Text 3"

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

    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($form1)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)
    $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hCtxt, 2)
    DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hCtxt, "int", 4)
    _GDIPlus_GraphicsDrawImage($hCtxt, $hImage, 0, 0)

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

    $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000 + $aFont[7])
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate($aFont[2])
    $hFont = _GDIPlus_FontCreate($hFamily, $aFont[3], $aFont[1])
    $tLayout = _GDIPlus_RectFCreate($x, $y, 1000, 1000)
    _GDIPlus_GraphicsDrawStringEx($hCtxt, $Text, $hFont, $tLayout, $hFormat, $hBrush)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)

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

    Global $hDLL = DllOpen("user32.dll")

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

    While 1
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\Ilse.jpg")
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_GraphicsDispose($hCtxt)
    _GDIPlus_Shutdown()
    DllClose($hDLL)
    GUIDelete()
    Exit
    EndSwitch
    If _IsPressed("25") Then
    $x -= 1
    DllStructSetData($tLayout, "X", $x)
    Update()
    ElseIf _IsPressed("27") Then
    $x += 1
    DllStructSetData($tLayout, "X", $x)
    Update()
    ElseIf _IsPressed("26") Then
    $y -= 1
    DllStructSetData($tLayout, "Y", $y)
    Update()
    ElseIf _IsPressed("28") Then
    $y += 1
    DllStructSetData($tLayout, "Y", $y)
    Update()
    EndIf
    WEnd

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

    Func Update()
    _GDIPlus_GraphicsDrawImage($hCtxt, $hImage, 0, 0)
    _GDIPlus_GraphicsDrawStringEx($hCtxt, $Text, $hFont, $tLayout, $hFormat, $hBrush)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
    Return 1
    EndFunc

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Wenn die Funktion durch das Aktualisieren der Position später so häufig aufgerufen wird, dann ist GraphicsDrawStringEx vielleicht sogar eher geeignet...
    Außerdem kann man nach erstellen der benötigten Objekte und Structs die Funktion beliebig oft mit den selben Parametern aufrufen ohne weiteren Arbeitsaufwand. Bei der Custom Variante werden alle Objekte bei jedem Aufruf neu erstellt und nie ordnungsgemäß aus dem Arbeitsspeicher entfernt. Wenn ich deine Funktion also oft genug aufrufe, dann hab ich irgendwann ein Problem...

    Wie wäre es wenn man erstmal klärt warum Ilses Script nicht funktioniert?
    Das Problem liegt bei den Parametern.

    [autoit]

    _GDIPlus_GraphicsDrawStringEx($hgraphics, $Text1, $Test[2], 0, $hFormat, $hBrush) ;hier mit StringEx

    [/autoit]


    Du hast hier ein bisschen zu enthusiastisch mit Copy/Paste gearbeitet wie mir scheint ^^. Der 3te Parameter sollte nicht $Test[2] sein, dort steht lediglich der Name der Schriftart drin, die Funktion verlangt ein Handle zu einem Schriftart Objekt. Das hast du sogar schon, du musst es nur noch einsetzen ($hFont ;)). Der 4te Parameter sollte auch nicht 0 sein... Du hast doch bereits ein Layout mit den gewünschten Koordinaten ($tLayout), also benutze es doch als Parameter.
    Wenn du die Koordinaten dieses Layouts später anpassen möchtest, dann kannst du entweder ein neues Layout mit der selben Funktion erzeugen und dadurch das alte in der Variable überschreiben, oder du kannst mit DLLStructSetData arbeiten (was ein wenig schwieriger ist).

    Edit: Oder du nimmst UEZs fertige Version :D.

  • Hallo,

    ich kämpfe immer noch mit den Farben.

    Ich habe die Funktion eingebaut.
    Mit _Arraydisplay ermittle ich ja den Farbwert.

    [autoit]


    $Fontdesign = _ChooseFont()
    _ArrayDisplay($Fontdesign)

    [/autoit]

    $Fontdesign[7] ist der Farbwert

    den habe ich mit der Funktion eingebaut

    [autoit]


    _GDIPlus_GraphicsDrawStringCustom ($hGraphics, $Text2, $x, $y, $Fontdesign[3], $Fontdesign[7])

    [/autoit]

    Die Schrift ändert sich, aber nicht die Farbe.

    Meine Baustelle:

    Spoiler anzeigen
    [autoit]


    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <gdiplus.au3>
    #include <ScreenCapture.au3>
    #include <WinAPI.au3>
    #include <misc.au3>
    #include <array.au3>

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

    $x = 90
    $y = 200

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

    $Form1 = GUICreate("Form1", 625, 443, 192, 124)
    GUISetState()

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

    $Fontdesign = _ChooseFont()
    _ArrayDisplay($Fontdesign)
    $Text1 = "Das ist Text 1"
    $Text2 = "Das ist Text 2"
    $Text3 = "Das ist Text 3"

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

    _GDIPlus_Startup()

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

    $hgraphics = _GDIPlus_GraphicsCreateFromHWND($Form1)
    $hImage = _GDIPlus_BitmapCreateFromFile(FileOpenDialog("Bild auswählen", @ScriptDir, "Bilder (*.jpg)"))
    $hgraphics2 = _GDIPlus_ImageGetGraphicsContext($hImage)

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

    _GDIPlus_GraphicsDrawImage($hgraphics, $hImage, 0, 0)

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

    _GDIPlus_GraphicsDrawStringCustom ($hGraphics, $Text2, $x, $y, $Fontdesign[3], $Fontdesign[7])
    ;_GDIPlus_GraphicsDrawStringCustom ($hGraphics, '_Text mit Farbe', 20, 100, 9, $Fontdesign[7], 0, 1, 'Arial')
    ;_GDIPlus_GraphicsDrawStringCustom ($hGraphics, '_GDIPlus_GraphicsDrawStringCustom', 20, 100, 9, 0xFF333333, 0, 1, 'Arial')
    ;_GDIPlus_GraphicsDrawString($hgraphics, $Text1, $x, $y,$Test[2],$Test[3]); hier habe ich XY Koordinaten

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

    ;~ _GDIPlus_GraphicsDrawString($hgraphics, $Text2, 80, 30, $Test[2], $Test[3])
    ;~ _GDIPlus_GraphicsDrawString($hgraphics, $Text3, 100, 70, $Test[2], $Test[3])
    $hgraphics2 = _GDIPlus_ImageGetGraphicsContext($hImage)

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

    ;ab hier wird gespeichert
    _GDIPlus_GraphicsDrawString($hgraphics, $Text1, $x, $y, $Fontdesign[2], $Fontdesign[3]); hier habe ich XY Koordinaten
    _GDIPlus_GraphicsDrawString($hgraphics, $Text2, 80, 30, $Fontdesign[2], $Fontdesign[3])
    _GDIPlus_GraphicsDrawString($hgraphics, $Text3, 100, 70, $Fontdesign[2], $Fontdesign[3])

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

    _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\Ilse.jpg")

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

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    Select
    Case _IsPressed(25) ;Pfeil links
    $x -= 1
    MsgBox(0, $x, "gedrückt")
    Case _IsPressed(26)
    $y -= 1
    Case _IsPressed(27)
    $x += 1
    Case _IsPressed(28)
    $y += 1
    EndSelect
    ;_GDIPlus_GraphicsDrawString($hgraphics, $Text1, $x, $y,$Test[2],$Test[3])
    WEnd

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

    _GDIPlus_Shutdown()

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

    ; #FUNCTION# ===================================================================
    ; Name ..........: _GDIPlus_GraphicsDrawStringCustom
    ; Description ...: Draw a customized string
    ; AutoIt Version : v3.3.6.1
    ; Syntax ........: _GDIPlus_GraphicsDrawStringCustom ($h_Graphics, $s_String, $i_X, $i_Y, $n_Size, $b_Color[, $i_Align = 0[, $i_Weight = 0[, $s_Font = 'Tahoma']]])
    ; Parameter(s): .: $h_Graphics - Handle to a Graphics object
    ; $s_String - String to be drawn
    ; $i_X - X coordinate where the string will be drawn
    ; $i_Y - Y coordinate where the string will be drawn
    ; $n_Size - Font size to use for drawing
    ; $b_Color - Alpha, Red, Green and Blue components
    ; $i_Align - The alignment can be one of the following:
    ; |0 - The text is aligned to the left
    ; |1 - The text is centered
    ; |2 - The text is aligned to the right
    ; $i_Weight - The style of the typeface. Can be a combination of the following:
    ; |0 - Normal weight or thickness of the typeface
    ; |1 - Bold typeface
    ; |2 - Italic typeface
    ; |4 - Underline
    ; |8 - Strikethrough
    ; $s_Font - Name of the Font Family
    ; Return Value ..: Success - True
    ; Failure - False
    ; Author(s) .....: $var
    ; ==============================================================================
    Func _GDIPlus_GraphicsDrawStringCustom ($h_Graphics, $s_String, $i_X, $i_Y, $n_Size, $b_Color, $i_Align = 0, $i_Weight = 0, $s_Font = 'Tahoma')
    Local $a_CreateSolidFill, $a_CreateStringFormat, $a_CreateFontFamilyFromName, $a_CreateFont, $t_Struct, $p_Layout, $a_Return
    $a_CreateSolidFill = DllCall ($ghGDIPDll, 'int', 'GdipCreateSolidFill', 'int', $b_Color, 'dword*', 0)
    $a_CreateStringFormat = DllCall ($ghGDIPDll, 'int', 'GdipCreateStringFormat', 'int', 0, 'word', 0, 'ptr*', 0)
    $a_CreateFontFamilyFromName = DllCall ($ghGDIPDll, 'int', 'GdipCreateFontFamilyFromName', 'wstr', $s_Font, 'ptr', 0, 'handle*', 0)
    $a_CreateFont = DllCall ($ghGDIPDll, 'int', 'GdipCreateFont', 'handle', $a_CreateFontFamilyFromName[3], 'float', $n_Size, 'int', $i_Weight, 'int', 3, 'ptr*', 0)
    $t_Struct = DllStructCreate ($tagGDIPRECTF)
    $p_Layout = DllStructGetPtr ($t_Struct)
    DllStructSetData ($t_Struct, 'X', $i_X)
    DllStructSetData ($t_Struct, 'Y', $i_Y)
    DllStructSetData ($t_Struct, 'Width', 0)
    DllStructSetData ($t_Struct, 'Height', 0)
    DllCall ($ghGDIPDll, 'int', 'GdipSetStringFormatAlign', 'handle', $a_CreateStringFormat[3], 'int', $i_Align)
    $a_Return = DllCall ($ghGDIPDll, 'int', 'GdipDrawString', 'handle', $h_Graphics, 'wstr', $s_String, 'int', -1, 'handle', $a_CreateFont[5], _
    'ptr', $p_Layout, 'handle', $a_CreateStringFormat[3], 'handle', $a_CreateSolidFill[2])
    Return $a_Return[0] = 0
    EndFunc ;==> _GDIPlus_GraphicsDrawStringCustom

    [/autoit]

    Mit dem Zeichnen...ob das heute noch klappt

    In diesem Script habe ich nur die Farben probiert, da hat sich nichts geändert


    Grüße
    Ilse :(

  • Hallo UEZ,

    erstmal VIELEN DANK für deine Lösung.
    Ich werde mir jetzt mal genau alles ansehen und versuchen zu verstehen.

    SUPER!

    Und natürlich DANKE an $Var und name22

    GDI+ ist toll, aber auch ziemlich heftig


    Liebe Grüße
    Ilse ;)

  • Hallo UEZ,

    eine Frage noch zu deinem Script:

    Das originalbild hat ja z.B. eine bestimmte Größe: sagen wir 600 x 400 Pixel
    Wenn aber abgespeichert wird dann stimmen diese Werte nicht mehr.

    Das Bild hat einen schwarzen Rand.

    Hast du eine Ahnung woran das liegen kann?


    ;) Grüße Ilse

  • Klar, das liegt an der Zeile 28:

    [autoit]


    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)

    [/autoit]

    Hier wird eine leere Bitmap mit der Größe $iW bzw. $iH erstellt und per

    [autoit]


    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)

    [/autoit]


    befüllt. Wenn die Größe des Bildes in $hImage ungleich der Größe der Bitmap ($hBitmap) ist, dann tritt genau dies ein.

    Du hast jetzt verschiedene Möglichkeiten dies zu lösen. Die einfachste Möglichkeit ist, die GUI und $hBitmap an die Größe des Bildes ($hImage) anzupassen.

    Probiere dies mal selbst hinzubekommen. :D

    Tipp: du musst "nur" zwei Zeilen modifizieren.

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Hallo UEZ,

    so, die 2 Zeilen haben mich zwar 1 Stunde beschäftigt.

    ABER: Ich hab's (juhu)


    Vielen vielen Dank nochmals
    für deine Hilfe

    Liebe Grüße
    Ilse ;)

  • [autoit]


    $iW = _GDIPlus_ImageGetWidth($hImage)
    $iH = _GDIPlus_ImageGetHeight($hImage)

    [/autoit]

    Gruß,
    UEZ ;)

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯