none
Bitmap files. In use by another process. Why? RRS feed

  • Question

  • I display some Bitmap objects in some PictureBox controls. In the course of processing I am done with certain of the .bmp files and want to delete them. I also cannot rename them, move them, or anything.
    I keep the (PictureBox objects in a list, and the Bitmap files to delete in a list.
                foreach (PictureBox pbx in pbxs)
                {
                    try
                    {
                        Bitmap bmp = (Bitmap)pbx.Image;
                        pbx.Image = null;
                        pbxs.Remove(pbx);
                        pbx.Dispose();

                        bmp.Dispose();
                        bmp = null;
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }

                for (int i = ToDelete.Count - 1; i >= 0; i--)
                {
                    string FQFN = ToDelete[i];
                    try
                    {
                        File.Delete(FQFN);
                        ToDelete.Remove(FQFN);
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
    In spite of disposing of the Bitmap objects and the PictureBox objects, trying to delete the .bmp files gives me a "in use by another process" exception.
    When I exit the program, I have no trouble deleting the files from Windows Explorer. I do not create any background threads, etc. in the application.
    What is going on?

    Jon Jacobs

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Tuesday, June 30, 2020 9:51 PM

Answers

  • My thanks to all of you. Your responses and links gave me food for thought. I came up with a simple approach that worked. Most important was Disposing the image in place rather than making a bitmap reference and disposing of it. Effectively that left a reference in place. So the solution was to do pbx.Image.Dispose(). I started with that as the innermost and worked outward, disposing of the PictureBox, then the control containing the PictureBox, etc. until I disposed of the TabPage on which the image displayed. Then I was able to delete the .bmp file.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    • Marked as answer by Jon Q Jacobs Saturday, July 4, 2020 3:35 PM
    Saturday, July 4, 2020 3:34 PM

All replies

  • Hi,

    Thank you for posting here.

    Setting picturebox.image to null will not release this file, which may cause the problem.

    Please take a look at these two threads, someone has provided a way to solve this problem.

    Delete a file being used by another process

    Delete an image being used by another process

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 1, 2020 2:06 AM
  • Probably it is caused by the code that loads or processes the images and assigns to picture boxes. Show some details.


    • Edited by Viorel_MVP Wednesday, July 1, 2020 8:42 AM
    Wednesday, July 1, 2020 8:41 AM
  • Your code in the try catch is redundant. Are you really getting rid of the Picturebox controls by destroying them because you don't need them anymore or are you just trying to get this stuff to clean up. Disposing of the PB will automatically dispose of the image associated with it. If that image is associated with a bitmap then it will be cleaned up as well. Therefore if you're getting rid of the PB then just remove it from the parent control and dispose of it. Everything else works automatically.

    I suspect your problem is in the code that you are using to assign the image to the PB. Please post that code. Also please use the Insert Code Block option in the editor to post code so we can more easily see and copy it.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 1, 2020 1:31 PM
    Moderator
  • Something like this should work

    public static class ImageLoader
    {
        /// <summary>
        /// Load an image into a PictureBox which allows the original image to be
        /// freed, not locked by the application.
        /// </summary>
        /// <param name="path">Path and filename to load into the picturebox</param>
        /// <param name="imageControl">Picturebox control to display the image</param>
        public static void LoadImageClone(string path, PictureBox imageControl)
        {
            Bitmap imageClone = null; 
            var imageOriginal = Image.FromFile(path);
    
            imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height); 
    
            var gr = Graphics.FromImage(imageClone); 
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
            gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height);
            gr.Dispose();
            imageOriginal.Dispose();
    
            imageControl.Image = imageClone; // assign the clone to picture box
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 1, 2020 1:52 PM
    Moderator
  • My thanks to all of you. Your responses and links gave me food for thought. I came up with a simple approach that worked. Most important was Disposing the image in place rather than making a bitmap reference and disposing of it. Effectively that left a reference in place. So the solution was to do pbx.Image.Dispose(). I started with that as the innermost and worked outward, disposing of the PictureBox, then the control containing the PictureBox, etc. until I disposed of the TabPage on which the image displayed. Then I was able to delete the .bmp file.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    • Marked as answer by Jon Q Jacobs Saturday, July 4, 2020 3:35 PM
    Saturday, July 4, 2020 3:34 PM