|
-
Aug 24th, 2024, 06:26 PM
#1
Thread Starter
Lively Member
Images loaded into an imagelist aren't displaying in ListView window.
Short version:
I'm reading png images into an ImageList which are then loaded into a ListView, but no images appear.
Long version:
I'm reading over 200 image files (eg: "File01.png"... "File10.png"... "File100.png", etc.) using a "For/Each loop"
Problem is, it reads "File100.png" before "File11.png", so my program was displaying them in the wrong order.
My "solution" was to extract the file number from the filename and plug the paths into an array using the file number for its position, then (once the For/Each loop has finished), I read the image at those paths into an ImageList and then assign the ImageList to a ListView. But when I try (using a For/Next loop after the array is filled), the ListView comes up empty and I have no idea why. 
Code:
Sub LoadImageList()
' Clear old items.
lsvCollection.Clear()
ImageList2.Images.Clear()
lsvCollection.Refresh()
Dim strTempSortBuffer(256) As String ' NEW. File100 comes before File11,
' so we must sort icons using an array.
' Read Items.
intCnt = 1
strTempSortBuffer.Initialize() ' NEW
For Each ClosetFile As String In IO.Directory.GetFiles(strClosetPath, strCategory & "*.png")
' Fill array in "File number" order.
strTempSortBuffer(Val(Mid(Path.GetFileNameWithoutExtension(ClosetFile.ToString), Len(strCategory) + 1))) = ClosetFile.ToString
' Old code: Dim img = Image.FromFile(strTempSortBuffer(intCnt)) ' pre-sorted array has gaps, so we must add images later AFTER array is filled.
' ImageList2.Images.Add((intCnt - 1).ToString, img)
' img.Dispose()
intCnt += 1
Next
' NEW code:
intTotalNumberOfFiles = intCnt - 1
For intCnt = 1 To intTotalNumberOfFiles
Dim img As Image = Image.FromFile(strTempSortBuffer(intCnt))
ImageList2.Images.Add(intCnt - 1, img)
img.Dispose() ' Tried WITH & WITHOUT this line.
Next
' after adding items to the listview, set the columns to autosize.
lsvCollection.Columns.Add("Column 1", 60, HorizontalAlignment.Left)
lsvCollection.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize)
lsvCollection.SmallImageList = ImageList2 ' Reassign to list with icons.
lsvCollection.View = View.Details
...
The old code "worked" (images appear) but in the wrong order after "File10". SAME code performed outside the ForEach loop results in NO images being displayed. Debugger says the image list has images in it. Paths are fine. I even tried using a hard coded path and it still didn't work.
Any ideas? I'm stumped. TIA
-
Aug 25th, 2024, 05:52 AM
#2
Re: Images loaded into an imagelist aren't displaying in ListView window.
This worked for me (I used .net framework 4.8.1)
Code:
Dim imgList As New ImageList
Dim imgFileName As String = ""
Dim img As Image
Dim fileNumber As String
Dim fileIndex As Int16 = 0
Dim imgFileNames(400) As String
Dim maxFileIndex As Integer
' assign the filename to the appropriate array index
For Each imgFileName In IO.Directory.GetFiles("F:\Temp\ListViewImages\", "*.jpg")
fileNumber = imgFileName.Substring(imgFileName.IndexOf("File") + 4, (imgFileName.IndexOf(".jpg") - 1) - (imgFileName.IndexOf("File") + 4) + 1)
fileIndex = Convert.ToInt16(fileNumber)
imgFileNames(fileIndex) = imgFileName
If fileIndex > maxFileIndex Then maxFileIndex = fileIndex
Next
ReDim Preserve imgFileNames(maxFileIndex)
For fileIndex = 0 To UBound(imgFileNames)
If Not imgFileNames(fileIndex) Is Nothing Then
' add the file if there is a path in the array
img = Image.FromFile(imgFileNames(fileIndex))
imgList.Images.Add(img)
img = Nothing
lvImages.Items.Add(IO.Path.GetFileName(imgFileNames(fileIndex))) ' display the filename in the listview
lvImages.Items(lvImages.Items.Count - 1).ImageIndex = lvImages.Items.Count - 1
End If
Next
lvImages.Columns.Add("FileName", 60, HorizontalAlignment.Left)
lvImages.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize)
lvImages.SmallImageList = imgList ' Reassign to list with icons.
lvImages.View = View.Details
-
Aug 27th, 2024, 02:57 PM
#3
Guest
Re: Images loaded into an imagelist aren't displaying in ListView window.
My posting privileges were revoked and I have no idea why, but thanks. I'll try this.
-
Aug 27th, 2024, 03:32 PM
#4
Re: Images loaded into an imagelist aren't displaying in ListView window.
-
Aug 27th, 2024, 04:46 PM
#5
Guest
Re: Images loaded into an imagelist aren't displaying in ListView window.
-
Aug 27th, 2024, 04:57 PM
#6
Re: Images loaded into an imagelist aren't displaying in ListView window.
You obviously can post replies. lol
-
Aug 27th, 2024, 05:48 PM
#7
Guest
Re: Images loaded into an imagelist aren't displaying in ListView window.
 Originally Posted by wes4dbt
You obviously can post replies. lol
Look again. I had to create a new account just to reply.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|