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. File lock problem with Image.FromFile()

File lock problem with Image.FromFile()

Scheduled Pinned Locked Moved C#
helpquestiongraphics
4 Posts 3 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.
  • S Offline
    S Offline
    Stefan Spenz
    wrote on last edited by
    #1

    Hi, when I load an image using this Bitmap bitmap = (Bitmap)Image.FromFile(pic.Path), it is not possible to delete the file afterwards, unless you exit the program. I tried to call .Dispose() on the object, but it seems like a bug in the FromFile method. I found out a workaround Bitmap origbmp = new Bitmap(pic.Path); Bitmap bitmap = new Bitmap(origbmp); origbmp.Dispose();. I keep working with the copy of the original bitmap as it's not locked anymore. But, loading an image using Bitmap's constructor, it's impossible to load animated GIFs as it seems that Bitmap doesn't support them. Now, how can I use animated GIFs and other image formats AND be able to delete the file afterwards? Thx :-D Stefan

    G D 2 Replies Last reply
    0
    • S Stefan Spenz

      Hi, when I load an image using this Bitmap bitmap = (Bitmap)Image.FromFile(pic.Path), it is not possible to delete the file afterwards, unless you exit the program. I tried to call .Dispose() on the object, but it seems like a bug in the FromFile method. I found out a workaround Bitmap origbmp = new Bitmap(pic.Path); Bitmap bitmap = new Bitmap(origbmp); origbmp.Dispose();. I keep working with the copy of the original bitmap as it's not locked anymore. But, loading an image using Bitmap's constructor, it's impossible to load animated GIFs as it seems that Bitmap doesn't support them. Now, how can I use animated GIFs and other image formats AND be able to delete the file afterwards? Thx :-D Stefan

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Please don't cross post. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • S Stefan Spenz

        Hi, when I load an image using this Bitmap bitmap = (Bitmap)Image.FromFile(pic.Path), it is not possible to delete the file afterwards, unless you exit the program. I tried to call .Dispose() on the object, but it seems like a bug in the FromFile method. I found out a workaround Bitmap origbmp = new Bitmap(pic.Path); Bitmap bitmap = new Bitmap(origbmp); origbmp.Dispose();. I keep working with the copy of the original bitmap as it's not locked anymore. But, loading an image using Bitmap's constructor, it's impossible to load animated GIFs as it seems that Bitmap doesn't support them. Now, how can I use animated GIFs and other image formats AND be able to delete the file afterwards? Thx :-D Stefan

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

        Don't pat yourself on the back too hard. This has been known for at least 4 years now, and it's not exactly a bug. Though, some people would probably argue that it is. An eaiser method would be to load the bitmap from a FileStream instead of creating two Bitmaps to get one. You can read more about it in this[^] KB article on MSDN. Dave Kreskowiak Microsoft MVP - Visual Basic

        S 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Don't pat yourself on the back too hard. This has been known for at least 4 years now, and it's not exactly a bug. Though, some people would probably argue that it is. An eaiser method would be to load the bitmap from a FileStream instead of creating two Bitmaps to get one. You can read more about it in this[^] KB article on MSDN. Dave Kreskowiak Microsoft MVP - Visual Basic

          S Offline
          S Offline
          Stefan Spenz
          wrote on last edited by
          #4

          Dave Kreskowiak wrote:

          This has been known for at least 4 years now, and it's not exactly a bug. Though, some people would probably argue that it is.

          When you call .Dispose() and the handle won't get free, it's a bug!

          Dave Kreskowiak wrote:

          You can read more about it in this[^] KB article on MSDN.

          Thank you, that worked! :) Regards, Stefan

          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