New answer:
I looked at the line where you are doing OpenRead (). Obviously, this locks your file. It would be better to specify the path to the file instead of the stream, because you cannot control the stream, because the bitmap will become erroneous.
Another thing I'm looking for in your code that might be bad practice is binding to FileInfo. Itβs better to create an object of an object / data transmission object and bind it to a collection of this type - some object that has the properties that you need to show in your control. This will help to avoid file locks.
On the other hand, you can do some trick: why don't you show compressed images with a screen resolution that compresses them, so the image size will be extremely lower than the actual ones, and you provide the "Show in HQ" button? This should solve the problem of preloading HD images. When the user clicks the "Show in HQ" button, loads this image into memory, and when it is closed, it becomes deleted.
Is it OK for you?
If I am not mistaken, FileInfo does not block any file. You do not open it, but read its metadata.
On the other hand, if the application displays images, you must transfer them to visible ones in memory and load them into your form from the memory stream.
This is reasonable because you can open the file stream, read its bytes and transfer them to the memory stream, leaving the file locked.
NOTE. This solution is great for not-so-large images ... Let me know if you work with HD images.
source share