Sign in to follow this  
Followers 0
muhmuuh

Image resizing

21 posts in this topic

Hello!

I want to make image resizor. It would be useful to resize pictures to avatar size or to make thumbnails. I've looked through the help file (mainly the _GDIPlus) functions but did not help function that does it. Than I searched the forum and found this awesome program.

http://www.autoitscript.com/forum/index.ph...=58286&st=0

It is really really good and does much more things than I want. I looked through its code but I was too complicated to me and did not understand it. I want to make a simple program event without gui and just need to know how to resize particular image. I'm sure I've seen a way how to do it in the forum but can't find it now. Would you please help me?

Thanks


I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites



do you want to show it resized? (i guess No) [possible with Guictrlcreatepic, and GDIPlus]

do wanna save it resized? [possible with GDIPlus]

Share this post


Link to post
Share on other sites

do you want to show it resized? (i guess No) [possible with Guictrlcreatepic, and GDIPlus]

do wanna save it resized? [possible with GDIPlus]

just to save it resized

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

I've looked through those functions but did not find something that can help me. There is _GDIPlus_ImageGetHeight but no _GDIPlus_ImageSetHeight. I'm sure I've seen it here so maybe someone keeps it


I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

Just 6 funcs:

_GDIPlus_Startup()

_GDIPlus_GraphicsCreateFromHWND() [use a hidden gui]

_GDIPlus_ImageLoadFromFile()

_GDIPlus_GraphicsDrawImageRectRect()

_GDIPlus_ImageSaveToFile()

_GDIPlus_Shutdown()

Share this post


Link to post
Share on other sites

OK here is what i've got (copied from RACI by smashly)

#include <GDIPlus.au3>

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   ;==>_CreateBitmap
Func _GDIPLus_GraphicsDrawImageRect($hGraphics, $hImage, $iX, $iY, $iW, $iH)
    Local $aResult
    $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectI", "hwnd", $hGraphics, "hwnd", $hImage, _
                                                        "int", $iX, "int", $iY, "int", $iW, "int", $iH)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRect

Global $oSize[3]
$oSize[1]=64
$oSize[2]=64
$iPath = FileOpenDialog("Choose image", @DesktopDir & "\", "Images (*.jpg)", 1)
$Ext=".jpg"
$sPath="C:\"

$hBMP = _CreateBitmap($oSize[1], $oSize[2])
ConsoleWrite("a" & @CRLF)
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
ConsoleWrite("b" & @CRLF)
$hImage2 = _GDIPlus_ImageLoadFromFile($iPath)
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage1)
ConsoleWrite("c" & @CRLF)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage2, 0, 0, $oSize[1], $oSize[2])
ConsoleWrite("d" & @CRLF)
$CLSID = _GDIPlus_EncodersGetCLSID(StringUpper($Ext))
$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, $sPath & "cool" & StringLower($Ext), $CLSID, $pParams)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_GraphicsDispose ($hGraphics)
_WinAPI_DeleteObject($hBMP)

it gives me an error

C:\Program Files\AutoIt3\Include\GDIPlus.au3 (482) : ==> Subscript used with non-Array variable.:

It seems I'm missing something but can't get what

Can someone please help me?


I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

ok, here is what i've got so far

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>

