Skip to content

Bitmap(file) locks the file. #4994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2020
Merged

Bitmap(file) locks the file. #4994

merged 2 commits into from
Apr 2, 2020

Conversation

ganik
Copy link
Member

@ganik ganik commented Apr 1, 2020

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

@ganik ganik requested a review from a team as a code owner April 1, 2020 20:19
@ganik ganik requested a review from harishsk April 1, 2020 20:20
@@ -238,8 +238,10 @@ private Delegate MakeGetterImageDataViewType(DataViewRow input, int iinfo, Func<
path = Path.Combine(_parent.ImageFolder, path);

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting. Can you please help me understand why the change was necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ganik ganik merged commit 77e7f98 into dotnet:master Apr 2, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Mar 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot delete image file passsed to PredictionEngine for Image Classification after disposing the engine
2 participants