Jump to content
Sign in to follow this  

GUICtrlSetImage does not always draw picture?

Recommended Posts


Hey guys, I have an issue with a small program I wrote that scrapes a downloaded html-page for references to JPG-files, and then downloads them.

It works really good, but recently I decided to add sort of a "Preview" window for what images were found.

The images are placed in small recangles on a GUI as they are downloaded, one at a time.

Problem: If I run the script, sometimes one or several of the up to 5 downloaded images, will not show when using the GUICtrlSetImage function.

If I rerun the code on the exact same html-code, the exact same JPG files are downloaded (with new names) perhaps they will all show. Sometimes less show, sometimes more. Sometimes if picture one were missing (but downloaded as I can see it in my temp folder) the next time i run the script, it will show and maybe picture three wont...

I dont want to post the code, as its almost 1000 lines long, but I'll give you some teasers, this is the GUI:

Func BuildGUI()

#Region ### START Koda GUI section ### Form=srv-cl02-fs01hemmakatalogtomo0816documents_scriptsblocket image rippermainwindow.kxf

$frmMain = GUICreate("BIR", 1195, 468, 228, 221)
$editText = GUICtrlCreateEdit("", 0, 22, 609, 409, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlSetData(-1, "Edit1")
$inputURL = GUICtrlCreateInput("", 38, 438, 521, 21, $GUI_SS_DEFAULT_INPUT)
$lblURL = GUICtrlCreateLabel("URL: ", 6, 441, 32, 17, 0)
$lblProgress = GUICtrlCreateLabel("Progress", 0, 5, 45, 17, 0)
$btnGo = GUICtrlCreateButton("Go", 568, 437, 41, 25, $BS_NOTIFY)

Global $Pic1 = GUICtrlCreatePic("", 624, 24, 460, 428, BitOR($GUI_SS_DEFAULT_PIC, $SS_CENTERIMAGE, $WS_BORDER))
Global $Pic2 = GUICtrlCreatePic("", 1088, 24, 100, 68, BitOR($GUI_SS_DEFAULT_PIC, $WS_BORDER))
Global $Pic3 = GUICtrlCreatePic("", 1088, 96, 100, 68, BitOR($GUI_SS_DEFAULT_PIC, $WS_BORDER))
Global $Pic4 = GUICtrlCreatePic("", 1088, 168, 100, 68, BitOR($GUI_SS_DEFAULT_PIC, $WS_BORDER))
Global $Pic5 = GUICtrlCreatePic("", 1088, 240, 100, 68, BitOR($GUI_SS_DEFAULT_PIC, $WS_BORDER))
Global $Pic6 = GUICtrlCreatePic("", 1088, 312, 100, 68, BitOR($GUI_SS_DEFAULT_PIC, $WS_BORDER))
Global $Pic7 = GUICtrlCreatePic("", 1088, 384, 100, 68, BitOR($GUI_SS_DEFAULT_PIC, $WS_BORDER))


#EndRegion ### END Koda GUI section ###

Global $ImageBoxes[1]
_ArrayAdd($ImageBoxes, $Pic2)
_ArrayAdd($ImageBoxes, $Pic2)
_ArrayAdd($ImageBoxes, $Pic3)
_ArrayAdd($ImageBoxes, $Pic4)
_ArrayAdd($ImageBoxes, $Pic5)
_ArrayAdd($ImageBoxes, $Pic6)
_ArrayAdd($ImageBoxes, $Pic7)

If @error Then

Return 0
Return 1

EndFunc ;==>BuildGUI

...and then a bit later on, when I have a list of the URLs for the pictures, I download them as such and add them to the picture boxes, where sometimes no picture is shown, and somtimes it is. It seems random, and the pictures dont get manipulated in between, its just a download with _InetGet and then load them onto the boxes on the GUI:

$FileList[0] = UBound($FileList)
For $y = 1 To $FileList[0] - 1

If FileExists($DownloadPath) Then
$CurrentFilename = "BIR_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC & ".jpg"

$Downloaded = InetGet($FileList[$y], $DownloadPath & $CurrentFilename, 1, 1)
GUICtrlSetData($editText, "Downloading " & $FileList[$y] & "...", "1")

Do ;Display progress bar
GUICtrlSetData($editText, ".", "1")
Until InetGetInfo($Downloaded, 2)
GUICtrlSetData($editText, InetGetInfo($Downloaded, 0) & " bytes.", "1")
GUICtrlSetData($editText, @CRLF & "Saved as " & $CurrentFilename & @CRLF, "1")
GUICtrlSetImage($ImageBoxes[$y + 1], $DownloadPath & $CurrentFilename)


; Add filename and path to array for later upload via FTP-function

_ArrayAdd($AdvertImages, $CurrentFilename)


GUICtrlSetData($editText, "Download of " & $Move[$Count] & " as " & $CurrentFilename & " failed." & @CRLF, "1")
GUICtrlSetData($editText, "Destination directory " & $DownloadPath & " does not exist." & @CRLF & @CRLF, "1")



Now, from what I can figure out, it is in the

GUICtrlSetImage($ImageBoxes[$y + 1], $DownloadPath & $CurrentFilename)

...line where the magic happens. Sometimes it draws and fills the area for the picture in the GUI, and sometimes it doesent.

The file gets downloaded, I can see it and preview it with Windows Image Viewer, so its defo there, as I think there wouldave been an error cast else?

The same thing happens later on in the code, as the user is given the choice to keep the "main" image in the big box, or to crop it.

If its cropped, I use FreeImage.DLL to crop the JPG and make it 20 pixles smaller on all sides. After a successfull crop, the image is reloaded and the new size/cropped image is shown in the large picturebox in the GUI.

Also this works sometimes, about half of the time, the box is updated and the new, cropped picture is shown, and sometimes, the box stays gray - no image. If I then choose to crop again, it can sometimes show up, and sometimes not. It seems very intermittent. The fact that it shows at all, sometimes, must surely proove the picture exists and is found, and the error to be found in the actual "update" or "load" part of the GUI?

I hope the question didnt get too lenghty.

Cheers from Sweden


Edited by Grefven

Share this post

Link to post
Share on other sites

You can use GDI+ to load an image and display it in your GUI.

Here a small example:

;coded by UEZ 2011
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#Include <Memory.au3>

Global Const $IMAGE_BITMAP = 0
Global Const $STM_SETIMAGE = 0x0172
Global $msg
Global Const $hBmp = Load_BMP_From_Mem(InetRead("http://aut1.autoit-cdn.com/forum/public/style_images/master/logo_autoit.png"), True) ;to load an image from the net
Global Const $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp)
Global Const $iWidth = _GDIPlus_ImageGetWidth($hBitmap)
Global Const $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
Global Const $hGUI = GUICreate("Display PNG Image in picture control", $iWidth, $iHeight)
Global Const $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
_WinAPI_DeleteObject(GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp))

