Sign in to follow this  
Followers 0
MagnumXL

Image Resize, How?

10 posts in this topic

I honestly have searched the forums, this is probably just of me not knowing where to start.

I just need to resize images. In case I am not being clear I need to take an image that is 1000 pixels wide and 1000 pixels high I could just...

_ImageResize("Original.jpg","New.jpg",500,500)

and end up with the same picture just 1/4th the size.

Anyone have any ideas?

Share this post


Link to post
Share on other sites



Use something like this:

; Load Image
$oldImage = _GDIPlus_ImageLoadFromFile()

;Create New image
$GC = _GDIPlus_ImageGetGraphicsContext($oldImage)
$newBmp = _GDIPlus_BitmapCreateFromGraphics($newW,$newH,$GC)
$newGC = _GDIPlus_ImageGetGraphicsContext($newBmp)

;Draw
_GDIPlus_GraphicsDrawImageRect($newGC,$oldImage,0,0,$newW,$newH)
_GDIPlus_ImageSaveToFile($newBmp,$newFileName)

;Clenaup
_GDIPlus_GraphicsDispose($GC)
_GDIPlus_GraphicsDispose($newGC)
_GDIPlus_BitmapDispose($newBmp)
_GDIPlus_ImageDispose($oldImage)
1 person likes this

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Awesome, I am back on track now! Thank you.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Bit late but here's a function to resize

#include <GDIPlus.au3>

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

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    
    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    
    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)
    
    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))
    
    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    ;Start GDIPlus
    _GDIPlus_Startup()
    
    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    
    ;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, $iW)
    
    ;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($hBMP)
    _GDIPlus_Shutdown()
EndFunc

Cheers

Edited by smashly
1 person likes this

Share this post


Link to post
Share on other sites

Hi, I have see this code in different post but when i try it he make more a Crop than a resize.

I explain,

the Vertical and Horizontal new size are correct (300x400) but about 15% (maybe 20%) of original image is stripped out on bottom of newly genrerated pictures.

no very difficult to verify on you example (bliss.jpg).

So something is wrong i think because i suppose tha the main aim of your code is to conserve all pictures (even at a different size).

I you have any solution for that it will be great.

Sorry for my poor english

1 person likes this

Share this post


Link to post
Share on other sites

So far i have find the problem:

You write this

;Draw the loaded image onto the blank bitmap at the size you want

_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iW)

and the correction is

;Draw the loaded image onto the blank bitmap at the size you want

_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

It's just a typing error $iW instead of $IH

Now it work great ! thank's.

1 person likes this

Share this post


Link to post
Share on other sites

I've been recently thinking about making something like this myself. Oddly enough this already has the size that I want to use. I think I'll try to figure out how to find all jpgs in a folder and do it to them rather than one at a time.


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

I've been recently thinking about making something like this myself. Oddly enough this already has the size that I want to use. I think I'll try to figure out how to find all jpgs in a folder and do it to them rather than one at a time.

Here's my addition:

#include <GDIPlus.au3>
#include <File.au3>
#include <Array.au3>


; Declare array
Dim $Images[1]

; Gets all JPG files in the current directory (@ScriptDir).
Local $search = FileFindFirstFile("*.jpg")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No JPG files could be found.")
    Exit
EndIf

; Resize array
While 1
    If IsArray($Images) Then
        Local $Bound = UBound($Images) 
        ReDim $Images[$Bound+1]
    EndIf
    $Images[$Bound] = FileFindNextFile($search)
    If @error Then ExitLoop
WEnd

; Close the search handle
FileClose($search)

; Create directory "resized" if not there yet
If NOT FileExists(@ScriptDir & "\resized\") Then
    DirCreate(@ScriptDir & "\resized\") 
EndIf   
    
; Loop for JPGs - gets dimension of JPG and calls resize function to resize to 50% width and 50% height 
For $i = 1 to Ubound($Images)-1
    If $Images[$i] <> "" AND FileExists(@ScriptDir & "\" & $Images[$i]) Then
        Local $ImagePath = @ScriptDir & "\" & $Images[$i]   
        _GDIPlus_Startup()
        Local $hImage = _GDIPlus_ImageLoadFromFile($ImagePath)
        Local $ImageWidth = _GDIPlus_ImageGetWidth($hImage)
        Local $ImageHeight = _GDIPlus_ImageGetHeight($hImage)
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_Shutdown()
        ;MsgBox(0,"DEBUG", $ImageWidth & " x " & $ImageHeight)
        Local $NewImageWidth = ($ImageWidth / 100) * 50
        Local $NewImageHeight = ($ImageHeight / 100) * 50
        
        ;MsgBox(0,"DEBUG: " & $i,$Images[$i])
        _ImageResize(@ScriptDir & "\" & $Images[$i], @ScriptDir & "\resized\" & $Images[$i], $NewImageWidth, $NewImageHeight)
    EndIf
Next

; Resize function
Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    
    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    
    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)
    
    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))
    
    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    ;Start GDIPlus
    _GDIPlus_Startup()
    
    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    
    ;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($hBMP)
    _GDIPlus_Shutdown()
EndFunc

Explanation - see comments!

best

Ronny

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Bit late but here's a function to resize

#include <GDIPlus.au3>

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

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    
    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    
    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)
    
    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))
    
    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    
    ;Start GDIPlus
    _GDIPlus_Startup()
    
    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    
    ;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, $iW)
    
    ;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($hBMP)
    _GDIPlus_Shutdown()
EndFunc

Cheers

Hi, first... sorry for my english.

I modify a bit this code, for add 1, 2 or 3 image, resize and save on BMP, this is the code that i use:

#include <GDIPlus.au3>

$IMGFile1 = FileOpenDialog("Image 1", @ScriptDir, "JPG (*.jpg)|ALL (*.*)", 1, "image1.jpg")
If @error Then Exit
    
$IMGFile2 = FileOpenDialog("Image 2", @ScriptDir, "JPG (*.jpg)|ALL (*.*)", 1, "image2.jpg")

$IMGFile3 = FileOpenDialog("Imagen 3", @ScriptDir, "JPG (*.jpg)|ALL (*.*)", 1, "image3.jpg")


_ImageResize($IMGFile1, $IMGFile2, $IMGFile3, "outfile.bmp", 170, 170)

Func _ImageResize($sInImage1, $sInImage2, $sInImage3, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hImage2B, $hImage2C, $hImage, $tBitmapData, $hGraphic, $CLSID, $i = 0
    
    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    
    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)
    
    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, 512, 256)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    ;Start GDIPlus
    _GDIPlus_Startup()
    
    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    $hImage = _GDIPlus_BitmapCloneArea($hImage1, 0, 0, 512, 256, $GDIP_PXF24RGB)
    $tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, 512, 512, $GDIP_ILMWRITE, $GDIP_PXF24RGB)
        
    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage1)
    $hImage2B = _GDIPlus_ImageLoadFromFile($sInImage2)
    $hImage2C = _GDIPlus_ImageLoadFromFile($sInImage3)
        
    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage)
    
    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iW)
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2B, 170, 0, $iW, $iW)
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2C, 340, 0, $iW, $iW)
    
    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID("BMP")
    
    
    ;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

_GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)


    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage, $sOutImage, $CLSID)
    
    
    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ImageDispose($tBitmapData)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_ImageDispose($hImage2B)
    _GDIPlus_ImageDispose($hImage2C)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_Shutdown()
EndFunc

Well, i have a problem, i can save as BMP 32 bits, 24 bits and 16 bits, but i want to save as BMP 8 bits (256 colors) but i don't know how, can you help me?

Thanks,

Edited by wallflow

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