Domanda

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?

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top