Jump to content

Load web jpg/jpeg/png into _GUIImageList


Go to solution Solved by UEZ,

Recommended Posts

Thanks to the help of a couple forum members I got a web png loaded onto a button.  I also want to do this with an image list. 

*Jump to the last code snippet (Slim Version) if you want to just see what I have so far.

Here is a basic example image list that works from locally stored bmp files. I would like to replace the bitmaps with jpegs or png's from the internet:

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $idListview, $hImage
    Local $sWow64 = ""
    If @AutoItX64 Then $sWow64 = "\Wow6432Node"
    Local $sPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE" & $sWow64 & "\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\Advanced\Images"

    GUICreate("ImageList AddBitmap", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create(16, 32)
    _GUIImageList_AddBitmap($hImage, $sPath & "\Red.bmp")
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_AddColumn($idListview, "Items", 120)

    ; Add items
    _GUICtrlListView_AddItem($idListview, "Item 1", 0)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

I tried integrating code from the working button example below that grabs a png file

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


_GDIPlus_Startup()
Global $myImage =        "http://icons.iconarchive.com/icons/hopstarter/soft-scraps/64/User-Administrator-Blue-icon.png"
Global $GDIbmpLarge =   _GDIPlus_BitmapCreateFromMemory(InetRead($myImage), 1) ;this is a GDI bitmap
Global $GDIplusLarge =  _GDIPlus_BitmapCreateFromHBITMAP($GDIbmpLarge) ;convert GDI to GDIPlus bitmap
Global $hGUI = GUICreate("Bitmap from inet", 540, 396, -1, -1)
GUISetBkColor(0xFFFFFF)
Global $iBtnL = GUICtrlCreateButton("", 65, 89, 72, 72, $BS_BITMAP) , $hBtnL = GUICtrlGetHandle($iBtnL)
_WinAPI_DeleteObject(_SendMessage($hBtnL, $BM_SETIMAGE, $IMAGE_BITMAP, $GDIbmpLarge)) ;this needs a GDI bitmap!!!

GUISetState()

Do
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _GDIPlus_BitmapDispose($GDIplusLarge)
            _WinAPI_DeleteObject($GDIbmpLarge)
            _GDIPlus_Shutdown()
            GUIDelete()
            Exit
        Case $iBtnL
            MsgBox(0, "Info", "Large Image Downloaded from web and used directly without saving to disk first!")
    EndSwitch
Until False

Here is what I tried but it doesn't work:

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIplus.au3> ; Needed for GDI functions

Example()

Func Example()
    Local $idListview, $hImage
    Local $sWow64 = ""
    If @AutoItX64 Then $sWow64 = "\Wow6432Node"
    Local $sPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE" & $sWow64 & "\AutoIt v3\AutoIt", "InstallDir") & "\Examples\GUI\Advanced\Images"

    GUICreate("ImageList AddBitmap", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create(16, 32)
    $webImage = "http://icons.iconarchive.com/icons/hopstarter/soft-scraps/64/User-Administrator-Blue-icon.png"
    ; This line works-> _GUIImageList_AddBitmap($hImage, $sPath & "\Red.bmp")
    Global $GDIbmpLarge =   _GDIPlus_BitmapCreateFromMemory(InetRead($webImage), 1) ;this is a GDI bitmap
    Global $GDIplusLarge =  _GDIPlus_BitmapCreateFromHBITMAP($GDIbmpLarge) ;convert GDI to GDIPlus bitmap
    ; This doesn't work -> _GUIImageList_AddBitmap($hImage, _GDIPlus_BitmapCreateFromHBITMAP($GDIbmpLarge))
    ; This doesn't work -> _GUIImageList_AddBitmap($hImage, _GDIPlus_BitmapCreateFromHBITMAP($GDIplusLarge))

    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_AddColumn($idListview, "Items", 120)

    ; Add items
    _GUICtrlListView_AddItem($idListview, "Item 1", 0)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example

*Slim version (No comments)

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIplus.au3> 

Example()

Func Example()
    Local $idListview, $hImage

    GUICreate("ImageList AddBitmap", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create(16, 32)
    $webImage = "http://icons.iconarchive.com/icons/hopstarter/soft-scraps/64/User-Administrator-Blue-icon.png"
    Global $GDIbmpLarge =   _GDIPlus_BitmapCreateFromMemory(InetRead($webImage), 1) ;this is a GDI bitmap
    Global $GDIplusLarge =  _GDIPlus_BitmapCreateFromHBITMAP($GDIbmpLarge) ;convert GDI to GDIPlus bitmap
    ; This doesn't work -> _GUIImageList_AddBitmap($hImage, _GDIPlus_BitmapCreateFromHBITMAP($GDIplusLarge))

    _GUICtrlListView_SetImageList($idListview, $hImage, 1)
    _GUICtrlListView_AddColumn($idListview, "Items", 120)
    _GUICtrlListView_AddItem($idListview, "Item 1", 0)

    Do
      Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc
Edited by nassausky
Link to comment
Share on other sites

You must start GDI+ before you use any function from GDI+ and don't forget to cleanup the resources!

Try this:

 

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIplus.au3> ; Needed for GDI functions

Example()

Func Example()
    _GDIPlus_Startup()
    Local $idListview, $hImage

    GUICreate("ImageList AddBitmap", 400, 300)
    $idListview = GUICtrlCreateListView("", 2, 2, 394, 268, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
    _GUICtrlListView_SetExtendedListViewStyle($idListview, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER))
    GUISetState(@SW_SHOW)

    ; Load images
    $hImage = _GUIImageList_Create(32, 32)
    $webImage = "http://icons.iconarchive.com/icons/hopstarter/soft-scraps/64/User-Administrator-Blue-icon.png"
    ; This line works-> _GUIImageList_AddBitmap($hImage, $sPath & "\Red.bmp")
    Global $GDIpBmpLarge = _GDIPlus_BitmapCreateFromMemory(InetRead($webImage)) ;GDI+ image!
    Global $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 32,32) ;GDI+ image
    Global $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!

    _GUIImageList_Add($hImage, $GDIbmp)
    _GUICtrlListView_SetImageList($idListview, $hImage, 1)

    ; Add columns
    _GUICtrlListView_AddColumn($idListview, "Items", 120)

    ; Add items
    _GUICtrlListView_AddItem($idListview, "Item 1", 0)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup resources
    _GDIPlus_BitmapDispose($GDIpBmpLarge)
    _GDIPlus_BitmapDispose($GDIpBmpResized)
    _WinAPI_DeleteObject($GDIbmp)
    _GDIPlus_Shutdown()
    GUIDelete()
EndFunc   ;==>Example
 

 

Br,

UEZ

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 comment
Share on other sites

That is great such a simple problem to initialize and I can't forget to cleanup.  I am a bit confused now about gdi and gdi+.

This is a piece of  your current code above from this topic:

[code A]

Global $GDIpBmpLarge = _GDIPlus_BitmapCreateFromMemory(InetRead($webImage)) ;GDI+ image!
    Global $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 32,32) ;GDI+ image
    Global $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized) ;GDI image!

The below code is a piece from my initial topic with creating images on buttons from ()

[code B]

Global $hHBitmapL = _GDIPlus_BitmapCreateFromMemory(InetRead($webImage), 1) ;this is a GDI bitmap
Global $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmapL) ;convert GDI to GDIPlus bitmap
Global $hBitmapM = _GDIPlus_ImageResize($hBmp,25,25) ;this is a GDIPlus bitmap!
Global $hHBitmapM = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmapM) ;this is a GDI bitmap

The first 2 lines of CODE B says _GDIPlus_BitmapCreateFromMemory creates just a GDI but CODE A says it creates a GDI+.

Another thing that confused me is that in CODE A you resized on line to immediately whatever came out of memory but on CODE B you resized it after converting from hBitmap.

I am so confused. How can I tell which comments are typos.

Thanks for your patience!

Link to comment
Share on other sites

  • Solution

Just check out the 2nd parameter from the function _GDIPlus_BitmapCreateFromMemory! -> _GDIPlus_BitmapCreateFromMemory ( $dImage [, $bHBITMAP = False] )

$bHBITMAP

[optional] If False a bitmap will be created, if True a hbitmap (GDI) will be created

 

If false then GDIPlus else GDI image!

Br,

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

×
×
  • Create New...