Jump to content
Sign in to follow this  
Will66

Image Viewer

Recommended Posts

Will66

Simple image viewer.

Selecting Browse for a folder creates clickable thumbs to view the larger original.

-rename images

-view images and hear them, good learning aid for kids.

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

#include <IE.au3>
#include <GDIPlus.au3>
#Include <File.au3>
#Include <Array.au3>
_IEErrorHandlerRegister ()

Global $voice,$ArgumentPairs

Dim $guiTitle="Lets Talk"
Dim $sHTML,$GetTheParent=@MyDocumentsDir
Dim $sFile_Name, $szDrive, $szDir, $szFName, $szExt
Dim $imageArray[1]
Dim $thumbfolder="LetsTalkThumbs"
Dim $myevent,$oEvent

$hGUI = GUICreate($guiTitle, 800, 600,-1,-1,$WS_OVERLAPPEDWINDOW)
GUISetOnEvent($GUI_EVENT_CLOSE,"_CLOSEClicked")
;GUISetFont (8,-1,"Arial")
Opt("GUIOnEventMode",1)
Opt("GUIResizeMode", 1)

_build_menus()

Dim $oIE = _IECreateEmbedded ()
Dim $GUIActiveX = GUICtrlCreateObj($oIE, 1, 0,798,500)
Dim $imgEdit=GUICtrlCreateEdit("",200,510,500,50, $ES_AUTOVSCROLL + $WS_VSCROLL)
Dim $imgEditSave=GUICtrlCreateButton("Change",710,510,50,24)
Dim $sayItButton=GUICtrlCreateButton("Say It!",10,510,180,26)
Dim $hearItButton=GUICtrlCreateButton("Hear It!",10,540,180,26)

GUICtrlSetOnEvent($imgEditSave, "_editorUpdated")
GUICtrlSetOnEvent($sayItButton, "_sayItButtonPressed")
GUICtrlSetOnEvent($hearItButton, "_hearItButtonPressed")


_IENavigate ($oIE, "about:blank")
_buildHTML()
 $myevent = _IEGetObjById ($oIE, "sinkField")
 $oEvent = ObjEvent( $myevent, "my_Event_")

GUISetState ()

While 1
    Sleep(100)
WEnd

Func _sayItButtonPressed()
;MsgBox(0,"",GUICtrlRead($imgEdit))
    $voice = ObjCreate("SAPI.SpVoice")
    $voice.Speak (GUICtrlRead($imgEdit))
    $voice = ""
EndFunc

Func _hearItButtonPressed()
    $hearFile=GUICtrlRead($imgEdit)
;MsgBox(0,"",$hearFile)
    If FileExists($hearFile & ".wav") Then
        SoundPlay($hearFile & ".wav")
    EndIf
EndFunc

Func _editorUpdated()
    If IsArray($ArgumentPairs) Then
        $thePath = _PathSplit($ArgumentPairs[2], $szDrive, $szDir, $szFName, $szExt)
        $x=_IEGetObjById ($oIE, $ArgumentPairs[2]);get current id(filename reference)
        $FileNameInput = $thePath[1] & $thePath[2]  & GUICtrlRead($imgEdit) & $thePath[4]
        If _IsFileName(GUICtrlRead($imgEdit)) Then
            If FileExists($FileNameInput) Then
                MsgBox(0,"Cannot Rename:","An image named " & GUICtrlRead($imgEdit) & $thePath[4] & " already exists!")
            ElseIf FileMove($ArgumentPairs[2], $FileNameInput) = 0 Then;failed to rename file
                MsgBox(16,"Cannot Rename:","Failed to rename File: " & GUICtrlRead($imgEdit) & $thePath[4] & " !" & @CRLF & "File name may be too long!")
            Else
            $x.setAttribute("alt",GUICtrlRead($imgEdit));update alt to edit value
            $x.setAttribute("id",$FileNameInput);update id to new path
            EndIf
        Else
            MsgBox(16, 'Error', 'The file name includes an invalid character/s' & @CR & '< > | ? : * / \ "', $hGui)
        EndIf
    EndIf
EndFunc

Func _IsFileName($Test)
    If StringRegExp($Test, '[<>|?:"*/\\]') <> 0 Or StringLen($test) < 1 Then
       ;MsgBox(16, 'Error', 'The file name include an invalid characters' & @CR & '< > | ? : * / \ "', $hGUI)
        Return False
    Else
        Return True
    EndIf
EndFunc

Func my_Event_onpropertychange()
    $ArgumentPairs = StringSplit($myevent.value, "|")

    GUICtrlSetData($imgEdit, $ArgumentPairs[1])
;MsgBox(0,"",$myevent.value)
EndFunc

Func _browsePressed()
        $sFile_Name = FileOpenDialog("Browse images", $GetTheParent & "", "Images (*.jpeg;*.jpg;*.bmp)", 1 + 4 )
        $thePath = _PathSplit($sFile_Name, $szDrive, $szDir, $szFName, $szExt)
        If $thePath[2] Then
            $GetTheParent=$thePath[1] & $thePath[2]
            $FileList=_FileListToArray($GetTheParent)
        ;_ArrayDisplay($FileList,"$FileList")

            If @Error=1 Then
                MsgBox (0,"","No Files\Folders Found.")
                Exit
            EndIf
            Local $imageArray[1]
            $c=0
            For $i =1 to $FileList[0]
                If $c>100 Then ExitLoop

                $filePath = _PathSplit($FileList[$i], $szDrive, $szDir, $szFName, $szExt)
                $ext=$filePath[4]
                if $ext = ".jpg" Or $ext = ".gif" Or $ext = ".jpeg" then
                    _ArrayAdd($imageArray,$filePath[3] & $filePath[4])
                    $c=$c+1
                    $imageArray[0]=$c
                EndIf

            Next
            DirCreate($GetTheParent & $thumbfolder)
            _GetThumb($GetTheParent, $imageArray,0,0);-->create thumbnails
        ;_ArrayDisplay($imageArray,"Demo _PathSplit()")

            _loadThumbsdiv($GetTheParent, $imageArray,0,0)
            DirRemove($GetTheParent & $thumbfolder,1)

        EndIf

