Jump to content

Recommended Posts

Posted

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

_ImageResize(@WindowsDir & "\Web\Wallpaper\Ascent.jpg", @ScriptDir & "\Ascent.png", 400, 300)


; #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"
    
    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)
    
    ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)
    
    ;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)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)
    
    ; WinAPI 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()
    Return SetError(0, 0, 1)
EndFunc   ;==>_ImageResize
I tried a GIF file and a PNG file with transparency - same old problem. It saves transparent parts of image to black.

Otherwise, it works well.

Posted

I tried a GIF file and a PNG file with transparency - same old problem. It saves transparent parts of image to black.

Otherwise, it works well.

As said in 1st post "Simple", mainly for those people that ask "how do I resize a picture" and "What's GDI+ got to do with it".

As for people like yourself, what are you even looking at this thread for :) (joking)

Your GDI+ knowledge of image manipulation is way beyond poking at this simple example.. lol

Off track: I was actually playing with your Gradient brush functions before responding.

Nice work on the translations of them gradient functions Malkey

Cheers

Posted

Smashly

This is cool but I ran in to a problem where I couldn't create a Bitmap big enough I beleive it was related to my graphics card memory so I modified it a bit to create a DIBSection instead which works for what I was trying to do 30x20 @ 300dpi

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

_ImageResize(@WindowsDir & "\Web\Wallpaper\Ascent.jpg", @ScriptDir & "\Ascent.jpg", 30 * 300, 20 * 300)


; #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"
    
    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)
    
   ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)
    
   ;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)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)
    
    $hBitmap = _WinAPI_CreateCompatibleBitmapSection($iW,$iH)
    
   ;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()
    Return SetError(0, 0, 1)
EndFunc  ;==>_ImageResize

Func _WinAPI_CreateCompatibleBitmapSection($iWidth,$iHeight,$iBitCount=24)
    
    $tBMI = DllStructCreate($tagBITMAPINFO)
    DllStructSetData($tBMI, "Size", DllStructGetSize($tBMI) - 4)
    DllStructSetData($tBMI, "Width", $iWidth)
    DllStructSetData($tBMI, "Height", $iHeight)
    DllStructSetData($tBMI, "Planes", 1)
    DllStructSetData($tBMI, "BitCount", $iBitCount)

    $aDIB = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', _
            'ptr', 0, _
            'ptr', DllStructGetPtr($tBMI), _
            'uint', 1, _
            'ptr*', 0, _
            'ptr', 0, _
            'uint', 0)  
    Return $aDib[0]
    
EndFunc

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...