Bitmap(file) locks the file.#4994
Merged
ganik merged 2 commits intodotnet:masterfrom Apr 2, 2020
ganik:ganik/bitmap
Merged
Conversation
… of Bitmap. Use workaround suggested https://stackoverflow.com/questions/6576341/open-image-from-file-then-release-lock
harishsk
approved these changes
Apr 1, 2020
harishsk
reviewed
Apr 1, 2020
| // to avoid locking file, use the construct below to load bitmap | ||
| using(var bmpTemp = new Bitmap(path)) | ||
| dst = new Bitmap(bmpTemp) { Tag = path }; | ||
| var bytes = File.ReadAllBytes(path); |
Contributor
There was a problem hiding this comment.
This is interesting. Can you please help me understand why the change was necessary?
Member
Author
There was a problem hiding this comment.
The first change was not preserving the original pixel encoding (format) from the file. It looks like the copy constructor was modifying the format. The second iteration preserves original encoding by moving all the bytes into the byte stream, so only underlying media is changed (memory vs file). See https://stackoverflow.com/questions/4803935/free-file-locked-by-new-bitmapfilepath for more details
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4585
Construct "new Bitmap(file)" locks the underlying file for the lifetime of Bitmap. Use workaround suggested https://stackoverflow.com/questions/6576341/open-image-from-file-then-release-lock