EndFunc

    Func _loadThumbsdiv($Fparent="",$sFiles="",$iW="", $iH="")
        $sHTML = ""
         $oDiv = _IEGetObjById  ($oIE, "thumbsDiv")
        For $i=1 to $sFiles[0]
            $fileSplit = _PathSplit($sFiles[$i], $szDrive, $szDir, $szFName, $szExt)
            Out("<img alt='"&$fileSplit[3]&"' id='" & $Fparent & $sFiles[$i] & "' src='" & $Fparent & $thumbfolder & "\" & $sFiles[$i] & "' onclick='loadImage(this);'>")
        next
        _IEPropertySet ($oDiv, "innerhtml", $sHTML)
    EndFunc

Func _GetThumb($Fparent="",$sFiles="",$iW="", $iH="")
    Local $hImage, $aResult, $hThumb,$hGraphic
    _GDIPlus_Startup()

    For $i =1 to $sFiles[0]
        $hImage = _GDIPlus_ImageLoadFromFile($Fparent & $sFiles[$i])
        $iX = _GDIPlus_ImageGetWidth ($hImage)
        $iY = _GDIPlus_ImageGetHeight ($hImage)
    ;MsgBox(0,"",$Fparent & $thumbfolder & "\" & $sFiles[$i])

        $aResult = DllCall($ghGDIPDll, "int", "GdipGetImageThumbnail", "hwnd", $hImage, "int", $iW, "int", $iH, _
            "int*", 0, "ptr", 0, "ptr", 0)
        $hThumb = _GDIPlus_BitmapCreateHBITMAPFromBitmap($aResult[4])
        _GDIPlus_ImageSaveToFile($aResult[4], $Fparent & $thumbfolder & "\" & $sFiles[$i])

    _GDIPlus_ImageDispose($aResult[4])
    _GDIPlus_ImageDispose($hImage)
    next
    _GDIPlus_Shutdown()
  ; Return $hThumb
EndFunc  ;==>_GetThumb


    Func _buildHTML()
        $sHTML = ""
        Out('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">')
        Out("<HEAD>")
        Out("<TITLE>Amba-view</TITLE>")
        Out("<style>BODY{border:0;font size:8pt;font-family:arial;} ")
        Out(".buttons{background:#fff000;} #myImg{max-width:400px;max-height:500px;}</style>")
        Out("<script type='text/javascript'>")
        Out("function loadImage (imgId){")
    ;Out("alert(imgId.id);")
        Out("var myImgDiv=document.getElementById('myImgDiv');")
        Out("var myImg=document.getElementById('myImg');")
        Out("var mySink=document.getElementById('sinkField');")
        Out("mySink.setAttribute('value',imgId.alt + '|' + imgId.id);")

        Out("divW=myImgDiv.offsetWidth;")
        Out("divH=myImgDiv.offsetHeight;")
        Out("myImg.setAttribute('src',imgId.id);")
        Out("myImg.setAttribute('alt',imgId.alt);")

        Out("myImg.style.maxWidth=divW-2+'px';")
        Out("myImg.style.maxHeight=divH-2+'px';")
        Out("myImg.style.display='';")

        Out("}")

        Out("function renameImage (imgId){")
        Out("}")

        Out("</script>")


        Out("</HEAD>")
        Out("<BODY scroll='no'>")
        Out("<div id='thumbsDiv' style='position:absolute;top:0px;left:0px;width:180px;height:100%;border-right:1px solid;overflow:auto;'></div>")
        Out("<div id='myImgDiv' style='position:absolute;top:0px;left:181px;width:100%;height:100%'>")
        Out("<form id='sinkForm'><input id='sinkField' type='hidden' value=''></form>")

        Out("<img id='myImg' style='display:none;' src=''>")
        Out("</div>")

        Out("</BODY>")
        Out("</HTML>")

        _IEDocWriteHTML ($oIE, $sHtml)
        _IEAction ($oIE, "refresh")

    EndFunc

    Func _build_menus()

        $ViewMenu = GUICtrlCreateMenu ("&Browse")
        GUICtrlCreateMenuitem ("Browse Images",$ViewMenu)
        GUICtrlSetOnEvent(-1, "_browsePressed")

        $browse=GUICtrlCreateMenu ("&Settings")
        GUICtrlCreateMenuitem ("Browse",$browse)
            GUICtrlSetState(-1, $GUI_CHECKED)

       ;GUICtrlSetOnEvent(-1, "TablesMenu_clicked")
        $x=GUICtrlCreateMenuitem ("Browse2",$browse)
            GUICtrlSetState($x, $GUI_CHECKED)


    EndFunc



    Func Out($temp)
        $sHtml &= $temp & @CRLF
    EndFunc

    Func _CLOSEClicked()
            GUIDelete($hGUI)
            Exit
    EndFunc

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.