none
After opening an image Image.FromFile( fileName ); I can't delete it. RRS feed

  • Question

  • Hi all. I have an app maintains a list of images from a directory.  For each name, I need to open that image, get the bytes and send the bytes back to the caller, but... I also need to delete the image once I've processed it.  

    I have this function here pulls a file name from a list, that opens the image from the hard drive, gets the bytes into a byte array and tries to then delete the file from the disk.  However, I keep getting an error

    System.UnauthorizedAccessException
      HResult=0x80070005
      Message=Access to the path 'K:\TestImages\5.png' is denied.


            public byte[] PullNextImage( )
            {
                string nextFile = DequeueNextImageName( );
                if( nextFile == "" )
                    return null;
    
                _currentImage   = Image.FromFile( nextFile );
                byte[] next     = ImageToByteArray( );
    
                _currentImage.Dispose( );
                _currentImage = null;
                
                if( next != null && next.Length > 0 )
                    File.Delete( nextFile );
    
                return next;
            }

    So, I've pulled the bytes out of the image, I've disposed it, set it to null but I still get this error, what else am I supposed to do to do?

    I know for a fact it's not a general file permissions issue with this file because if run this exact same function but I take out all the image processing, it will delete the file just fine.  

            public byte[ ] PullNextImage( )
            {
                string nextFile = DequeueNextImageName( );
                if( nextFile == "" )
                    return null;
    
                File.Delete( nextFile );
    
                return null;
            }


    Rick


    • Edited by m00n Friday, March 8, 2019 9:31 PM
    Friday, March 8, 2019 9:30 PM

All replies

  • Hello,

    You could clone the image which once the image is read in the object is disposed.

    public Bitmap ClonedImage(string pPath)
    {
        Bitmap imageClone = null;
        Image imageOriginal = Image.FromFile(pPath);
    
        imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height);
    
        Graphics 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();
    
        return imageClone;
    }


    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

    Friday, March 8, 2019 9:48 PM
    Moderator
  • Hi Karen... I think I have a working solution after doing some digging.... 

    public byte[] PullNextImage( ) { string nextFile = DequeueNextImageName( ); if( nextFile == "" ) return null; byte[] next = null; using( FileStream f = new FileStream( nextFile, FileMode.Open, FileAccess.Read ) ) { _currentImage = Image.FromStream( f );

    // Get the byte[] array from _currentImage next = ImageToByteArray( );

    f.Close( ); _currentImage.Dispose( ); _currentImage = null; File.Delete( nextFile ); } return next; }


    Rick




    Friday, March 8, 2019 10:14 PM
  • Hi, moon

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    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.


    Monday, March 11, 2019 1:58 AM
    Moderator