While True
    $msg = GUIGetMsg()
    Switch $msg
        Case $idPic
            MsgBox(0, "Information", "PNG image was clicked")
        Case $GUI_EVENT_CLOSE

; Function Name:        Load_BMP_From_Mem
; Description:              Loads an image which is saved as a binary string and converts it to a bitmap or hbitmap
; Parameters:               $bImage:    the binary string which contains any valid image which is supported by GDI+
; Optional:                     $hHBITMAP:  if false a bitmap will be created, if true a hbitmap will be created
; Remark:                       hbitmap format is used generally for GUI internal images, $bitmap is more a GDI+ image format
;                                       Don't forget _GDIPlus_Startup() and _GDIPlus_Shutdown()
; Requirement(s):       GDIPlus.au3, Memory.au3 and _WinAPI_BitmapCreateDIBFromBitmap() from WinAPIEx.au3
; Return Value(s):      Success: handle to bitmap (GDI+ bitmap format) or hbitmap (WinAPI bitmap format),
;                                       Error: 0
; Error codes:              1: $bImage is not a binary string
;                                       2: unable to create stream on HGlobal
;                                       3: unable to create bitmap from stream
; Author(s):                    UEZ
; Additional Code:      thanks to progandy for the MemGlobalAlloc and tVARIANT lines and
;                                       Yashied for _WinAPI_BitmapCreateDIBFromBitmap() from WinAPIEx.au3
; Version:                      v0.98 Build 2012-08-29 Beta
Func Load_BMP_From_Mem($bImage, $hHBITMAP = False)
    If Not IsBinary($bImage) Then Return SetError(1, 0, 0)
    Local $aResult
    Local Const $memBitmap = Binary($bImage) ;load image  saved in variable (memory) and convert it to binary
    Local Const $len = BinaryLen($memBitmap) ;get length of image
    Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory  ($GMEM_MOVEABLE = 0x0002)
    Local Const $pData = _MemGlobalLock($hData) ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $len & "]", $pData) ;create struct
    DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
    _MemGlobalUnlock($hData) ;decrements the lock count  associated with a memory object that was allocated with GMEM_MOVEABLE
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    If @error Then Return SetError(2, 0, 0)
    Local Const $hStream = $aResult[3]
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
    If @error Then Return SetError(3, 0, 0)
    Local Const $hBitmap = $aResult[2]
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
                                           "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) ;release memory from $hStream to avoid memory leak
    $tMem = 0
    $tVARIANT = 0
    If $hHBITMAP Then
        Local Const $hHBmp = _WinAPI_BitmapCreateDIBFromBitmap($hBitmap)
        Return $hHBmp
    Return $hBitmap
