Sign in to follow this  
Followers 0
qwer85

_Resize UDF creating error

3 posts in this topic

I have extracted the function from RACI, Resize And Convert Images and can't make it to work. When I start the following script, AutoIt exits with abnormal termination errors.

#include <GDIPlus.au3>

_Resize("test.jpg", 100, 100)

Func _Resize($zsFilePath, $Width, $Height)
    Dim $oSize = StringSplit($Width & "|" & $Height, "|")
    $hBMP = _CreateBitmap($oSize[1], $oSize[2])
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    $hImage2 = _GDIPlus_ImageLoadFromFile($zsFilePath)
    $hGraphics = _GDIPlus_ImageGetGraphicsContext ($hImage1)
    _GDIPLus_GraphicsDrawImageRect($hGraphics, $hImage2, 0, 0, $oSize[1], $oSize[2])
    $CLSID = _GDIPlus_EncodersGetCLSID("JPG")
    $giJPGQuality = 100
    $tParams = _GDIPlus_ParamInit(1)
    $tData = DllStructCreate("int Quality")
    DllStructSetData($tData, "Quality", $giJPGQuality)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData))
    If IsDllStruct($tParams) Then $pParams = DllStructGetPtr($tParams)
    _GDIPlus_ImageSaveToFileEx($hImage1, $zsFilePath, $CLSID, $pParams)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphics)
    _WinAPI_DeleteObject($hBMP)
EndFunc

Func _CreateBitmap($iW, $iH)
    Local $hWnd, $hDDC, $hCDC, $hBMP
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    Return $hBMP
EndFunc

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

Hi, because your not using _GDIPlus_Startup() & _GDIPlus_Shutdown() as well as a few other things.. here's a stand alone resize function.

$sInImage can be *.bmp, *.gif, *.ico, *.jpg, *.png, *.tif. (use full path to the file to resize)

$sOutImage can be *.bmp, *.gif, *.jpg, *.png, *.tif. (Use a full path to where to save the resized image)

$iW and $iH are the width and height to resize the image to.

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

_ImageResize("C:\WINDOWS\Web\Wallpaper\bliss.bmp", @ScriptDir & "\Bliss.jpg", 400, 300)

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $sOP, $sOF, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0

    ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

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

    ;OutFile extension , to use for the encoder later on.
    $Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))

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

    ;Start GDIPlus
    _GDIPlus_Startup()

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

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

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

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

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

    ;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))

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

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)
    
    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>_ImageResize

Cheers

Edit: Cleaned up unused variables.

Edited by smashly

Share this post


Link to post
Share on other sites

Thanks a lot.

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