You used the Image.FromFile
method to create an Image
object representing the image contained in that file
Image i= Image.FromFile(fileName);
That's why you cannot delete the file: because your process is still using it! You won't be able to delete the file on disk until the Image
object you created from it is disposed, freeing the lock on the file.
The documentation for the FromFile
method confirms this:
The file remains locked until the Image is disposed.
To ensure that an object is disposed, wrap its creation and use inside of a using
block.