gcue

preserve thumbnails from _GUIImagelist

5 posts in this topic

#1 ·  Posted (edited)

hello

i have a listview that toggles between different image types.  each listview item has a thumbnail image associated to it.  at first load, the thumbnails load fine.  when i toggle between image types, i destroy the listview items and reload listview items for the types selected but i don't know how to reference the previously created thumbnails for each corresponding item.  A bit difficult to explain but if you run the script you will see what I mean. (make sure to choose a directory that has images)

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <File.au3>
#include <GuiImageList.au3>
#include <GDIPlus.au3>

Opt("GUIOnEventMode", 1)

$images_dir = @DesktopDir ;choose an image directory!!!!!!!!!!!!

$GUI = GUICreate("Listview", 1024, 300, 0, 0)

$all_files_radio = GUICtrlCreateRadio("All Files", 5, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$png_radio = GUICtrlCreateRadio("PNG", 95, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$jpg_radio = GUICtrlCreateRadio("JPG", 185, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$gif_radio = GUICtrlCreateRadio("GIF", 275, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$cListView = GUICtrlCreateListView("Thumbnail|File Name|Extension", 5, 25, 1017, 271, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)

_GUICtrlListView_SetColumnWidth($hListView, 0, 64)
_GUICtrlListView_SetColumnWidth($hListView, 1, 225)
_GUICtrlListView_SetColumnWidth($hListView, 2, 225)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState()

Popualte()

While True
    Sleep(10)
WEnd

Func Popualte()

    $file_types = "*.gif;*.png;*.jpg"

    $file_paths_array = _FileListToArrayRec($images_dir, $file_types, $FLTAR_FILES, 1, $FLTAR_SORT, $FLTAR_FULLPATH) ;change to a directory that has images

    Global $all_files_array[UBound($file_paths_array)][3], $PNGs_array[UBound($file_paths_array)][3], $JPGs_array[UBound($file_paths_array)][3], $GIFs_array[UBound($file_paths_array)][3]

    $hImage = _GUIImageList_Create(64, 64, 5, 3)
    _GUICtrlListView_SetImageList($cListView, $hImage)

    _GDIPlus_Startup()

    _GUICtrlListView_BeginUpdate($cListView)

    For $x = 1 To UBound($file_paths_array) - 1
        $full_file_path = $file_paths_array[$x]
        $file_name_extension = _GetFileNameExtension($full_file_path)
        $file_extension = _GetFileExtension($file_name_extension)

        Select
            Case $file_extension = "png"
;~              $PNGs_array[$x][0] = thumbnail
                $PNGs_array[$x][1] = $file_name_extension
                $PNGs_array[$x][2] = $file_extension
            Case $file_extension = "jpg"
;~              $JPGs_array[$x][0] = thumbnail
                $JPGs_array[$x][1] = $file_name_extension
                $JPGs_array[$x][2] = $file_extension
            Case $file_extension = "gif"
;~              $GIFs_array[$x][0] = thumbnail
                $GIFs_array[$x][1] = $file_name_extension
                $GIFs_array[$x][2] = $file_extension
        EndSelect

;~      $all_files_array[$x][0] = thumbnail
        $all_files_array[$x][1] = $file_name_extension
        $all_files_array[$x][2] = $file_extension

        $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($full_file_path)
        $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 64, 64)
        $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized)
        $img = _GUIImageList_Add($hImage, $GDIbmp)

        $list = _GUICtrlListView_SetImageList($cListView, $hImage, 1)

        If $img <> -1 Then
            _GUICtrlListView_AddItem($cListView, StringRegExp($full_file_path, ".*\\(.+)$", 1), $img)
        Else
            _GUICtrlListView_AddItem($cListView, "")
        EndIf

        _GUICtrlListView_AddSubItem($cListView, $x - 1, $file_name_extension, 1)
        _GUICtrlListView_AddSubItem($cListView, $x - 1, $file_extension, 2)

        _GDIPlus_BitmapDispose($GDIpBmpLarge)
        _GDIPlus_BitmapDispose($GDIpBmpResized)
    Next

    _GUICtrlListView_EndUpdate($cListView)

    _GDIPlus_Shutdown()

    $all_files_array = _DeleteEmptyRows($all_files_array)
    $PNGs_array = _DeleteEmptyRows($PNGs_array)
    $JPGs_array = _DeleteEmptyRows($JPGs_array)
    $GIFs_array = _DeleteEmptyRows($GIFs_array)

    MsgBox(0, "", "done loading", 1)

EndFunc   ;==>Popualte

Func _DeleteEmptyRows($aArray)

    Local $Rows = UBound($aArray, 1)
    Local $Cols = UBound($aArray, 2)
    Local $aTemp[$Rows][$Cols]
    Local $not_empty
    Local $Count = 0

    ;Loop through rows
    For $y = 0 To $Rows - 1
        $not_empty = 0

        ;Loop through columns
        For $x = 0 To $Cols - 1

            ;Copy all columns to temp array even if they are all empty
            $aTemp[$Count][$x] = $aArray[$y][$x]

            ;If even one column contains data, make sure it doesn't get deleted
            If $aArray[$y][$x] <> "" Then $not_empty = BitOR($not_empty, 1)
        Next

        ;If the row has any data, increment, else keep overwriting last row until it contains something
        If $not_empty Then $Count += 1
    Next

    ReDim $aTemp[$Count][$Cols]
    Return $aTemp

EndFunc   ;==>_DeleteEmptyRows

Func _GetFileNameExtension($sFile)
    Return StringRegExpReplace($sFile, "^.*\\", "")
EndFunc   ;==>_GetFileNameExtension

Func _GetFileExtension($sFile)
    Return StringRegExpReplace($sFile, "^.*\.", "")
EndFunc   ;==>_GetFileExtension

Func Filter()

    _GUICtrlListView_DeleteAllItems($cListView)

    Select
        Case BitAND(GUICtrlRead($all_files_radio), $GUI_CHECKED)
            _GUICtrlListView_AddArray($cListView, $all_files_array)

        Case BitAND(GUICtrlRead($png_radio), $GUI_CHECKED)
            _GUICtrlListView_AddArray($cListView, $PNGs_array)

        Case BitAND(GUICtrlRead($jpg_radio), $GUI_CHECKED)
            _GUICtrlListView_AddArray($cListView, $JPGs_array)

        Case BitAND(GUICtrlRead($gif_radio), $GUI_CHECKED)
            _GUICtrlListView_AddArray($cListView, $GIFs_array)
    EndSelect

;~ _GUICtrlListView_RedrawItems($hListView, 0,4)

EndFunc   ;==>Filter

Func _Exit()
    Exit
EndFunc   ;==>_Exit

thank you in advance!

Edited by gcue
spelling errors

Share this post


Link to post
Share on other sites



i see _GDIPlus_ImageGetThumbnail but i dont think that can be added back to the listview - i think it has to be used in _gdiplus_graphicsdrawimagerect.  any other ideas?

Share this post


Link to post
Share on other sites

This should give you some ideas:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <array.au3>
#include <File.au3>
#include <GuiImageList.au3>
#include <GDIPlus.au3>

Opt("GUIOnEventMode", 1)

$images_dir = @DesktopDir ;choose an image directory!!!!!!!!!!!!

$GUI = GUICreate("Listview", 1024, 300, 0, 0)

$all_files_radio = GUICtrlCreateRadio("All Files", 5, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$png_radio = GUICtrlCreateRadio("PNG", 95, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$jpg_radio = GUICtrlCreateRadio("JPG", 185, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$gif_radio = GUICtrlCreateRadio("GIF", 275, 5, 80, 20)
GUICtrlSetOnEvent(-1, "Filter")

$cListView = GUICtrlCreateListView("Thumbnail|File Name|Extension", 5, 25, 1017, 271, -1, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($cListView)
$hImage = _GUIImageList_Create(64, 64, 5, 3)
_GUICtrlListView_SetImageList($cListView, $hImage, 1)

_GUICtrlListView_SetColumnWidth($hListView, 0, 64)
_GUICtrlListView_SetColumnWidth($hListView, 1, 225)
_GUICtrlListView_SetColumnWidth($hListView, 2, 225)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState()

$file_types = "*.gif;*.png;*.jpg"
$file_paths_array = _FileListToArrayRec($images_dir, $file_types, $FLTAR_FILES, 1, $FLTAR_SORT, $FLTAR_FULLPATH) ;change to a directory that has images
Global $all_files_array[UBound($file_paths_array)][3], $PNGs_array[UBound($file_paths_array)][3], $JPGs_array[UBound($file_paths_array)][3], $GIFs_array[UBound($file_paths_array)][3]
Popualte()

While True
    Sleep(10)
WEnd

Func Popualte()

    _GDIPlus_Startup()

    _GUICtrlListView_BeginUpdate($cListView)

    Local $p = 0, $j = 0, $g = 0
    For $x = 1 To UBound($file_paths_array) - 1
        $full_file_path = $file_paths_array[$x]
        $file_name_extension = _GetFileNameExtension($full_file_path)
        $file_extension = _GetFileExtension($file_name_extension)

        $GDIpBmpLarge = _GDIPlus_ImageLoadFromFile($full_file_path)
        $GDIpBmpResized = _GDIPlus_ImageResize($GDIpBmpLarge, 64, 64)
        $GDIbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($GDIpBmpResized)
        $img = _GUIImageList_Add($hImage, $GDIbmp)

        Select
            Case $file_extension = "png"
                $PNGs_array[$p][0] = $img
                $PNGs_array[$p][1] = $file_name_extension
                $PNGs_array[$p][2] = $file_extension
                $p += 1
            Case $file_extension = "jpg"
                $JPGs_array[$j][0] = $img
                $JPGs_array[$j][1] = $file_name_extension
                $JPGs_array[$j][2] = $file_extension
                $j += 1
            Case $file_extension = "gif"
                $GIFs_array[$g][0] = $img
                $GIFs_array[$g][1] = $file_name_extension
                $GIFs_array[$g][2] = $file_extension
                $g += 1
        EndSelect

        $all_files_array[$x-1][0] = $img
        $all_files_array[$x-1][1] = $file_name_extension
        $all_files_array[$x-1][2] = $file_extension

        If $img <> -1 Then
            _GUICtrlListView_AddItem($cListView, StringRegExp($full_file_path, ".*\\(.+)$", 1), $img)
        Else
            _GUICtrlListView_AddItem($cListView, "")
        EndIf

        _GUICtrlListView_AddSubItem($cListView, $x - 1, $file_name_extension, 1)
        _GUICtrlListView_AddSubItem($cListView, $x - 1, $file_extension, 2)

        _GDIPlus_BitmapDispose($GDIpBmpLarge)
        _GDIPlus_BitmapDispose($GDIpBmpResized)
    Next

    _GUICtrlListView_EndUpdate($cListView)

    _GDIPlus_Shutdown()

    ;$all_files_array = _DeleteEmptyRows($all_files_array)
    ;$PNGs_array = _DeleteEmptyRows($PNGs_array)
    ;$JPGs_array = _DeleteEmptyRows($JPGs_array)
    ;$GIFs_array = _DeleteEmptyRows($GIFs_array)

    MsgBox(0, "", "done loading", 1)

EndFunc   ;==>Popualte

Func _DeleteEmptyRows($aArray)

    Local $Rows = UBound($aArray, 1)
    Local $Cols = UBound($aArray, 2)
    Local $aTemp[$Rows][$Cols]
    Local $not_empty
    Local $Count = 0

    ;Loop through rows
    For $y = 0 To $Rows - 1
        $not_empty = 0

        ;Loop through columns
        For $x = 0 To $Cols - 1

            ;Copy all columns to temp array even if they are all empty
            $aTemp[$Count][$x] = $aArray[$y][$x]

            ;If even one column contains data, make sure it doesn't get deleted
            If $aArray[$y][$x] <> "" Then $not_empty = BitOR($not_empty, 1)
        Next

        ;If the row has any data, increment, else keep overwriting last row until it contains something
        If $not_empty Then $Count += 1
    Next

    ReDim $aTemp[$Count][$Cols]
    Return $aTemp

EndFunc   ;==>_DeleteEmptyRows

Func _GetFileNameExtension($sFile)
    Return StringRegExpReplace($sFile, "^.*\\", "")
EndFunc   ;==>_GetFileNameExtension

Func _GetFileExtension($sFile)
    Return StringRegExpReplace($sFile, "^.*\.", "")
EndFunc   ;==>_GetFileExtension

Func Filter()

    _GUICtrlListView_DeleteAllItems($cListView)

    Local $array
    Select
        Case BitAND(GUICtrlRead($all_files_radio), $GUI_CHECKED)
            ;_GUICtrlListView_AddArray($cListView, $all_files_array)
            $array = $all_files_array

        Case BitAND(GUICtrlRead($png_radio), $GUI_CHECKED)
            ;_GUICtrlListView_AddArray($cListView, $PNGs_array)
            $array = $PNGs_array

        Case BitAND(GUICtrlRead($jpg_radio), $GUI_CHECKED)
            ;_GUICtrlListView_AddArray($cListView, $JPGs_array)
            $array = $JPGs_array

        Case BitAND(GUICtrlRead($gif_radio), $GUI_CHECKED)
            ;_GUICtrlListView_AddArray($cListView, $GIFs_array)
            $array = $GIFs_array
    EndSelect

    Local $i = 0
    While $array[$i][1]
      _GUICtrlListView_AddItem($cListView, "", $array[$i][0])
      _GUICtrlListView_AddSubItem($cListView, $i, $array[$i][1], 1)
      _GUICtrlListView_AddSubItem($cListView, $i, $array[$i][2], 2)
      $i += 1
    WEnd

;~ _GUICtrlListView_RedrawItems($hListView, 0,4)

EndFunc   ;==>Filter

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Share this post


Link to post
Share on other sites

Hello. @LarsJ validate While $array[$i][1]

 

Saludos

Share this post


Link to post
Share on other sites

works nice Larsj!

thank you!!!

Dannyfirex, not sure what you mean

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