Jump to content

Image List


Eluvatar
 Share

Recommended Posts

the problem I'm having is that I cant add a jpg image to an image list :x and I'd love to know how, first off I'v writen a script to copy images from one place to another and once its done I want to display a preview of all the images in my gui along with there title, the amount of images can vary from 1 to 20 so it wont always fit in the gui and I want to put it into a list, also due to the fact that I'm copying from one place to another (preserving the file type) I cant change the images to bmp because of why I'm copying the images. note this works fine when I'v changed the image to a bmp but not as a jpg, I have also tried using GDIp to load the image and convert it into a bmp but I didnt know what I was doing so if someone could show me what too do that would be great, thanks

#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#Include <GuiListView.au3>
#include <ImageListConstants.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>


Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
Global $Form2 = GUICreate("Form2", 405, 296, 302, 218)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")

Global $ListView1 = GUICtrlCreateListView("", 63, 31, 288, 222, $LVS_REPORT)
GUICtrlSetOnEvent(-1, "ListView1Click")

$ImageList1 = _GUIImageList_Create(150, 150)

_GUIImageList_AddBitmap($ImageList1, "test.jpg")

ConsoleWrite(_GUICtrlListView_SetImageList($listview1, $ImageList1, 1) & @CRLF)
_GUICtrlListView_AddColumn($listview1, "Items", 222)
_GUICtrlListView_AddItem($ListView1, "testimg", 0)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Form2Close()
    Exit
EndFunc
Link to comment
Share on other sites

#Include <GDIPlus.au3>
#Include <GUIImageList.au3>
#Include <GUIListView.au3>

_GDIPlus_Startup()

$hForm = GUICreate('MyGUI', 400, 400, 302, 218)
$hListView = GUICtrlCreateListView('', 10, 10, 380, 380)
_GUICtrlListView_AddColumn($hListView, 'Items', 200)
$hImageList = _GUIImageList_Create(150, 160, 5, 1)
_GUICtrlListView_SetImageList($hListView, $hImageList, 1)
_GUIImageList_AddImage($hImageList, -1, @ScriptDir & '\test.jpg')
_GUICtrlListView_AddItem($hListView, 'Test', 0)
GUISetState()

Do
Until GUIGetMsg() = -3

Func _GUIImageList_AddImage($hWnd, $iIndex, $sFile)

    Local $Size = _GUIImageList_GetIconSize($hWnd)

    If (Not $Size[0]) Or (Not $Size[1]) Then
        Return -1
    EndIf

    Local $W, $H, $hGraphic, $hPic, $hImage, $hIcon

    _GDIPlus_Startup()
    $hPic = _GDIPlus_ImageLoadFromFile($sFile)
    $W = _GDIPlus_ImageGetWidth($hPic)
    $H = _GDIPlus_ImageGetHeight($hPic)
    If ($W < 0) Or ($H < 0) Then
        _GDIPlus_Shutdown()
        Return -1
    EndIf
    If $W < $H Then
        $W = $Size[0] * $W / $H
        $H = $Size[1]
    Else
        $H = $Size[1] * $H / $W
        $W = $Size[0]
    EndIf
    $hImage = DllCall($ghGDIPDll, 'int', 'GdipGetImageThumbnail', 'ptr', $hPic, 'int', $Size[0], 'int', $Size[1], 'ptr*', 0, 'ptr', 0, 'ptr', 0)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage[4])
    _GDIPlus_GraphicsClear($hGraphic, 0)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hPic, ($Size[0] - $W) / 2, ($Size[1] - $H) / 2, $W, $H)
    $hIcon = DllCall($ghGDIPDll, 'int', 'GdipCreateHICONFromBitmap', 'ptr', $hImage[4], 'ptr*', 0)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage[4])
    _GDIPlus_ImageDispose($hPic)
    _GDIPlus_Shutdown()
    If Not $hIcon[2] Then
        Return -1
    EndIf
    $iIndex = _GUIImageList_ReplaceIcon($hWnd, $iIndex, $hIcon[2])
    _WinAPI_DestroyIcon($hIcon[2])
    Return $iIndex
EndFunc   ;==>_GUIImageList_AddImag

Link to comment
Share on other sites

The code works for me in Windows XP Pro SP3, but I don't know if it works "the same" without seeing how it works in Vista/7.

I don't see any black bars, but trying changing this line from this:

_GDIPlus_GraphicsClear($hGraphic, 0)
to this.

_GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)
perfect, I looked for a color code like that but didnt see any and I didnt know what graphicsclear meant, thanks Deathbringer
Link to comment
Share on other sites

  • 2 weeks later...

so I'v been using the code posted for a while, and it works great but the only problem is now that I cant remove items and images from the list :x here is some of the code

;code to create the lists in the first place
Global $ListView1 = GUICtrlCreateListView( "", 5, 80, 409, 242)
$ImageList1 = _GUIImageList_Create(150, 150, 5, 1)
_GUICtrlListView_SetImageList($listview1, $ImageList1, 1)
_GUICtrlListView_AddColumn($listview1, "Images", 240)

;code to add images and there items
_GUICtrlListView_AddItem($ListView1, $fileName, _GUIImageList_AddImage($fileName))

I found if I create the listview with "_GUICtrlListView_Create" and not the normal I can remove items, (using "_GUICtrlListView_DeleteAllItems") except I cant figure out how to force the list to only display on the tab I want it too, (my gui has a tab design) so I use the normal way to create a listview but I cant figure out how to remove all the items from the list so I can add some more later.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...