Jump to content

[SOLVED] Resize png image with GDIPlus


FireFox
 Share

Recommended Posts

Hi,

I want to resize a png image with GDIPlus (and WinAPI), but the problem is that when I try to resize it, it does not appear on screen :D

Here is what I have, _SetBitmapResize -> merge of _SetBitmap and _ImageResize (does not work)

#include <GDIPlus.au3>


$GUI_3 = GUICreate("", 50, 50, 0, 0, -1, 0x00080000 + 0x00000008, WinGetHandle(AutoItWinGetTitle( )))

_GDIPlus_Startup( )
$h_img = _GDIPlus_ImageLoadFromFile('win7.png')
;~ _SetBitMapResize($GUI_3, $h_img, 255, 250, 250)
_SetBitMap($GUI_3, $h_img, 255, 250, 250)

GUISetState(@SW_SHOW, $GUI_3)


While Sleep(250)

WEnd


Func _SetBitmapResize($hGUI, $hImage, $iOpacity, $n_width, $n_height)
    ; 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, $n_width, $n_height)
    _WinAPI_ReleaseDC($hWnd, $hDC)

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

    ;Load the image you want to resize.
    $hImage2 = $hImage;_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, $n_width, $n_height)


    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
;~  _WinAPI_DeleteDC($hDC)

    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

;~  $hScrDC = _WinAPI_GetDC(0)
;~  $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
;~  $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
;~  $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $hOld = _WinAPI_SelectObject($hMemDC, $hImage1)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
;~  DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth ($hImage))
;~  DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend  = DllStructCreate($tagBLENDFUNCTION)
    $pBlend  = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha" , $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
;~  _WinAPI_DeleteDC($hMemDC)
EndFunc

Func _SetBitmap($hGUI, $hImage, $iOpacity, $n_width, $n_height)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
;~  DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth ($hImage))
;~  DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend  = DllStructCreate($tagBLENDFUNCTION)
    $pBlend  = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha" , $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc

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

Win7 image : Click here

Thanks for anyhelp.

Cheers, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Hi,

I want to resize a png image with GDIPlus (and WinAPI), but the problem is that when I try to resize it, it does not appear on screen :D

Here is what I have, _SetBitmapResize -> merge of _SetBitmap and _ImageResize (does not work)

#include <GDIPlus.au3>


$GUI_3 = GUICreate("", 50, 50, 0, 0, -1, 0x00080000 + 0x00000008, WinGetHandle(AutoItWinGetTitle( )))

_GDIPlus_Startup( )
$h_img = _GDIPlus_ImageLoadFromFile('win7.png')
;~ _SetBitMapResize($GUI_3, $h_img, 255, 250, 250)
_SetBitMap($GUI_3, $h_img, 255, 250, 250)

GUISetState(@SW_SHOW, $GUI_3)


While Sleep(250)

WEnd


Func _SetBitmapResize($hGUI, $hImage, $iOpacity, $n_width, $n_height)
    ; 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, $n_width, $n_height)
    _WinAPI_ReleaseDC($hWnd, $hDC)

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

    ;Load the image you want to resize.
    $hImage2 = $hImage;_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, $n_width, $n_height)


    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
;~  _WinAPI_DeleteDC($hDC)

    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

;~  $hScrDC = _WinAPI_GetDC(0)
;~  $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
;~  $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
;~  $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $hOld = _WinAPI_SelectObject($hMemDC, $hImage1)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
;~  DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth ($hImage))
;~  DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend  = DllStructCreate($tagBLENDFUNCTION)
    $pBlend  = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha" , $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
;~  _WinAPI_DeleteDC($hMemDC)
EndFunc

Func _SetBitmap($hGUI, $hImage, $iOpacity, $n_width, $n_height)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
;~  DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth ($hImage))
;~  DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend  = DllStructCreate($tagBLENDFUNCTION)
    $pBlend  = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha" , $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc

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

Win7 image : Click here

Thanks for anyhelp.

Cheers, FireFox.

This works on xp.

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

$GUI_3 = GUICreate("", 50, 50, 0, 0, -1, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))

_GDIPlus_Startup()
$h_img = _ImageResize('win7.png', 250, 250) ; , @DesktopCommonDir & "\Bitm.png") ;
_SetBitMap($GUI_3, $h_img, 255, 250, 250)

GUISetState(@SW_SHOW, $GUI_3)


While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_GraphicsDispose($h_img)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd


Func _SetBitmap($hGUI, $hImage, $iOpacity, $n_width, $n_height)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $n_width)
    DllStructSetData($tSize, "Y", $n_height)
;~  DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth ($hImage))
;~  DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>_SetBitmap

;Parameter:-
;            $sOutImage = ""             - A re-sized $sInImage bitmap is returned. ( The called function needs to
;                                          be within _GDIPlus_Startup() & _GDIPlus_Shutdown() when $sOutImage = "" ONLY)
;            $sOutImage = "FileName,png" - A re-sized $sInImage called "FileName,png" will be written to disk.
Func _ImageResize($sInImage, $newW, $newH, $sOutImage = "")
    Local $oldImage, $GC, $newBmp, $newGC
    If $sOutImage = "" Then _GDIPlus_Startup()
    ; Load Image
    $oldImage = _GDIPlus_ImageLoadFromFile($sInImage)
    ;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)

    ;Clenaup
    _GDIPlus_GraphicsDispose($GC)
    _GDIPlus_GraphicsDispose($newGC)
    _GDIPlus_ImageDispose($oldImage)
    If $sOutImage = "" Then
        Return $newBmp ;
    Else
        _GDIPlus_ImageSaveToFile($newBmp, $sOutImage)
        _GDIPlus_BitmapDispose($newBmp)
        _GDIPlus_Shutdown()
        Return 1
    EndIf
EndFunc   ;==>_ImageResize
;
Link to comment
Share on other sites

_GDIPlus_GraphicsSetTransform() and _GDIPlusMatrixScale are probably what you want.

You can adapt the _GDIPlusMatrixScale() example to your needs.

Remember that using a larger image, then resizing it, will likely get better visual results than making a smaller image larger.

Link to comment
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
 Share

  • Recently Browsing   0 members

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