Sign in to follow this  
Followers 0
dazza

GUICtrlCreatePic stop picture resizing?

5 posts in this topic

$pic = GUICtrlCreatePic($PICS & "picture.jpg", 10, 265, 80, 80)

How do I stop the picture being resized in the 80 by 80 dimensions? I don't want it stretched. Clipped is fine.

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Are you searching for something like this?

#include <GUIConstantsEx.au3>

$iFile = FileOpenDialog("Please select an image", "", "Image (*.jpg;*.png;*.bmp;*.gif)")
$iD = GetImageDim($iFile)
If @error Then Exit

$iW = $iD[0]
$iH = $iD[1]

$base_w = 150
$base_h = 150

$hGUI_w = 320
$hGUI_h = 150
$button_w = 100
$hGUI = GUICreate("Display Image by UEZ 2010", $hGUI_w, $hGUI_h)
$button = GUICtrlCreateButton("Exit", $base_w + ($hGUI_w - $base_w - $button_w ) / 2, $hGUI_h / 2 - 12, $button_w)

If $iW < $base_w And $iH < $base_h Then
    $w = $base_w / 2 - $iW / 2
    $h = $base_h / 2 - $iH / 2
    GUICtrlCreatePic($iFile, $base_w / 2 - $iW / 2, $base_h / 2 - $iH / 2, $iW, $iH)
Else
    If $iW > $iH Then
        $f = $iW / $base_w
        $w = $iW / $f
        $h = $ih / $f
    Else
        $f = $iH / $base_h
        $w = $iW / $f
        $h = $ih / $f
    EndIf
    GUICtrlCreatePic($iFile, $base_w / 2 - $w / 2, $base_h / 2 - $h / 2, $w, $h)
EndIf

$g = GUICtrlCreateGraphic(0, 0, $base_w, $base_h)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xc0c0ff, 0xc0c0ff)
GUICtrlSetGraphic(-1, $GUI_GR_RECT, 0, 0, $base_w, $base_h)

GUISetState()

Do
    Switch GUIGetMsg()
        Case -3, $button
            GUIDelete($hGUI)
            Exit
    EndSwitch
Until False

Func GetImageDim($file) ;code by Melba23 - modified by UEZ
    Local $sFile = StringRegExp($file, "(?i).*\\(.*)", 3)
    If Not IsArray($sFile) Or @error Then Return SetError(1, 0, 0)
    Local $sPath = StringRegExp($file, "(?i)(.*)\\.+", 3)
    If Not IsArray($sFile) Or @error Then Return SetError(1, 0, 0)
    Local $sDimensions = ""
    Local $oShellApp = ObjCreate("shell.application")
    If IsObj($oShellApp) Then
        Local $oDir = $oShellApp.NameSpace($sPath[0])
        If IsObj($oDir) Then
            Local $oFile = $oDir.Parsename($sFile[0])
            If IsObj($oFile) Then
                If @OSBuild > 6000 Then
                    $sDimensions = $oDir.GetDetailsOf($oFile, 31)
                ElseIf @OSVersion = "WIN_XP" Then
                    $sDimensions = $oDir.GetDetailsOf($oFile, 26)
                EndIf
            EndIf
        EndIf
    EndIf
    If $sDimensions = "" Then Return SetError(1, 0, 0) ;"Object creation failed"
    Local $aDimensions = StringRegExp($sDimensions, "(?i)[\d]*x*[\d]", 3)
    If Not IsArray($aDimensions) Then Return SetError(1, 0, 0) ;"Cannot get image resolution!"
    Return SetError(0, 0, $aDimensions)
EndFunc

Maybe code can be shorten with styles! Further some PNGs or BMPs might be displayed not properly or not displayed at all! GDI+ can handle images better.

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!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Good example UEZ.

@dazza. If you want to show the image full size, ie not stretched or compressed, and are not worried about clipping it then here is a mod to UEZ's code. You can change the part of rthe image which is seen by changing the 0,0 coords of the pic to something else.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

$iFile = FileOpenDialog("Please select an image", "", "Image (*.jpg;*.png;*.bmp;*.gif)")
$iD = GetImageDim($iFile)
If @error Then Exit

$iW = $iD[0]
$iH = $iD[1]

$base_w = 80
$base_h = 80

$hGUI_w = 320
$hGUI_h = 150
$button_w = 100
$hGUI = GUICreate("Display Image UEZ 2010", $hGUI_w, $hGUI_h)

$button = GUICtrlCreateButton("Exit", $base_w + ($hGUI_w - $base_w - $button_w) / 2, $hGUI_h / 2 - 12, $button_w)
GUISetState()
$ch = GUICreate("", $base_w, $base_h, 0, 0, $WS_CHILD, -1, $hGUI)

GUICtrlCreatePic($iFile, 0, 0, $iW, $iH)


GUISetState()

Do
    Switch GUIGetMsg()
        Case -3, $button
            GUIDelete($hGUI)
            Exit
    EndSwitch
Until False

Func GetImageDim($file) ;code by Melba23 - modified by UEZ
    Local $sFile = StringRegExp($file, "(?i).*\\(.*)", 3)
    If Not IsArray($sFile) Or @error Then Return SetError(1, 0, 0)
    Local $sPath = StringRegExp($file, "(?i)(.*)\\.+", 3)
    If Not IsArray($sFile) Or @error Then Return SetError(1, 0, 0)
    Local $sDimensions = ""
    Local $oShellApp = ObjCreate("shell.application")
    If IsObj($oShellApp) Then
        Local $oDir = $oShellApp.NameSpace($sPath[0])
        If IsObj($oDir) Then
            Local $oFile = $oDir.Parsename($sFile[0])
            If IsObj($oFile) Then
                If @OSBuild > 6000 Then
                    $sDimensions = $oDir.GetDetailsOf($oFile, 31)
                ElseIf @OSVersion = "WIN_XP" Then
                    $sDimensions = $oDir.GetDetailsOf($oFile, 26)
                EndIf
            EndIf
        EndIf
    EndIf
    If $sDimensions = "" Then Return SetError(1, 0, 0) ;"Object creation failed"
    Local $aDimensions = StringRegExp($sDimensions, "(?i)[\d]*x*[\d]", 3)
    If Not IsArray($aDimensions) Then Return SetError(1, 0, 0) ;"Cannot get image resolution!"
    Return SetError(0, 0, $aDimensions)
EndFunc   ;==>GetImageDim

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Thank you guys. Super stuff.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

:unsure:

A question about the above thread... why should we calculate the height and width of a picture by ourself, when AutoIt provides the facility to show the picture in its actual size, by using the default parameters(widht=0 and height=0) of GUICtrlCreatePic API? Below written code helped me to show a picture in a dialog with its actual size.

$hPicWnd = GUICreate( "", 300, 300, 21, 99, BitOR( $WS_CHILD, $WS_POPUP ), $WS_EX_MDICHILD, $hGUI )
$hPic = GUICtrlCreatePic( "", 0, 0, 0, 0 )
GUICtrlSetImage( $hPic, $strFileName )
GUISetState( @SW_SHOW, $hPicWnd )

But I must thank UEZ for I didn't got such an idea of putting a GUI inside to clip the picture... Thanks a lot! :>

Edited by ManojGR

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