Jump to content

_GDIPlus_Image Resize


Recommended Posts

I'm trying to add a title above an image and a subtitle below an image and save it as a file. I'm not sure of the best approach for this, so I was thinking I should create a create a blank graphic, draw the image on it in the middle, and add the text above and below the image. I can't seem to make the Image's size actually change, however. Here's what I have...

Spoiler
Func ConvertPngBmp($sPngPath, $sTitle, $sHover)

    _GDIPlus_Startup()

    ; GDI+ Variables to destroy later...
    Local $hImage_Desktop, $hFont, $hFamily, $hFormat, $hBrush, $hBrushClear, $hGraphicsBack, $hGraphics, $hImage_Real

    $hImage_Desktop = _GDIPlus_ImageLoadFromFile($sPngPath)                                 ; Load the image  (used to make desktop-sized image)
    $hImage_Real = _GDIPlus_ImageClone($hImage_Desktop)                                     ; Copy the object (used for actual image)
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage_Desktop)                          ; Get the graphics
    Local $iImgHeight = _GDIPlus_ImageGetHeight($hImage_Desktop)                            ; Get the height
    Local $iImgWidth = _GDIPlus_ImageGetWidth($hImage_Desktop)                              ; Get the width
    Local $iImgTop = (@DesktopHeight - $iImgHeight)/2                                       ; Calculate top
    Local $iImgLeft = (@DesktopWidth - $iImgWidth)/2                                        ; Calculate left
    _GDIPlus_ImageResize($hImage_Desktop, @DesktopWidth, @DesktopHeight)                    ; Resize to desktop
    Local $aiTemp = _GDIPlus_ImageGetDimension($hImage_Desktop)                         ; Check the size
    ConsoleWrite($aiTemp[0] & @CRLF)
    ConsoleWrite($aiTemp[1] & @CRLF)
    _GDIPlus_GraphicsClear($hGraphics, "0xFFFFFFFF")                                        ; Clear the image (white)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hImage_Real, $iImgTop, $iImgLeft)               ; Draw the real image onto the graphics
    
;------- String related... skip for now, mostly works, just wrong location -------------------------------------------
;~  Local $hRect = _GDIPlus_RectFCreate($iImgTop - 15, 0, @DesktopWidth, 50)                ; Create a rectange
;~  $hFormat = _GDIPlus_StringFormatCreate()                                                ; Create text format
;~  _GDIPlus_StringFormatSetAlign($hFormat, 1)                                              ; Center the text
;~  $hFamily = _GDIPlus_FontFamilyCreate("Arial")                                           ; Create the Font Family
;~  $hFont = _GDIPlus_FontCreate($hFamily, 15)                                              ; Create the Font
;~  $hBrush = _GDIPlus_BrushCreateSolid()                                                   ; Create the brush
;~  _GDIPlus_GraphicsDrawStringEx($hGraphics, $sTitle, $hFont, $hRect, $hFormat, $hBrush)   ; Draw the string
;---------------------------------------------------------------------------------------------------------------------

    Local $sNewPath = StringReplace($sPngPath, ".png", ".bmp")
    
    _GDIPlus_ImageSaveToFile($hImage_Desktop, $sNewPath)                                            ; Save
    
    ; Delete all the objects/handles/stuff
    GDIPlusUnRegister($hImage_Desktop, $hFont, $hFamily, $hFormat, $hBrush, $hBrushClear, $hGraphicsBack, $hGraphics, $hImage_Real)
    
    ; The bmp's path
    Return $sNewPath

EndFunc

 

Also, please note that this is my first time using GDI+, so there may be something completely wrong here, but I'm trying ;)

Edit: I removed all my error checks before posting, but none of the functions throw an error. The image saved looks like the old image offset diagonally and cropped to the original size.

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Works well for me :

#include <GDIPlus.au3>

Example(4)

; pixel ratio should be identical horizontal and vertical, otherwise it will be distorded
Func Example($iRatio)
  _GDIPlus_Startup()

  Local $hBitmap = _GDIPlus_BitmapCreateFromFile("Trav.png")
  Local $iW = _GDIPlus_ImageGetWidth ($hBitmap)*$iRatio, $iH = _GDIPlus_ImageGetHeight ($hBitmap)*$iRatio
  Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBitmap, $iW, $iH) ; respecting pixel ratio
  Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap_Scaled)
  _GDIPlus_GraphicsDrawString($hGraphics, "Hello", $iW/2 - 20, 10, "Arial", 14)

  _GDIPlus_ImageSaveToFile ($hBitmap_Scaled, "Trav.bmp")

  _GDIPlus_GraphicsDispose($hGraphics)
  _GDIPlus_BitmapDispose($hBitmap)
  _GDIPlus_BitmapDispose($hBitmap_Scaled)
  _GDIPlus_Shutdown()
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Oh, thank you! I failed to realize that _GDIPlus_ImageResize returned a handle to a new image and that caused it to fail.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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...