GDI+ Resize

  • Guten Abend,

    Ich habe folgendes Skript: mit dem ich die Grösse eines Bildes anpassen will:

    Skript
    [autoit]

    #include <GDIPlus.au3>
    #include <WinAPI.au3>

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

    $sInImage = FileOpenDialog("Bilder",@WindowsDir & "\", "Bilder (*.jpg)", 1 + 4 )
    $sOutImage ="C:\Users\Fabian\Pictures\Wallpaper_Cars\19_Resize.jpg"
    $iW = @DesktopWidth
    $iH = @DesktopHeight

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

    _ImageResize($sInImage, $sOutImage, $iW, $iH)
    If @error Then MsgBox(0,"",@error)

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

    ; #FUNCTION# =========================================================================================
    ; Name...........: _ImageResize
    ; Description....: Resize an image and optionally convert it to the format you want.
    ; Syntax.........: _ImageResize($sInImage, $sOutImage, $iW, $iH)
    ; Parameters ....: $sInImage - Full path to the image to resize / convert.
    ; In types: *.bmp, *.gif, *.ico, *.jpg, *.jpeg, *.png, *.tif, *.tiff
    ; $sOutImage - Full path where to save the resized / converted image.
    ; Out types: *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.tif, *.tiff
    ; $iW - Width to resize image to.
    ; $iH - Height to resize image to.
    ; Return values .: Success - Return 1 and @error 0
    ; Failure - Return 0 and @error 1~5
    ; @error 1 = In File does not exist
    ; @error 2 = In File format not supported
    ; @error 3 = Out File path does not exist
    ; @error 4 = Out file format not supported
    ; @error 5 = Resize Width or Height not an integer
    ; Author ........: smashly
    ; ====================================================================================================
    Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"

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

    If Not FileExists($sInImage) Then Return SetError(1, 0, 0)
    $sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))
    If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)

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

    ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)

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

    ;OutFile name, to use later on.
    $sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

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

    ;OutFile extension , to use for the encoder later on.
    $Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

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

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)

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

    ; WinAPI to create blank bitmap at the width and height to put your resized image on.
    $hBitmap = _WinAPI_CreateBitmap($iW, $iH, 1, 32)

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

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

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

    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

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

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

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

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

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

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

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

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
    $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

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

    ;Prefix the number to the begining of the output filename
    $sOutImage = $sOP & $i & "_" & $sOF

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

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

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

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
    Return SetError(0, 0, 1)
    EndFunc ;==>_ImageResize

    [/autoit]

    Folgenden Fehler bekomme ich:

    [autoit]

    ...\Include\GDIPlus.au3 (960) : ==> Subscript used with non-Array variable.:
    For $iI = 1 To $aEncoders[0][0]
    For $iI = 1 To $aEncoders^ ERROR
    ->19:52:54 AutoIT3.exe ended.rc:1

    [/autoit]


    Jemand eine Idee, wie man das Problem lösen kann?


    LG, Fabian

    2 Mal editiert, zuletzt von Fabian (29. Juli 2010 um 14:54)

  • Hallo.
    Also ich habe die Fehlerquelle gefunden.
    In der GDIPlus.au3 gibt es folgende Funktion, die den Error verursacht und auch in deinem Skript verwendet wird.

    Spoiler anzeigen
    [autoit]

    Func _GDIPlus_EncodersGetCLSID($sFileExt)
    Local $aEncoders = _GDIPlus_Encoders()
    For $iI = 1 To $aEncoders[0][0]
    If StringInStr($aEncoders[$iI][6], "*." & $sFileExt) > 0 Then Return $aEncoders[$iI][1]
    Next
    Return SetError(-1, -1, "")
    EndFunc ;==>_GDIPlus_EncodersGetCLSID

    [/autoit]

    Der Error ist, das die Funktion _GDIPlus_Encoders() @error setzt, aber dies nicht abgefragt wird.
    Ich hab mir selbst in die Funktion das Abfangen des Fehlers eingesetzt und es kam kein error.
    Wie man das allerdings behebt weiß ich leider nicht.
    MfG. PrideRage

    Meine Projekte:
    ClipBoard Manager (beendet)
    Gutes ClipBoard Verwaltungs Programm mit nützlichen Funktionen.

    HTML Creator (beendet)
    Nützliches Tool um schnell ein eigenes HTML Dokument zu erstellen.

  • UP :)

    Falls niemand eine Idee hat, wie man den Fehler beheben kann gibt es eine andere Möglichkeit die Auflösung eines Bildes mit Autoit zu ändern?

    Lg, Fabian

  • Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <WinAPI.au3>

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

    $sInImage = FileOpenDialog("Bilder",@WindowsDir & "\", "Bilder (*.jpg)", 1 + 4 )
    $sOutImage = @ScriptDir & "\19_Resize.jpg"
    $iW = @DesktopWidth
    $iH = @DesktopHeight
    _GDIPlus_Startup()
    _ImageResize($sInImage, $sOutImage, $iW, $iH)
    If @error Then MsgBox(0,"",@error)

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

    ; #FUNCTION# =========================================================================================
    ; Name...........: _ImageResize
    ; Description....: Resize an image and optionally convert it to the format you want.
    ; Syntax.........: _ImageResize($sInImage, $sOutImage, $iW, $iH)
    ; Parameters ....: $sInImage - Full path to the image to resize / convert.
    ; In types: *.bmp, *.gif, *.ico, *.jpg, *.jpeg, *.png, *.tif, *.tiff
    ; $sOutImage - Full path where to save the resized / converted image.
    ; Out types: *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.tif, *.tiff
    ; $iW - Width to resize image to.
    ; $iH - Height to resize image to.
    ; Return values .: Success - Return 1 and @error 0
    ; Failure - Return 0 and @error 1~5
    ; @error 1 = In File does not exist
    ; @error 2 = In File format not supported
    ; @error 3 = Out File path does not exist
    ; @error 4 = Out file format not supported
    ; @error 5 = Resize Width or Height not an integer
    ; Author ........: smashly
    ; ====================================================================================================
    Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"

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

    If Not FileExists($sInImage) Then Return SetError(1, 0, 0)
    $sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))
    If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)

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

    ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)

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

    ;OutFile name, to use later on.
    $sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

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

    ;OutFile extension , to use for the encoder later on.
    $Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

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

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)

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

    ; WinAPI to create blank bitmap at the width and height to put your resized image on.
    $hBitmap = _WinAPI_CreateBitmap($iW, $iH, 1, 32)

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

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

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

    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

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

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)

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

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

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

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

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

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
    $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

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

    ;Prefix the number to the begining of the output filename
    $sOutImage = $sOP & $i & "_" & $sOF

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

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

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

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
    Return SetError(0, 0, 1)
    EndFunc ;==>_ImageResize

    [/autoit]

    Du hast das _GDIPlus_Startup vergessen

    mfg. Jam00

  • Lol :)

    Danke ,das ist mir gar nicht aufgefallen, da ich eigentlich die Funktion separat mit

    [autoit]

    #Include <Skalieren.au3>

    [/autoit]

    einbide, und dann

    [autoit]

    _ImageResize($sInImage, $sOutImage, $iW, $iH)

    [/autoit]

    direkt aufrufe..