Jump to content

Recommended Posts

This is first time I need to load picture to my GUI from URL and I did some research about it. I found only this solution:

I have converted this to funtion:

#include-once
#cs
  original script:
  https://www.autoitscript.com/forum/topic/164419-updating-guictrlcreatepic-with-image-url/?do=findComment&comment=1199349
  coded by UEZ 2011
#ce

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

Func _GUICtrlSetPicFromUrl($idGUICtrlPic, $Url)
    ;check the control pos and size
    Local $aPos = ControlGetPos(_WinAPI_GetParent(GUICtrlGetHandle($idGUICtrlPic)), "", $idGUICtrlPic)

    _GDIPlus_Startup()
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead($Url), True)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp)
    Local Const $iWidth = _GDIPlus_ImageGetWidth($hBitmap)
    Local Const $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
    GUICtrlSendMsg($idGUICtrlPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp)
    _WinAPI_DeleteObject($hBmp)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()

    ;resize control back to it's original size if size of the image is bigger than control size
    if $iWidth > $aPos[2] or $iHeight > $aPos[3] then
        GUICtrlSetPos($idGUICtrlPic, $aPos[0]-1, $aPos[1]-1, $aPos[2], $aPos[3]) ;here I had to use "-1" to change position because otherwise it does not work
    EndIf
EndFunc

I was wondering if there is any other way to do this? My research is from 2014

Link to post
Share on other sites
6 hours ago, maniootek said:

I was wondering if there is any other way to do this?

Why? Is it not working? What kind other way are you looking for?

Of course, you can save the image first to disk and load it afterwards to your script.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to post
Share on other sites
  • 2 weeks later...

It is working but I thought maybe there is simpler way to do this.

I am wondering why I need to move picture control to make the picture appear? Or how can I change the size of the image to fit the control instead of fitting big image to the smaller control (quality of picture is not good)

image.png.552f1c90192fed33bad1e8748f470c71.png

Link to post
Share on other sites

Maybe by using resize :

#include <GUIConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Const $URL = "https://www.autoitscript.com/forum/uploads/monthly_2021_04/image.png.552f1c90192fed33bad1e8748f470c71.png"

_GDIPlus_Startup()
Local $hGUI = GUICreate("Example Resize")
Local $hImage = _GDIPlus_BitmapCreateFromMemory(InetRead($URL))
Local $iWidth = Int(_GDIPlus_ImageGetWidth($hImage) * 0.75)    ; New size
Local $iHeight = Int(_GDIPlus_ImageGetHeight($hImage) * 0.75)
Local $hImageResized = _GDIPlus_ImageResize($hImage, $iWidth, $iHeight)
_GDIPlus_ImageDispose($hImage)
Local $idPic = GUICtrlCreatePic("", 10, 10, $iWidth, $iHeight)
Local $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageResized)
_GDIPlus_ImageDispose($hImageResized)
GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap)

GUISetState()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
WEnd
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_Shutdown()

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...