CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 21st, 2009, 08:20 PM
    rliq's Avatar
    rliq rliq is offline
    Member
     
    Join Date: Jun 2001
    Location: Melbourne/Aus
    Posts: 334
    rliq has a spectacular aura about (100+) rliq has a spectacular aura about (100+)
    Picturebox keeping Image file open?

    In my initialisation I have:
    Code:
     // read the image
     Image thumbnailImage = Image.FromFile(thumbnailFilename);
    
     // put the image in a picturebox
     PictureBox pb = new PictureBox();
     pb.Image = thumbnailImage;
    Later on, I allow the user to right-click on the PictureBox and choose to remove the image. However, when I try to:
    Code:
     File.Delete(thumbnailFileName);
    It reports the Image file as being open by another process. Similarly if I try to delete it from within Windows Explorer it reports the file is open in vshost32-clr2.exe.

    How can I put an Image from a File into a PictureBox, without keeping the file open?
    __________________
    ---
    Rob
    I'm based on a true story, set sometime in the near future.

    A "Dumb" question, is one that you wished you had asked when you had the chance.
    ---
    Reply With Quote
      #2    
    Old November 21st, 2009, 08:29 PM
    invader7 invader7 is offline
    Member
     
    Join Date: Jun 2009
    Posts: 96
    invader7 is on a distinguished road (10+)
    Re: Picturebox keeping Image file open?

    before
    Code:
    File.Delete(thumbnailFileName);
    try to put
    Code:
    pb.Image.Dispose();
    Reply With Quote
      #3    
    Old November 21st, 2009, 09:43 PM
    rliq's Avatar
    rliq rliq is offline
    Member
     
    Join Date: Jun 2001
    Location: Melbourne/Aus
    Posts: 334
    rliq has a spectacular aura about (100+) rliq has a spectacular aura about (100+)
    Re: Picturebox keeping Image file open?

    Thanks, that fixed it. Infact I had already tried
    Code:
    pb.Dispose();
    which had not worked.
    __________________
    ---
    Rob
    I'm based on a true story, set sometime in the near future.

    A "Dumb" question, is one that you wished you had asked when you had the chance.
    ---
    Reply With Quote
      #4    
    Old November 21st, 2009, 11:47 PM
    Arjay's Avatar
    Arjay Arjay is online now
    Moderator / MS MVP
    Power Poster
     
    Join Date: Aug 2004
    Posts: 6,914
    Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+)
    Re: Picturebox keeping Image file open?

    Another way to do this is to open the image from a Stream with a using block.
    Reply With Quote
      #5    
    Old November 22nd, 2009, 05:11 PM
    rliq's Avatar
    rliq rliq is offline
    Member
     
    Join Date: Jun 2001
    Location: Melbourne/Aus
    Posts: 334
    rliq has a spectacular aura about (100+) rliq has a spectacular aura about (100+)
    Re: Picturebox keeping Image file open?

    Using a stream maybe neater. I am in the 'get it working' phase at the moment, I'll try that on my first refactor...

    Thanks to both for the ideas.
    __________________
    ---
    Rob
    I'm based on a true story, set sometime in the near future.

    A "Dumb" question, is one that you wished you had asked when you had the chance.
    ---
    Reply With Quote
      #6    
    Old November 24th, 2009, 06:14 PM
    rliq's Avatar
    rliq rliq is offline
    Member
     
    Join Date: Jun 2001
    Location: Melbourne/Aus
    Posts: 334
    rliq has a spectacular aura about (100+) rliq has a spectacular aura about (100+)
    Re: Picturebox keeping Image file open?

    I'm still having fun with this. I think my current problem(s) may all be related to the following question:

    This works:
    Code:
    Image image;
    
    using (FileStream fs = new FileStream(imageFileName, FileMode.Open))
    {
        _image = Image.FromStream(fs);
        _image.Save(anotherFileName);
    }
    But this gives 'A generic error occured in GDI+'
    Code:
    Image image;
    
    using (FileStream fs = new FileStream(imageFileName, FileMode.Open))
    {
        _image = Image.FromStream(fs);
    }
    _image.Save(anotherFileName);  // <-- errors here
    It seems as though the Image remains 'attached' to the FileStream, which has been disposed in the second example. Also, it seems that Image.FromFile(...) causes the Image to remain attached to the File.

    How can I get an Image from a file and detach it from everything, so another process can overwrite the original file for example?
    __________________
    ---
    Rob
    I'm based on a true story, set sometime in the near future.

    A "Dumb" question, is one that you wished you had asked when you had the chance.
    ---
    Reply With Quote
      #7    
    Old November 25th, 2009, 02:27 AM
    foamy's Avatar
    foamy foamy is offline
    Member
     
    Join Date: May 2007
    Location: Denmark
    Posts: 401
    foamy has a spectacular aura about (150+) foamy has a spectacular aura about (150+)
    Re: Picturebox keeping Image file open?

    I don't think you can detach it from the file system as such, but you could copy the file to a temporary directory and display the copy to the user. Then the original file should be left open.
    __________________
    It's not a bug, it's a feature!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:27 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.