0

I am using the following code in order to delete all my controls inside a FlowLayoutPanel:

While FlowLayoutPanel1.Controls.Count > 0
    Dim controltoremove = FlowLayoutPanel1.Controls(0)
    FlowLayoutPanel1.Controls.Remove(controltoremove)
    controltoremove.Dispose()
    Application.DoEvents()
End While

This seems to work (as in i no longer see my controls inside my FlowLayoutPanel1 box) but once i get to this part of the code:

My.Computer.FileSystem.DeleteFile("C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")

It tells me that the image is currently in use??? Since i disposed these controls (that have these images as backgroundimage) shouldn't i be able to delete the image file??

To create the buttons inside the FlowLayoutPanel i use this:

For Each subitem As JObject In item.Values
   Dim newPictureBox As New Button
   Dim Client As New WebClient

   strID = "http://graph.facebook.com/" & subitem("id").ToString.Replace("""", "") & "/picture?width=126&height=114"
   strName = subitem("name").ToString

   Client.DownloadFile(strID, "C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")
   Client.Dispose()

   Dim bmp As New Bitmap(Image.FromFile("C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg"))
   newPictureBox.BackgroundImage = Image.FromFile("C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")
   newPictureBox.Tag = subitem("id").ToString.Replace("""", "")
   newPictureBox.Name = "img" & intX
   newPictureBox.Width = bmp.Width.ToString()
   newPictureBox.Height = bmp.Height.ToString()
   FlowLayoutPanel1.Controls.Add(newPictureBox)
   AddHandler newPictureBox.Click, AddressOf newPictureBox_Click
   intX += 1
   imgDir.Add(strName, subitem("id").ToString.Replace("""", ""))
   bmp.Dispose()
Next

And it looks like the problem is the Dim newPictureBox As New Button. If i add

newPictureBox.dispose()

at the end of that code above it works with deleting the images from the directory... but is never displays the images inside the buttons to begin when if i do that :o/

What would I be doing incorrectly?

tshepang
  • 12,111
  • 21
  • 91
  • 136
StealthRT
  • 10,108
  • 40
  • 183
  • 342

1 Answers1

1

This will definitely work for you, sorry for the delay... Make sure you Import System.IO namespace as well... When you use the Image.FromFile(strFileName) method to create the Image, the method locks the file until you release the Image. Instead we can use FileStream to accomplish this task.

 For Each subitem As JObject In item.Values
  Dim newPictureBox As New Button
  Dim Client As New WebClient

  strID = "http://graph.facebook.com/" & subitem("id").ToString.Replace("""", "") & "/picture?width=126&height=114"
  strName = subitem("name").ToString

  Client.DownloadFile(strID, "C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg")
  Client.Dispose()

  Dim bmp As Bitmap
  Dim strPath As String = "C:\wamp\www\img\" & subitem("id").ToString.Replace("""", "") & ".jpg"

  Using fs As New FileStream(strPath, FileMode.Open, FileAccess.Read)
        Using b As New Bitmap(fs)
            bmp = New Bitmap(b.Width, b.Height, b.PixelFormat)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.DrawImage(b, Point.Empty)
                g.Flush()
            End Using
        End Using
  End Using     

  newPictureBox.BackgroundImage = bmp
  newPictureBox.Tag = subitem("id").ToString.Replace("""", "")
  newPictureBox.Name = "img" & intX
  newPictureBox.Width = bmp.Width.ToString()
  newPictureBox.Height = bmp.Height.ToString()
  FlowLayoutPanel1.Controls.Add(newPictureBox)
  AddHandler newPictureBox.Click, AddressOf newPictureBox_Click
  intX += 1
  imgDir.Add(strName, subitem("id").ToString.Replace("""", ""))

 Next
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • :o( did not work either. It has to do with the **Dim newPictureBox As New Button**. – StealthRT Feb 18 '14 at 04:20
  • Did you try to wrap your buttons in a using statement? – Trevor Feb 18 '14 at 04:22
  • its clearing the flow just fine.. its just leaving the newPictureBox for some reason. Thats what i need to get rid of. – StealthRT Feb 18 '14 at 04:33
  • Please see the edit, it wasn't the buttons that you were creating rather how you were grabbing the images specifically. Please see updated answer. Also I have tried and tested this with deleting images as well :) – Trevor Feb 18 '14 at 05:15