Images do not appear in listview

Hi all,

I am using a listview and have images being shown in the first column, however these images do not show up every time. Every now and then, there is no set pattern, the images do not show and there is an empty space instead.

If anyone can help me it would be greatly appreciated.

Thanks in advance



Answer this question

Images do not appear in listview

  • J. Hammer

    Nothing really jumps out as "that's why!". You specifically use a blank bitmap when the XML string is empty, be sure to put a breakpoint on that statement.

    More relevant perhaps is that your app is behaving as though the garbage collector is disposing your images. That would explain the random behavior. Test that theory first by calling GC.Collect() after you've loaded the images. If that reliably produces blank images, the LoadIcon() function would be the possible cause. GDI+ is funky about loading images, if it loads them from a file, it keeps a lock on the file handle. Something similar may happen when it loads them from a stream. The 'stream' and 'reader' local variables would definitely be garbage collected...


  • Amde

    Thanks for the help so far nobugz, however the GC.Collect() did not change anything. Images are still shown and not shown at random times.


  • Mohan1

    I don't know if this has anything to do with missing images, but i was testing various ideas and noticed that the Handle of

    StatusImageList

    was changing. I ran 100 tests of the program and recieved the following results:

    Handle Images No Images
    1963784 10
    1961816 1
    1961552 2
    1963608 40
    1963400 29
    1963576 15
    1962896 1
    1962664 1
    1962688 1

    The results are interesting because it shows that certain handles show the images whilst others do not. Could this be the reason the images do not show


  • Gianpi

    Windows starts randomly dropping images when it is running low on available GDI handles. Check if a reboot fixes this problem.


  • Ed Steele

    Dim stream As System.IO.Stream = LoadResource( strIdentifier, bShared )

    Dim reader As System.IO.StreamReader = New System.IO.StreamReader( stream )

    Return New System.Drawing.Icon( reader.BaseStream ).ToBitmap()

    reader.Close() // try this or something that will close the stream

    The other thing is that the xml where the icon setting is coming from might not be properly release

     


  • coder2k

    I'm out of ideas. If you email me your project, I'll take a stab at it. |Monkeytail|=@


  • andyedw

    We need to see some of your code to help you past this point. Try to put a small demo together that exhibits this behavior, then post that code to this forum.


  • SivaS

    Below is the code to view the images.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    listview1.SmallImageList = cResources.Instance.StatusImageList

    listview1.View = View.SmallIcon

    listview1.Columns.Add( "test", 100, HorizontalAlignment.Center )

    listview1.Items.Add( "test", 1 )

    End Sub

    When this form loads the images do no always show, trying to post the cResources.Instance.StatusImageList code is going to be harder because i have inherited this code from some one else and trying to break down what is happening to post something here is goign to take some time.

    I have found out some more, when the images do not show up the cResources.Instance.StatusImageList does have the images loaded.

    I have also been able to find the code for loading the images

    Do

    'Get the icon from the element representing this status

    Dim attIcon As System.Xml.XmlAttribute = elmStatus.Attributes( XML_ATTR_ICON )

    Debug.Assert( Not attIcon Is Nothing )

    'Load the named icon from resource

    Dim icon As System.Drawing.Image = Nothing

    If ( Not attIcon.Value = String.Empty )

    Dim strResource As String = attIcon.Value

    Console.WriteLine( strResource )

    icon = LoadIcon( strResource, True )

    Else

    icon = bmpBlank

    End If

    'Add the icon to the statius image list

    Debug.Assert( Not icon Is Nothing )

    m_StatusImageList.Images.Add( icon )

    elmStatus = elmStatus.NextSibling

    Loop While ( Not ( elmStatus ) Is Nothing )

    The Load Icon function

    Public Function LoadIcon( ByRef strIdentifier As String, Optional ByVal bShared As Boolean = False ) As System.Drawing.Image

    Dim stream As System.IO.Stream = LoadResource( strIdentifier, bShared )

    Dim reader As System.IO.StreamReader = New System.IO.StreamReader( stream )

    Return New System.Drawing.Icon( reader.BaseStream ).ToBitmap()

    End Function

    The load resource function

    Public Function LoadResource( ByVal strResource As String, Optional ByVal bShared As Boolean = False ) As System.IO.Stream

    Dim resource As System.IO.Stream = Nothing

    If ( Not bShared )

    'Prepend the resource name with the application name

    Dim strAssemblyName() As String = Reflection.Assembly.GetEntryAssembly.FullName.Split( "," )

    strResource = strAssemblyName( 0 ) & "." & strResource

    resource = Reflection.Assembly.GetEntryAssembly.GetManifestResourceStream( strResource )

    Else

    'Prepend the resource name with the shared module assembly name

    strResource = "ClientShared." & strResource

    resource = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream( strResource )

    End If

    'Load the resource

    Debug.Assert( Not resource Is Nothing )

    Return resource

    End Function

    It appears that despite the images being loaded into the imagelist, they are not being shown up.  I have tracked the flow in visual studios and have checked at every step of the way that the images are present, and i can say that they are.  This is why this problem is really bugging me, because there does not seem to be an answer.

    Any help would be appreciated.

    Thanks


  • hrubesh

    If the image index you use for a particular Listview item doesn't match any of those in the ImageList then you will get a blank image. Is that a possibility

  • Mr.Yesser

    Are you using some sort of double-buffering
  • D--

    Thanks you the effort, i'll keep trying things and if i get a solution i'll post it here.


  • Scott_P

    A reboot dosen't fix the problem, whether the images show up or not seems completely random. I cannot seem to work out why and have been working on this for quite a while and have got very fustrated.

    I have loaded the images into a image list and then when the list view is shown the images should appear in the first column, this does not always happen and i cannot seem to track down why.


  • dumian

    As far as i can tell no, there is always images in the image list and when the images do not show up, there is no images in the program at all. However, the next time the program loads the images appear. There is no constant action that makes the images not appear, which makes it hard to find a solution.


  • Salvatore Di Fazio

    I've come across

    ListView_SetImageList

    Does anyone know if it could solve the problem and how to use it


  • Images do not appear in listview