EndFunc   ;==>Load_BMP_From_Mem

Func _WinAPI_BitmapCreateDIBFromBitmap($hBitmap) ;create 32-bit bitmap v5 (alpha channel supported)
    Local $tBIHDR, $aRet, $tData, $pBits, $hResult = 0
    $aRet = DllCall($ghGDIPDll, 'uint', 'GdipGetImageDimension', 'ptr', $hBitmap, 'float*', 0, 'float*', 0)
    If (@error) Or ($aRet[0]) Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $aRet[2], $aRet[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, 'Scan0')
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate(  'dword bV5Size;long bV5Width;long bV5Height;word bV5Planes;word bV5BitCount;dword bV5Compression;' & _ ;http://msdn.microsoft.com/en-us/library/windows/desktop/dd183381(v=vs.85).aspx
                                                                'dword bV5SizeImage;long bV5XPelsPerMeter;long bV5YPelsPerMeter;dword bV5ClrUsed;dword bV5ClrImportant;'  & _
                                                                'dword bV5RedMask;dword bV5GreenMask;dword bV5BlueMask;dword bV5AlphaMask;dword bV5CSType;' & _
                                                                'int bV5Endpoints[3];dword bV5GammaRed;dword bV5GammaGreen;dword bV5GammaBlue;dword bV5Intent;' & _
                                                                'dword bV5ProfileData;dword bV5ProfileSize;dword bV5Reserved')
    DllStructSetData($tBIHDR, 'bV5Size', DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, 'bV5Width', $aRet[2])
    DllStructSetData($tBIHDR, 'bV5Height', $aRet[3])
    DllStructSetData($tBIHDR, 'bV5Planes', 1)
    DllStructSetData($tBIHDR, 'bV5BitCount', 32)
    DllStructSetData($tBIHDR, 'bV5Compression', 0) ; $BI_BITFIELDS = 3, $BI_RGB = 0, $BI_RLE8 = 1, $BI_RLE4 = 2, $RGBA = 0x41424752
    DllStructSetData($tBIHDR, 'bV5SizeImage', $aRet[3] * DllStructGetData($tData, 'Stride'))
    DllStructSetData($tBIHDR, 'bV5AlphaMask', 0xFF000000)
    DllStructSetData($tBIHDR, 'bV5RedMask', 0x00FF0000)
    DllStructSetData($tBIHDR, 'bV5GreenMask', 0x0000FF00)
    DllStructSetData($tBIHDR, 'bV5BlueMask', 0x000000FF)
    DllStructSetData($tBIHDR, 'bV5CSType', 2) ; LCS_WINDOWS_COLOR_SPACE = 2
    DllStructSetData($tBIHDR, 'bV5Intent', 4) ; $LCS_GM_IMA
    $hResult = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', 'hwnd', 0, 'ptr', DllStructGetPtr($tBIHDR), 'uint', 0, 'ptr*', 0, 'ptr', 0, 'dword', 0)
    If (Not @error) And ($hResult[0]) Then
        DllCall('gdi32.dll', 'dword', 'SetBitmapBits', 'ptr', $hResult[0], 'dword', $aRet[2] * $aRet[3] * 4, 'ptr', DllStructGetData($tData, 'Scan0'))
        $hResult = $hResult[0]
        $hResult = 0
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    $tData = 0
    $tBIHDR = 0
    Return $hResult
EndFunc   ;==>_WinAPI_BitmapCreateDIBFromBitmap



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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post

Link to post
Share on other sites

Thankyou for your tip, much appreciated, I will definately look into using that instead.

I also seem to have found the issue: When developing in our corporate environment, using folder redirection, the @scriptdir used to define the location of the downloaded files, sometimes gets inaccessible.

I dont know why this happens, but i made a script that loads a picture from C:Temp 10000 times and checks for a pixle color change (picture load succeeded) and made the same test using a file from my home directory on a mapped network drive, and sometimes, a path error is returned - the redirected home folder is either too slow to respond or responds incorrectly, thus the image load function fails with a "file not found" error.

Changing temp directory for downloaded files to C:Temp has my program working perfectly.

Thread can be locked, as it is not an issue with Autoit but with our corporate network and redirected folders.

Thanks for a great forum,


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