Jump to content
Sign in to follow this  
NassauSky

Using an image from the web on a button

Recommended Posts

Converting images has been around for a pretty long time on the AutoIt forum and I'm sure there's already a function out there but this can take any image format and convert it to bmp that's valid for what you're wanting to do

; #FUNCTION# ====================================================================================================================
; Name...........: ConvertToBmp
; Description ...: Convert any image file to BMP format
; Syntax.........: ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
; Parameters ....: $sFileToConvert  - String path of the file to be converted. (C:\Users\User\Pictures\Pic.png)
;                  $sFileToSave     - String path of the file to save as. (C:\Users\User\Pictures\Converted Pic.bmp)
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
    ; Start GDIPlus
    _GDIplus_Startup()
    ; Load the image to convert
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToConvert)
    If (@Error) Then MsgBox("", "Load image filed", "Failed to load image " & $sFileToConvert & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Get encounter for BMP file
    Local $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")
    If (@Error) Then MsgBox("", "Encoder failed", "Failed to get encoder CLSID for BMP file type" & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Same image as BMP type
    Local const $bSaveImage = _GDIPlus_ImageSaveToFileEx($hImage, $sFileToSave, $sCLSID)
    If (@Error) Then MsgBox("", "Save image failed", "Failed to save image " & $sFileToSave & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

    Return $bSaveImage
EndFunc

You'll need #include <GDIPlus.au3> in your script. Here's the finished code. Tested using jpg and png file to BMP worked properly.

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>

Example()

Func Example()
    GUICreate("My GUI") ; will create a dialog box that when displayed is centered
    
    $load  = @ScriptDir & "\ArIII.bmp"
    ConvertToBmp(@ScriptDir & "\ArIII.jpg", @ScriptDir & "\ArIII.bmp")

    $Btn = GUICtrlCreateButton("my picture button", 10, 10, 260, 260, $BS_BITMAP)
    GUICtrlSetImage(-1, $load)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            case $Btn
                MsgBox("", "", "Button Pressed!")
        EndSwitch
    WEnd
EndFunc   ;==>Example

; #FUNCTION# ====================================================================================================================
; Name...........: ConvertToBmp
; Description ...: Convert any image file to BMP format
; Syntax.........: ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
; Parameters ....: $sFileToConvert  - String path of the file to be converted. (C:\Users\User\Pictures\Pic.png)
;                  $sFileToSave     - String path of the file to save as. (C:\Users\User\Pictures\Converted Pic.bmp)
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
    ; Start GDIPlus
    _GDIplus_Startup()
    ; Load the image to convert
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToConvert)
    If (@Error) Then MsgBox("", "Load image filed", "Failed to load image " & $sFileToConvert & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Get encounter for BMP file
    Local $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")
    If (@Error) Then MsgBox("", "Encoder failed", "Failed to get encoder CLSID for BMP file type" & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Same image as BMP type
    Local const $bSaveImage = _GDIPlus_ImageSaveToFileEx($hImage, $sFileToSave, $sCLSID)
    If (@Error) Then MsgBox("", "Save image failed", "Failed to save image " & $sFileToSave & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

    Return $bSaveImage
EndFunc

 

Share this post


Link to post
Share on other sites

ha i did this and while it kind of works,   it is not deleting the .jpg file...

Global $sGame = "testing"
downloadNreEncode()

Func downloadNreEncode()
    Local $hImage, $sCLSID
    ; Initialize GDI+ library
    _GDIPlus_Startup()

   ; set filepath and name to jpg
   $FilePath = @ScriptDir & "/Imgs/" & $sGame & ".jpg"
   If FileExists(@ScriptDir & "/Imgs/ " & $sGame & ".jpg") Then
      Sleep(10)
   Else
      $ReplaceWS = StringReplace($sGame, " ", "%20")
      InetGet("https://scontent-ord1-1.xx.fbcdn.net/hphotos-xpt1/v/t1.0-9/12418116_880965192022234_6972350528783742954_n.jpg?oh=4d2ecc65e263a37df0fed82910cfcfbf&oe=57409FEF", $FilePath)
   EndIf

    ; Load jpg image
    $loadJPG = _GDIPlus_ImageLoadFromFile($FilePath)

    ; Get MBP encoder CLSID
    $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")

    ; Save - new bmp  image
    $savedPath = @ScriptDir & "/Imgs/" & $sGame & ".bmp"
    _GDIPlus_ImageSaveToFileEx($loadJPG, $savedPath, $sCLSID)

   ; now delete jpg
   FileRecycle($FilePath)
   _WinAPI_DeleteObject($FilePath)

    ; Shut down GDI+ library
    _GDIPlus_Shutdown()
EndFunc   ;==>Example



but i come back here and answered!  thanks

Share this post


Link to post
Share on other sites

Converting images has been around for a pretty long time on the AutoIt forum and I'm sure there's already a function out there but this can take any image format and convert it to bmp that's valid for what you're wanting to do

; #FUNCTION# ====================================================================================================================
; Name...........: ConvertToBmp
; Description ...: Convert any image file to BMP format
; Syntax.........: ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
; Parameters ....: $sFileToConvert  - String path of the file to be converted. (C:\Users\User\Pictures\Pic.png)
;                  $sFileToSave     - String path of the file to save as. (C:\Users\User\Pictures\Converted Pic.bmp)
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
    ; Start GDIPlus
    _GDIplus_Startup()
    ; Load the image to convert
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToConvert)
    If (@Error) Then MsgBox("", "Load image filed", "Failed to load image " & $sFileToConvert & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Get encounter for BMP file
    Local $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")
    If (@Error) Then MsgBox("", "Encoder failed", "Failed to get encoder CLSID for BMP file type" & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Same image as BMP type
    Local const $bSaveImage = _GDIPlus_ImageSaveToFileEx($hImage, $sFileToSave, $sCLSID)
    If (@Error) Then MsgBox("", "Save image failed", "Failed to save image " & $sFileToSave & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

    Return $bSaveImage
EndFunc

You'll need #include <GDIPlus.au3> in your script. Here's the finished code. Tested using jpg and png file to BMP worked properly.

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>

Example()

Func Example()
    GUICreate("My GUI") ; will create a dialog box that when displayed is centered
    
    $load  = @ScriptDir & "\ArIII.bmp"
    ConvertToBmp(@ScriptDir & "\ArIII.jpg", @ScriptDir & "\ArIII.bmp")

    $Btn = GUICtrlCreateButton("my picture button", 10, 10, 260, 260, $BS_BITMAP)
    GUICtrlSetImage(-1, $load)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            case $Btn
                MsgBox("", "", "Button Pressed!")
        EndSwitch
    WEnd
EndFunc   ;==>Example

; #FUNCTION# ====================================================================================================================
; Name...........: ConvertToBmp
; Description ...: Convert any image file to BMP format
; Syntax.........: ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
; Parameters ....: $sFileToConvert  - String path of the file to be converted. (C:\Users\User\Pictures\Pic.png)
;                  $sFileToSave     - String path of the file to save as. (C:\Users\User\Pictures\Converted Pic.bmp)
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
    ; Start GDIPlus
    _GDIplus_Startup()
    ; Load the image to convert
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToConvert)
    If (@Error) Then MsgBox("", "Load image filed", "Failed to load image " & $sFileToConvert & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Get encounter for BMP file
    Local $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")
    If (@Error) Then MsgBox("", "Encoder failed", "Failed to get encoder CLSID for BMP file type" & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Same image as BMP type
    Local const $bSaveImage = _GDIPlus_ImageSaveToFileEx($hImage, $sFileToSave, $sCLSID)
    If (@Error) Then MsgBox("", "Save image failed", "Failed to save image " & $sFileToSave & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

    Return $bSaveImage
EndFunc

 

the one issue here is that they are showing its a .bmp  file   not a jpg  ... 

i am trying to see how to overwrite when saving bmp to bmp , is that possible?

Share this post


Link to post
Share on other sites

the one issue here is that they are showing its a .bmp  file   not a jpg  ... 
i am trying to see how to overwrite when saving bmp to bmp , is that possible?

Once you've loaded the image using _GDIPlus_ImageLoadFromFile you can delete the old image. You can also resize the image to fit your button like this. I also loaded image and deleted the image before I saved it. The image name stays the same on the computer but is resized.

; #FUNCTION# ====================================================================================================================
; Name...........: ResizeImage
; Description ...: Convert any image file to BMP format
; Syntax.........: ResizeImage(Const ByRef $sFileToResize, Const ByRef $iWidth, Const ByRef $iHeight)
; Parameters ....: $sFileToResize   - String path of the file to be resized. (C:\Users\User\Pictures\Pic.png)
;                  $iWidth          - New width of the image.
;                  $iHeight         - New height of the image.
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ResizeImage(Const ByRef $sFileToResize, Const ByRef $iWidth, Const ByRef $iHeight)
    _GDIplus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToResize)
    If (@Error) Then MsgBox("", "Load image failed", "Failed to load image " & $sFileToResize & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    Local $hImageResized = _GDIPlus_ImageResize($hImage, $iWidth, $iHeight)
    Local $bReturn = @Error
    If ($bReturn) Then MsgBox("", "Resize image failed", "Failed to resize image " & $sFileToResize & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    _GDIPlus_ImageDispose($hImage)

    If ($hImageResized) Then
        ; Delete the old file so save image works properly
        FileDelete($sFileToResize)
    EndIf

    ; Save the resized image if resizing succeeded
    _GDIPlus_ImageSaveToFile($hImageResized, $sFileToResize)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ImageDispose($hImageResized)
    _GDIPlus_Shutdown()

    Return Not $bReturn
EndFunc

Also this

_WinAPI_DeleteObject($FilePath)

Is unnecessary. $FilePath is just a string, not a handle to an object.

Edited by InunoTaishou

Share this post


Link to post
Share on other sites

Here ya go, this worked for me. I took out the ByRef in the parameters of the function since AutoIt is having issues combining strings in the function call, for some strange reason, and not letting me do it properly.

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Example()

Func Example()
    GUICreate("My GUI") ; will create a dialog box that when displayed is centered

    Local $sImagePath = @ScriptDir & "\"
    Local $sOldImage = "Downloaded.jpg"
    Local $sNewImage = "Downloaded.bmp"

    InetGet("https://scontent-ord1-1.xx.fbcdn.net/hphotos-xpt1/v/t1.0-9/12418116_880965192022234_6972350528783742954_n.jpg?oh=4d2ecc65e263a37df0fed82910cfcfbf&oe=57409FEF", $sImagePath & $sOldImage)
    
    ConvertToBmp($sImagePath & $sOldImage, $sImagePath & $sNewImage)
    ResizeImage($sImagePath & $sNewImage, 260, 260)
    
    $Btn = GUICtrlCreateButton("my picture button", 10, 10, 260, 260, $BS_BITMAP)
    GUICtrlSetImage(-1, $sImagePath & $sNewImage)

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            case $Btn
                MsgBox("", "", "Button Pressed!")
        EndSwitch
    WEnd
EndFunc   ;==>Example

; #FUNCTION# ====================================================================================================================
; Name...........: ConvertToBmp
; Description ...: Convert any image file to BMP format
; Syntax.........: ConvertToBmp(Const ByRef $sFileToConvert, Const ByRef $sFileToSave)
; Parameters ....: $sFileToConvert  - String path of the file to be converted. (C:\Users\User\Pictures\Pic.png)
;                  $sFileToSave     - String path of the file to save as. (C:\Users\User\Pictures\Converted Pic.bmp)
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ConvertToBmp(Const $sFileToConvert, Const $sFileToSave)
    ; Start GDIPlus
    _GDIplus_Startup()
    ; Load the image to convert
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToConvert)
    If (@Error) Then MsgBox("", "Load image failed", "Failed to load image " & $sFileToConvert & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Get encounter for BMP file
    Local $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")
    If (@Error) Then MsgBox("", "Encoder failed", "Failed to get encoder CLSID for BMP file type" & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    ; Same image as BMP type
    Local const $bSaveImage = _GDIPlus_ImageSaveToFileEx($hImage, $sFileToSave, $sCLSID)
    If (@Error) Then MsgBox("", "Save image failed", "Failed to save image " & $sFileToSave & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

    Return $bSaveImage
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: ResizeImage
; Description ...: Convert any image file to BMP format
; Syntax.........: ResizeImage(Const ByRef $sFileToResize, Const ByRef $iWidth, Const ByRef $iHeight)
; Parameters ....: $sFileToResize   - String path of the file to be resized. (C:\Users\User\Pictures\Pic.png)
;                  $iWidth          - New width of the image.
;                  $iHeight         - New height of the image.
; Return values .: Success - Returns True
;                 Failure - Returns False
; ===============================================================================================================================
Func ResizeImage(Const $sFileToResize, Const $iWidth, Const $iHeight)
    _GDIplus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFileToResize)
    If (@Error) Then MsgBox("", "Load image failed", "Failed to load image " & $sFileToResize & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    Local $hImageResized = _GDIPlus_ImageResize($hImage, $iWidth, $iHeight)
    Local $bReturn = @Error
    If ($bReturn) Then MsgBox("", "Resize image failed", "Failed to resize image " & $sFileToResize & @CRLF & "Error code = " & @Error & @CRLF & "Extended error = " & @Extended)
    _GDIPlus_ImageDispose($hImage)

    If ($hImageResized) Then
        ; Delete the old file so save image works properly
        FileDelete($sFileToResize)
    EndIf

    ; Save the resized image if resizing succeeded
    _GDIPlus_ImageSaveToFile($hImageResized, $sFileToResize)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ImageDispose($hImageResized)
    _GDIPlus_Shutdown()

    Return Not $bReturn
EndFunc

Also wanted to let you know there's a function called _GDIPlus_ImageGetWidth and _GDIPlus_ImageGetHeight that will get the width and height of your image (once loaded from file) so you could resize the button to fit the image or use the function to resize the image to fit the button.

Edited by InunoTaishou

Share this post


Link to post
Share on other sites

this is great But it is still not deleting the .jpg file

can this be done like uez does and just save it to memory instead of downloading it?  that would speed things up right?

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...