Store Images

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
I am dynamically creating picture boxes of size 600x400 and each will have some images from the web assign to them at runtime. What is the best way to store those images so when i click on the picture box, it will cycle thru them in a loop?
Right now when i click the box, it set the .imagelocation to a single image url. But i want to change it so one box will associated with a few images stored in some image array.
 
Last edited:
Hi,

I am not sure if I have understood you correctly here but to start with this is how you can iterate through the images that you have already added to your flow layout panel:-

VB.NET:
Dim ImageLocations As New List(Of String)
 
'flp is your flow layout panel
For Each myControl As Control In flp.Controls
  If myControl.GetType = GetType(PictureBox) Then
    Dim curPic As PictureBox = DirectCast(myControl, PictureBox)
    ImageLocations.Add(curPic.ImageLocation)
  End If
Next
I have used a list here to store the ImageLocation property but maybe you can use this to complete what it is that you want to do.

Cheers,

Ian
 
you are getting the imagelocation from the pictureboxes and putting them into one big list. What i want is the other way around. I need a lot of little arrays to store the actual images. i need the array to attach to their picturebox. I already created a custom type to store info in the picbox.tag. Would it be good idea to store the actual images in the tag? Or is there a better way to store images?
I had a backgroundworker to pull all the urls of the images into the tag. I was thinking of pulling the images into multiple arrays that attach to the picturebox with the backgroundworker too
 
Hi,

Oh, I think I get what you mean now? Are you saying that you want to store a variable number of additional images to a particular image?

Then the answer is yes, if you remember the timer code that I posted to a previous thread of yours? Well I have modified it to associate other picture names to the same picture. You may well need to change the variable type to accommodate your actual needs but I think you should be able to see how this works.

VB.NET:
Public Class Form1
 
  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    'I have used this timer to simulate the pictures being add to your flow layout panel
    Static Counter As Integer
 
    'firstly create a SINGLE instance of the class that is going to hold all the details
    'that you want to save in the ta property of the picture
    'See the class myTagDetails defined below to hold the information for the tag
    Dim myTagInfo As New myTagDetails
    With myTagInfo
      .PicName = "Sample Pic"
      .PicSize = 1000
      .TimeAdded = Now()
      .OtherImages.Add("Pic1")
      .OtherImages.Add("Pic2")
    End With
 
    'Now create the pciturebox for the new image and set whatever properties you need
    'finally add the myTagInfo variable to the tag property. Remember this is a single 
    'instance of the class but it has multiple properties
    Dim PicBox As New PictureBox
    With PicBox
      .Name = "PictureBox" & Counter
      .Image = My.Resources.BionatureLoginPic 'add your own picture here
      .Width = 50
      .Height = 50
      .Tag = myTagInfo
    End With
 
    'add the picturebox to the flowlayoutpanel and add an event handler for the click event
    flp.Controls.Add(PicBox)
    AddHandler PicBox.Click, AddressOf PictureBoxClicked
  End Sub
 
  Private Sub PictureBoxClicked(sender As System.Object, e As System.EventArgs)
    'so here is the event handler that is processed when each button is clicked
    'The first this you see is that DirectCast is used to convert the sender object to type PictureBox
    Dim ClickedPicBox As PictureBox = DirectCast(sender, PictureBox)
    'Once we have got a Valid PictureBox type we can then convert the tag property which is now an object to its correct type
    'bring type myTagDetails using the same principal as above
    Dim PicTagDetails As myTagDetails = DirectCast(ClickedPicBox.Tag, myTagDetails)
    'now that we have two corrected typed variables we can now do whatever we want with the selected picturebox.
    MsgBox(PicTagDetails.PicName)
    MsgBox(PicTagDetails.PicSize)
    MsgBox(PicTagDetails.TimeAdded)
  End Sub
 
  Private Class myTagDetails
    'Here we define a class that has multiple properties tht be be set as needed
    'any variable type can be used here to do whatever you want
    Public Property PicName As String
    Public Property PicSize As Integer
    Public Property TimeAdded As DateTime
    Public Property OtherImages As New List(Of String)
  End Class
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'flp is your flow layout panel
    For Each myControl As Control In flp.Controls
      If myControl.GetType = GetType(PictureBox) Then
        Dim curPic As PictureBox = DirectCast(myControl, PictureBox)
        Dim mytagInfo As myTagDetails = DirectCast(curPic.Tag, myTagDetails)
        With mytagInfo
          .OtherImages.Add("Pic1")
          .OtherImages.Add("Pic2")
          .OtherImages.Add("Pic3")
        End With
      End If
    Next
  End Sub
End Class
Cheers

Ian
 
I guess what i am asking is... Is it a good idea to store images in a tag? Is there any kind of memory limitation or whatever? Why is the imagelist limit you to 256x256?

VB.NET:
    Private Class picTagDetails
        Public Property imageHQUrl As String    'HQ image url
        Public Property imageHQ As Image        'HQ image
        Public Property imageLQUrl As String    'LQ image url
        Public Property imageLQ As Image        'LQ image
    End Class


dim tagDetail as new picTagDetails
with tagDetail
   .imageHQUrl =URL
   .imageHQ = System.Drawing.Bitmap.FromStream(Net.HttpWebRequest.Create(URL).GetResponse.GetResponseStream)
end with

dim picBox as new picturebox
picBox.tag = tagdetail
 
Hi,

From what I can see there is no issue with this since you are storing the information in your defined class as you need. As for the image size issue, I cannot comment, since I am not a Microsoft expert.

Maybe one of the forum experts can see an issue with this, but if they do, I will be learning something new along with yourself.

Cheers,

Ian
 
Back
Top