Problem Using List(Of Image) RRS feed

  • Question

  • I am trying to use a List(Of Image) to store various images from a single picture box. The images displayed in the picture box are not from a file. They are data plotted and displayed in the picture box. The problem occurs when recalling these images.

    All the images are the same as the last image added. One post that is most relevant advises to create a clone of the picture box's image and add that to the list. I've tried every variation I can think of but I'm having no success. Seems the List doesn't actually create a new bitmap when adding an image?

    What I need to accomplish is:

    1) Capture a picture box image and add to a List(Of Image). Up to about 25 images all the same size (921 x 531).

    2) Recall an Image from List(Of Image).

    I've got everything else working well, except for the glitch where all the recalled images are the same as last image added.

    Greatly simplifying, it should work like this?

    Public ImageList As New List(Of Image)       ' Declare / Define the List

    ImageList.Add(Bitmap)     ' Add the bitmap to the list

    Dim TraceRect As New Rectangle(50,190,750,460)     ' Define the rectangle for printing

    e.Graphics.DrawImage(ImageList.Item(PageNumber), TraceRect)     ' Print the image in the list

    Any pointers or assistance is greatly appreciated.

    Monday, July 29, 2019 2:23 AM


  • Hi,
    try this demo:

    Public Class Form1
      Private pb As New PictureBox With {.Dock = DockStyle.Fill}
      Public ImageList As New List(Of Image)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      End Sub
      Private Sub FillImageList()
        For i = 1 To 10
          Dim img As New Bitmap(921, 531)
          Dim gr As Graphics = Graphics.FromImage(img)
          gr.DrawString($"Image {i}", Me.Font, Brushes.Red, New PointF(100, 100))
      End Sub
      Private rnd As New Random
      Private Delegate Sub del(i As Integer)
      Private Sub DisplayImage()
        Task.Run(New Action(Sub()
                                pb.Invoke(New del(Sub(i As Integer)
                                                    pb.Image = ImageList(i)
                                                  End Sub), rnd.Next(1, 10))
                            End Sub), Nothing)
      End Sub
    End Class

    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    • Marked as answer by dw80916 Monday, August 19, 2019 9:22 PM
    Monday, July 29, 2019 3:53 AM