Results 1 to 5 of 5

Thread: [RESOLVED] [02/03] Can't move pic file used by PictureBox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Resolved [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.
    Dim img as Image
    img = Image.FromFile("C:\asdf.bmp")
    Me.PictureBox.Image = img.Clone
    And I use this to Move the file
    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?
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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()
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    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.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  4. #4
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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")
    Last edited by bmahler; Jul 3rd, 2007 at 01:25 PM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [02/03] Can't move pic file used by PictureBox

    That'll do. Thank you so much.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width