Jump to content

GUICtrlCreatePic strange behaviour


Zoldex
 Share

Recommended Posts

I'm trying to displaya picture on a gui but it doesn't work if I make some GDI+ operations just before creating the gui:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>
#include <StaticConstants.au3>

Local $GUIimg_title,$GUIwidth,$GUIheight,$GUILeft,$GUITop

Local $filename = FileOpenDialog("Select image", @ScriptDir, "Image (*.jpg;*.bmp)", 3)

_GDIPlus_Startup()

Local $imagefromfile = _GDIPlus_ImageLoadFromFile($filename) ;Create an image object based on a file
Local $GUIwidth = _GDIPlus_ImageGetWidth($imagefromfile)
Local $GUIheight = _GDIPlus_ImageGetHeight($imagefromfile)


; Display image
$hBitmap_GUI = GUICreate("", $GUIwidth, $GUIheight)
$hPic = GUICtrlCreatePic($filename, 0, 0)

GUISetState()
_GDIPlus_Shutdown()


While 1

Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE;, $hCancel_Button
Exit
;~
EndSwitch

WEnd

Instead it works if I remove GDI+ ... Can't understand why!

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>
#include <StaticConstants.au3>
Local $GUIimg_title,$GUIwidth,$GUIheight,$GUILeft,$GUITop

Local $filename = FileOpenDialog("Select image", @ScriptDir, "Image (*.jpg;*.bmp)", 3)

;~ _GDIPlus_Startup()

;~ Local $imagefromfile = _GDIPlus_ImageLoadFromFile($filename) ;Create an image object based on a file
;~ Local $GUIwidth = _GDIPlus_ImageGetWidth($imagefromfile)
;~ Local $GUIheight = _GDIPlus_ImageGetHeight($imagefromfile)
;~ _GDIPlus_Shutdown()


; Display image
;$hBitmap_GUI = GUICreate("", $GUIwidth, $GUIheight)
$hBitmap_GUI = GUICreate("", 800, 800); <---- had to use fixed size...
$hPic = GUICtrlCreatePic($filename, 1, 1)

GUISetState()


While 1

Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE;, $hCancel_Button
Exit
;~
EndSwitch

WEnd

:huh2:

Any hints?

Edited by Zoldex
Link to comment
Share on other sites

Not sure why the GDI+ stuff is causing a problem, but if all you need it for is to get the dimensions of the file, you could do it this way.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>
#include <Misc.au3>
#include <StaticConstants.au3>
#include <Array.au3>
Local $GUIimg_title, $GUIwidth, $GUIheight, $GUILeft, $GUITop

Local $filename = FileOpenDialog("Select image", @ScriptDir, "Image (*.jpg;*.bmp)", 3)

;~ _GDIPlus_Startup()

;~ Local $imagefromfile = _GDIPlus_ImageLoadFromFile($filename) ;Create an image object based on a file
;~ Local $GUIwidth = _GDIPlus_ImageGetWidth($imagefromfile)
;~ Local $GUIheight = _GDIPlus_ImageGetHeight($imagefromfile)

;~ _GDIPlus_Shutdown()

; Display image
$aDimensions = StringRegExp(_FileGetProperty($filename, "height"), "(d+)", 2)
$GUIheight = $aDimensions[0]
$aDimensions = StringRegExp(_FileGetProperty($filename, "width"), "(d+)", 2)
$GUIwidth = $aDimensions[0]
$hBitmap_GUI = GUICreate("", $GUIwidth, $GUIheight)
$hPic = GUICtrlCreatePic($filename, 0, 0, 0, 0)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE;, $hCancel_Button
            Exit

    EndSwitch