_GDIPlus_Startup()
$iPath = FileOpenDialog("Choose image", @DesktopDir & "\", "Images (*.jpg)", 1)
$hImage = _GDIPlus_ImageLoadFromFile ($iPath)
$h = _GDIPlus_ImageGetHeight($hImage)
$w = _GDIPlus_ImageGetWidth($hImage)
$hGUI1 = GUICreate("img", $w, $h, 0, 0)
GUISetState()
$hGraphic1 = _GDIPlus_GraphicsCreateFromHWND ($hGUI1)
;_GDIPlus_GraphicsDrawImage ($hGraphic1, $hImage, 0, 0)
_GDIPlus_GraphicsDrawImageRectRect ($hGraphic1, $hImage, 0, 0, $w, $h, 0, 0, 100, 100)
Sleep(5000)
_GDIPlus_GraphicsDispose ($hGraphic1)
_GDIPlus_ImageDispose ($hImage)
_GDIPlus_Shutdown ()
GUIDelete($hGUI1)

This successfully resizes the image to 100x100 and displays it but how can I save it? The function _GDIPlus_GraphicsDrawImageRectRect only returns true or false so I can't use any of the save functions. Do I have to use another function rather than _GDIPlus_GraphicsDrawImageRectRect to resize the image? Thanks


I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

@muhmuuh

Maybe this can get you started.

RACI

regards

ptrex

He has already seen it.

OK here is what i've got (copied from RACI by smashly)

@muhmuuh

In the RACI script you posted there was a command like this:

_GDIPlus_ImageSaveToFileEx($hImage1, $sPath & "cool" & StringLower($Ext), $CLSID, $pParams)

So it is the _GDIPlus_ImageSaveToFileEx function

Use:

_GDIPlus_ImageSaveToFileEx($hImage, $sFileName, $sEncoder[, $pParams = 0])

$hImage Handle to an image object

$sFileName Fully qualified image file name

$sEncoder GUID string of encoder to be used

$pParams Pointer to a $tagGDIPPENCODERPARAMS structure

Hope you get it working!

Share this post


Link to post
Share on other sites

Hi,

#include <GDIPlus.au3>
#include <WinAPI.au3> ; use this include for _CreateBitmap function

Global $oSize[3]
$oSize[1] = 64
$oSize[2] = 64
$iPath = FileOpenDialog("Choose image", @DesktopDir & "\", "Images (*.jpg)", 1)
$Ext = "jpg"
$sPath = "C:\"

;Start up GDIPlus
_GDIPlus_Startup ()

;Create canvas to put the resized image onto
$hBMP = _CreateBitmap($oSize[1], $oSize[2])
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

;Load an image file that you want to resize
$hImage2 = _GDIPlus_ImageLoadFromFile($iPath)
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage1)

;Put 2nd image onto the canvas at the desired size
_GDIPLus_GraphicsDrawImageRect($hGraphics, $hImage2, 0, 0, $oSize[1], $oSize[2])

;The encoder to use for the output image
$CLSID = _GDIPlus_EncodersGetCLSID(StringUpper($Ext))

;Save the new image to a file
_GDIPlus_ImageSaveToFileEx($hImage1, $sPath & "cool." & StringLower($Ext), $CLSID)

;Clean up resources
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_GraphicsDispose($hGraphics)
_WinAPI_DeleteObject($hBMP)

;Shut down GDIPlus
_GDIPlus_Shutdown()

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   ;==>_CreateBitmap

Func _GDIPLus_GraphicsDrawImageRect($hGraphics, $hImage, $iX, $iY, $iW, $iH)
    Local $aResult
    $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectI", "hwnd", $hGraphics, "hwnd", $hImage, _
            "int", $iX, "int", $iY, "int", $iW, "int", $iH)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_GDIPLus_GraphicsDrawImageRect

Cheers

Share this post


Link to post
Share on other sites

@smashly

Func _GDIPLus_GraphicsDrawImageRect($hGraphics, $hImage, $iX, $iY, $iW, $iH)
    Local $aResult
    $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectI", "hwnd", $hGraphics, "hwnd", $hImage, _
            "int", $iX, "int", $iY, "int", $iW, "int", $iH)
    If @error Then Return SetError(@error, @extended, 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_GDIPLus_GraphicsDrawImageRect

This part does not seem to be needed, correct ?

regards

ptrex

Share this post


Link to post
Share on other sites

Hi ptrex,

If your using AutoIt beta 3.2.11.0 then no that function isn't needed.

But if your using AutoIt public release 3.2.10.0 then yes the function is needed.

The function wasn't added until the 3.2.11.0 beta.

Cheers

Share this post


Link to post
Share on other sites

Hi weaponx,

Because I didn't want another third party tool to do the job.

Even though ImageMagick is faster an more efficient at doing it , not to mention it has a lot more options as well.

Even so it was nice to know that it can be done with one third patry program.. AutoIt :)

Cheers

Share this post


Link to post
Share on other sites

#17 ·  Posted

Is there any way to create a transparent BMP? I've changed the extension from JPG to BMP and want to create a transparent BMP.

Share this post


Link to post
Share on other sites

#18 ·  Posted

bmp and jpg dont support transparency. But you can set a transparency color:

To have a transparent picture it is needed to create the GUI window with WS_EX_LAYERED extended style. The left-top pixel will be used as the transparency color. If several pictures are created the last picture is defining the transparent color.

Share this post


Link to post
Share on other sites

#19 ·  Posted

Is there any way to create a transparent BMP? I've changed the extension from JPG to BMP and want to create a transparent BMP.

Can you change a txt file into an mp3 too? You can't just change the file extension to change the contents of a file.

Share this post


Link to post
Share on other sites

#20 ·  Posted

Can you change a txt file into an mp3 too? You can't just change the file extension to change the contents of a file.

OK.... When I said that I changed the extension, I don't mean that I renamed the file. I was talking about the script. I changed:
$Ext = "jpg"
to
$Ext = "bmp"
The script now produces a BMP file, not a JPG.

And to answer your question, I have seen examples on how to use the speech engine in XP to get the computer to talk out a line of text. I've also seen code to capture audio produced. So, yes, if one was inclined to do so, you could change a text file into a MP3.

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