[RESOLVED] [02/03] Can't move pic file used by PictureBox
I have a representation of some files. If you click on one that is a picture, a PictureBox gives you a preview of that picture. You can also delete these files. I am implementing a Recycle Bin of sorts, but when I try to Move the file in question, I get an error that states that the file is in use by another process. This only occurs when deleting pics. I can delete other files just fine. I tried to get around this by cloning the image, but that doesn't seem to work. Here is the code I am using.
Quote:
Dim img as Image
img = Image.FromFile("C:\asdf.bmp")
Me.PictureBox.Image = img.Clone
And I use this to Move the file
Quote:
File.Move("C:\asdf.bmp", "C:\RecycleBin\asdf0.bmp")
This happens with all pics. Nothing else is using the file, only that PictureBox.
Any ideas?
Re: [02/03] Can't move pic file used by PictureBox
Try disposing of your img object ie
Code:
Dim img as Image
img = Image.FromFile("C:\asdf.bmp")
Me.PictureBox.Image = img.Clone
img.Dispose()
Re: [02/03] Can't move pic file used by PictureBox
Sorry, that didn't seem to do it.
It is strange. It is telling me that the new file name (asdf0.bmp) is currently in being used by another process. However, that file doesn't exist. Now, if I Copy the file first and then try to Delete the old one, I get an error message saying the old one (asdf.bmp) is in use, which makes sense.
Also, the PictureBox gets cleared each time a new node is selected. So, if the new node isn't a pic, you see nothing.
If I have a "folder" with three files, where 2 are pics and 1 is a doc, I never have any problems deleting the doc. But, if I have ever clicked on a pic, I can't delete it. If I click on pic1, then the doc, then "Delete All In Folder", I can't delete pic1. If I click on pic1, then pic2, then the doc, then "Delete All In Folder", I can't delete either pic1 or pic2.
It seems that my use of the file in the PicBox is locking that file up.
Re: [02/03] Can't move pic file used by PictureBox
I ran this code using a binary reader and Image.FromStream and it seems to work. To prevent form overwrite errors when moving the tile, I do a copy and then delete.
Code:
Dim img As Image
Dim br As New IO.BinaryReader(IO.File.Open("C:\temp.jpg", IO.FileMode.Open), System.Text.Encoding.Default)
img = Image.FromStream(br.BaseStream)
br.Close()
br = Nothing
Me.PictureBox1.Image = img
IO.File.Copy("C:\temp.jpg", "C:\RecycleBin\temp0.jpg", True)
IO.File.Delete("C:\temp.jpg")
Re: [02/03] Can't move pic file used by PictureBox
That'll do. Thank you so much.