WEnd
;===============================================================================
; Function Name.....: _FileGetProperty
; Description.......: Returns a property or all properties for a file.
; Version...........: 1.0.2
; Change Date.......: 05-16-2012
; AutoIt Version....: 3.2.12.1+
; Parameter(s)......: $S_PATH - String containing the file path to return the property from.
;                     $S_PROPERTY - [optional] String containing the name of the property to return. (default = "")
; Requirements(s)...: None
; Return Value(s)...: Success: Returns a string containing the property value.
;                       If $S_PROPERTY is empty, an two-dimensional array is returned:
;                         $av_array[0][0] = Number of properties.
;                         $av_array[1][0] = 1st property name.
;                         $as_array[1][1] = 1st property value.
;                         $av_array[n][0] = nth property name.
;                         $as_array[n][1] = nth property value.
;                     Failure: Returns 0 and sets @error to:
;                       1 = The folder $S_PATH does not exist.
;                       2 = The property $S_PROPERTY does not exist or the array could not be created.
;                       3 = Unable to create the "Shell.Application" object $objShell.
; Author(s).........: - Simucal <Simucal@gmail.com>
;                     - Modified by: Sean Hart <autoit@hartmail.ca>
;                     - Modified by: teh_hahn <sPiTsHiT@gmx.de>
;                     - Modified by: BrewManNH
; URL...............: http://www.autoitscript.com/forum/topic/34732-udf-getfileproperty/page__view__findpost__p__557571
; Note(s)...........: Modified the script that teh_hahn posted at the above link to include the properties that
;                     Vista and Win 7 include that Windows XP doesn't. Also removed the ReDims for the $av_ret array and
;                     replaced it with a single ReDim after it has found all the properties, this should speed things up.
;===============================================================================
Func _FileGetProperty($S_PATH, Const $S_PROPERTY = "")
    $S_PATH = StringRegExpReplace($S_PATH, '["'']', "") ; strip the quotes, if any from the incoming string
    If Not FileExists($S_PATH) Then Return SetError(1, 0, 0)
    Local Const $objShell = ObjCreate("Shell.Application")
    If @error Then Return SetError(3, 0, 0)
    Local $iPropertyCount = 300 ; arbitrary number used, Windows 7 only returns 289 properties, Windows XP only returns 38 (future proofing)
    Local Const $S_FILE = StringTrimLeft($S_PATH, StringInStr($S_PATH, "", 0, -1))
    Local Const $S_DIR = StringTrimRight($S_PATH, StringLen($S_FILE) + 1)
    Local Const $objFolder = $objShell.NameSpace($S_DIR)
    Local Const $objFolderItem = $objFolder.Parsename($S_FILE)
    If $S_PROPERTY Then
        For $I = -1 To $iPropertyCount + 1
            If $objFolder.GetDetailsOf($objFolder.Items, $I) = $S_PROPERTY Then
                Return $objFolder.GetDetailsOf($objFolderItem, $I)
            EndIf
        Next
        Return SetError(2, 0, 0)
    EndIf
    Local $av_ret[$iPropertyCount][2] = [[1]]
    For $I = 1 To $iPropertyCount + 1
        If $objFolder.GetDetailsOf($objFolder.Items, $I) Then
            $av_ret[$I][0] = $objFolder.GetDetailsOf($objFolder.Items, $I - 1)
            $av_ret[$I][1] = $objFolder.GetDetailsOf($objFolderItem, $I - 1)
            $av_ret[0][0] += 1
        EndIf
    Next
    ReDim $av_ret[$av_ret[0][0] + 1][2]
    If Not $av_ret[1][0] Then Return SetError(2, 0, 0)
    Return $av_ret
EndFunc   ;==>_FileGetProperty

I used the RegEx because the results on my computer returns ### pixels, where the ### is the dimension size and this will strip off anything not a number.

This works on Windows 7, XP does not have this information available, but does have the Dimensions property you could work with.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

It's because you do not dispose the image and is still in use. Try this:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>
#include <StaticConstants.au3>

Local $GUIimg_title,$GUIwidth,$GUIheight,$GUILeft,$GUITop

Local $filename = FileOpenDialog("Select image", @ScriptDir, "Image (*.jpg;*.bmp)", 3)

_GDIPlus_Startup()

Local $imagefromfile = _GDIPlus_ImageLoadFromFile($filename) ;Create an image object based on a file
Local $GUIwidth = _GDIPlus_ImageGetWidth($imagefromfile)
Local $GUIheight = _GDIPlus_ImageGetHeight($imagefromfile)
_GDIPlus_ImageDispose($imagefromfile)
_GDIPlus_Shutdown()

; Display image
$hBitmap_GUI = GUICreate("", $GUIwidth, $GUIheight)
$hPic = GUICtrlCreatePic($filename, 0, 0, $GUIwidth, $GUIheight)

GUISetState()



While 1

Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE;, $hCancel_Button
Exit
;~
EndSwitch

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