Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. releasing PictureBox's Image file handle

releasing PictureBox's Image file handle

Scheduled Pinned Locked Moved C#
questionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ABean
    wrote on last edited by
    #1

    In the code below I load an image into a PictureBox using the Image.FromFile method. When I am done with that image I would like to release it so that I can delete it from the hard drive. However, even after I set the PictureBox's Image property to another image I still can not delete it. Obviously my process still has a handle to the file but I can't find a way to release that handle. I tried looking through the methods and properties of pictureBox1.Image but I did not see anything that looked useful. Any ideas? ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile); //This fails becuase MyFile is "in use by another process"

    A 1 Reply Last reply
    0
    • A ABean

      In the code below I load an image into a PictureBox using the Image.FromFile method. When I am done with that image I would like to release it so that I can delete it from the hard drive. However, even after I set the PictureBox's Image property to another image I still can not delete it. Obviously my process still has a handle to the file but I can't find a way to release that handle. I tried looking through the methods and properties of pictureBox1.Image but I did not see anything that looked useful. Any ideas? ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile); //This fails becuase MyFile is "in use by another process"

      A Offline
      A Offline
      ABean
      wrote on last edited by
      #2

      Got it. Thanks. ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image.Dispose(); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile);

      D 1 Reply Last reply
      0
      • A ABean

        Got it. Thanks. ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image.Dispose(); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile);

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        i was just about to say, Dispose the Image. When you Dispose an object, it releases any unmanaged resources it has open, including file handles. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups