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 bitmap file after open

Releasing bitmap file after open

Scheduled Pinned Locked Moved C#
graphicshelpannouncement
14 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.
  • R Offline
    R Offline
    Rick Beideman
    wrote on last edited by
    #1

    I read a bitmap from file with the following code: Bitmap tempBitmap = new Bitmap(bitmapFileName); Later I want to save an updated version of the bitmap with this code: if (System.IO.File.Exists(bitmapFileName) System.IO.File.Delete(bitmapFileName); tempBitmap.Save(bitmapFileName); Unfortunately, when it gets to the delete, I get the following error: "System.IO.IOException: The process cannot access the file "filename.bmp" because it is being used by another process." The creation of the bitmap from file is holding on to the connection. I tried cloning and copying to the clipboard, but the only way I have been able to get this to work is by copying all my bitmaps to temp files and opening from the temp file. Then the file I delete and save to is a different file. I am hoping there is a better way.

    D 1 Reply Last reply
    0
    • R Rick Beideman

      I read a bitmap from file with the following code: Bitmap tempBitmap = new Bitmap(bitmapFileName); Later I want to save an updated version of the bitmap with this code: if (System.IO.File.Exists(bitmapFileName) System.IO.File.Delete(bitmapFileName); tempBitmap.Save(bitmapFileName); Unfortunately, when it gets to the delete, I get the following error: "System.IO.IOException: The process cannot access the file "filename.bmp" because it is being used by another process." The creation of the bitmap from file is holding on to the connection. I tried cloning and copying to the clipboard, but the only way I have been able to get this to work is by copying all my bitmaps to temp files and opening from the temp file. Then the file I delete and save to is a different file. I am hoping there is a better way.

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

      The Bitmap object holds the file open for the lifetime of the object. The trick is to read the file yourself, create the bitmap from that stream, then close the stream.

      FileStream fs = new FileStream(@"C:\bitmap.bmp");
      Bitmap b = new Bitmap(fs);
      fs.Close();

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        The Bitmap object holds the file open for the lifetime of the object. The trick is to read the file yourself, create the bitmap from that stream, then close the stream.

        FileStream fs = new FileStream(@"C:\bitmap.bmp");
        Bitmap b = new Bitmap(fs);
        fs.Close();

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        A Offline
        A Offline
        Alomgir Miah
        wrote on last edited by
        #3

        From MSDN, You must keep the stream open for the lifetime of the Bitmap object. Live Life King Size Alomgir Miah

        R D 2 Replies Last reply
        0
        • A Alomgir Miah

          From MSDN, You must keep the stream open for the lifetime of the Bitmap object. Live Life King Size Alomgir Miah

          R Offline
          R Offline
          Rick Beideman
          wrote on last edited by
          #4

          I tried using the Filestream and closing it after creating the bitmap. It is working. Why must the stream remain open?

          A 1 Reply Last reply
          0
          • R Rick Beideman

            I tried using the Filestream and closing it after creating the bitmap. It is working. Why must the stream remain open?

            A Offline
            A Offline
            Alomgir Miah
            wrote on last edited by
            #5

            If its working thats fine. But in some situations you might get Generic GDI+ error. Check this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassctorTopic6.asp So you should implement IDisposable interface and close the stream when Dispose Method is called. I have written a BitmapHelper, hope this helps, using System; using System.IO; using System.Drawing; namespace Test.Controls { /// /// Summary description for BitmapHelper. /// public class BitmapHelper: IDisposable { private Bitmap _bm; private Icon _ic; private MemoryStream _ms; // Track whether Dispose has been called. private bool disposed = false; public BitmapHelper() { } public void LoadBitmap(Stream sm) { Clear(); int len = (int) sm.Length; byte[] buf = new byte[len]; sm.Read(buf,0,len); _ms = new MemoryStream(buf); _bm = (Bitmap) Image.FromStream(_ms); } public void LoadIcon(Stream sm) { Clear(); int len = (int) sm.Length; byte[] buf = new byte[len]; sm.Read(buf,0,len); _ms = new MemoryStream(buf); _ic = new Icon(_ms); } public void LoadBitmap(string fileName) { FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read); LoadBitmap(fs); fs.Close(); } public void LoadIcon(string fileName) { FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read); LoadIcon(fs); fs.Close(); } public void LoadBitmap(object dbField) { Clear(); if (dbField != DBNull.Value) { byte[] buf = (byte[])dbField; _ms = new MemoryStream(buf); _bm = (Bitmap) Image.FromStream(_ms); } } public void LoadIcon(object dbField) { Clear(); if (dbField != DBNull.Value) { byte[] buf = (byte[])dbField; _ms = new MemoryStream(buf); _ic = new Icon(_ms); } } public void Clear() { if (_bm != null) { _bm.Dispose(); _bm = null; } if (_ic != null) { _ic.Dispose(); _ic = null; } if (_ms != null) { _ms.Close(); _ms = null; } } // Implement IDisposable. // Do not make this method virtual. // A derived class should not be able to

            1 Reply Last reply
            0
            • A Alomgir Miah

              From MSDN, You must keep the stream open for the lifetime of the Bitmap object. Live Life King Size Alomgir Miah

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

              Never have I had a problem with a Generic GDI error while using a Bitmap object. And I've created Bitmap objects from just about every imaginable source, be it a resource stream, external file, serialized network stream, XML data, SQL database, ... Come to think of it, I've never had a GDI+ error that wasn't caused by me doing something stupid, like trying to draw to the same object from two different threads at the same time. If you have to keep the stream open, why does MSDN have an article that shows you how to get around this -> PRB: Image File Is Locked When You Set the PictureBox Image Property to a File[^] The Bitmap class inherits from Image, so I fail to why you would have to keep the stream open for a Bitmap object and not for an Image object. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              R 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Never have I had a problem with a Generic GDI error while using a Bitmap object. And I've created Bitmap objects from just about every imaginable source, be it a resource stream, external file, serialized network stream, XML data, SQL database, ... Come to think of it, I've never had a GDI+ error that wasn't caused by me doing something stupid, like trying to draw to the same object from two different threads at the same time. If you have to keep the stream open, why does MSDN have an article that shows you how to get around this -> PRB: Image File Is Locked When You Set the PictureBox Image Property to a File[^] The Bitmap class inherits from Image, so I fail to why you would have to keep the stream open for a Bitmap object and not for an Image object. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                R Offline
                R Offline
                Rick Beideman
                wrote on last edited by
                #7

                Closing the stream is working for me. Thanks

                A 1 Reply Last reply
                0
                • R Rick Beideman

                  Closing the stream is working for me. Thanks

                  A Offline
                  A Offline
                  Alomgir Miah
                  wrote on last edited by
                  #8

                  Straight from MSDN Remarks You must keep the stream open for the lifetime of the Bitmap object. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassctorTopic6.asp This happens sometimes when the OnPaint tries to access the BitMap and gets a null. Live Life King Size Alomgir Miah

                  D 1 Reply Last reply
                  0
                  • A Alomgir Miah

                    Straight from MSDN Remarks You must keep the stream open for the lifetime of the Bitmap object. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingBitmapClassctorTopic6.asp This happens sometimes when the OnPaint tries to access the BitMap and gets a null. Live Life King Size Alomgir Miah

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

                    So it is... But, like I said, never had a problem with it. I fail to see why any Bitmap object (properly defined) would ever come back as null in the middle of OnPaint. I've never had it happen in any painting of any of my forms or controls. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                    A 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      So it is... But, like I said, never had a problem with it. I fail to see why any Bitmap object (properly defined) would ever come back as null in the middle of OnPaint. I've never had it happen in any painting of any of my forms or controls. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      A Offline
                      A Offline
                      Alomgir Miah
                      wrote on last edited by
                      #10

                      Try adding a toolbar to your application. In each toolbar add a button. Set an ImageList with BitMap streams. If you close the stream see what happens. Live Life King Size Alomgir Miah

                      D 1 Reply Last reply
                      0
                      • A Alomgir Miah

                        Try adding a toolbar to your application. In each toolbar add a button. Set an ImageList with BitMap streams. If you close the stream see what happens. Live Life King Size Alomgir Miah

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

                        How many times do I have to tell you, I've never had a problem with it! I know about that little quirk! That's why I NEVER keep my Toolbar images in files. I keep them in Resources! I hate having open files laying around. I keep as many images as I can in resources. I've even developed an ImageResources resource .DLL that holds my common images, including Toolbar buttons, in various sizes, masks, and transparencies. It's completely static so I don't have to instantiate the thing either. Instead of deploying a bunch of image files along with my apps, I only deploy a sinlge .DLL. Now, all I have to do to set a Toolbar button image is:

                        SaveButton.Image = ImageResources.Buttons.Retrieve("Save 16x16 Trans.bmp")

                        Obviously, Retrieve() returns a Bitmap object. And "Look Ma!," no open file handles! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                        A 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          How many times do I have to tell you, I've never had a problem with it! I know about that little quirk! That's why I NEVER keep my Toolbar images in files. I keep them in Resources! I hate having open files laying around. I keep as many images as I can in resources. I've even developed an ImageResources resource .DLL that holds my common images, including Toolbar buttons, in various sizes, masks, and transparencies. It's completely static so I don't have to instantiate the thing either. Instead of deploying a bunch of image files along with my apps, I only deploy a sinlge .DLL. Now, all I have to do to set a Toolbar button image is:

                          SaveButton.Image = ImageResources.Buttons.Retrieve("Save 16x16 Trans.bmp")

                          Obviously, Retrieve() returns a Bitmap object. And "Look Ma!," no open file handles! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                          A Offline
                          A Offline
                          Alomgir Miah
                          wrote on last edited by
                          #12

                          Calm down man. There is one more way, compile the images as embedded resources into a DLL, this way you have just one DLL. Still you have to open an stream. FYI, when you use resource files to embed images, a stream is opened internally. If you dont trust debug for yourself. SO I am not fighting here, I am just trying to make my point. Live Life King Size Alomgir Miah

                          D 1 Reply Last reply
                          0
                          • A Alomgir Miah

                            Calm down man. There is one more way, compile the images as embedded resources into a DLL, this way you have just one DLL. Still you have to open an stream. FYI, when you use resource files to embed images, a stream is opened internally. If you dont trust debug for yourself. SO I am not fighting here, I am just trying to make my point. Live Life King Size Alomgir Miah

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

                            Alomgir Miah wrote: when you use resource files to embed images, a stream is opened internally. Ya! Why do you think I said "no open file handles"? You just made it sound like there was only one way to create images, using FromFile, and that EVERY Bitmap had to have an open stream associated with it. That's just not true. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                            A 1 Reply Last reply
                            0
                            • D Dave Kreskowiak

                              Alomgir Miah wrote: when you use resource files to embed images, a stream is opened internally. Ya! Why do you think I said "no open file handles"? You just made it sound like there was only one way to create images, using FromFile, and that EVERY Bitmap had to have an open stream associated with it. That's just not true. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                              A Offline
                              A Offline
                              Alomgir Miah
                              wrote on last edited by
                              #14

                              lol u r tough. lets wrap it up. Live Life King Size Alomgir Miah

                              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