Skip to content

Commit

Permalink
Bitmap(file) locks the file. (#4994)
Browse files Browse the repository at this point in the history
* 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

* Maintain original pixel format from file.

Co-authored-by: Gani Nazirov <ganaziro@microsoft.com>
  • Loading branch information
ganik and ganik authored Apr 2, 2020
1 parent 54223a1 commit 77e7f98
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Microsoft.ML.ImageAnalytics/ImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ private Delegate MakeGetterImageDataViewType(DataViewRow input, int iinfo, Func<
if (!string.IsNullOrWhiteSpace(_parent.ImageFolder))
path = Path.Combine(_parent.ImageFolder, path);
dst = new Bitmap(path) { Tag = path };
// to avoid locking file, use the construct below to load bitmap
var bytes = File.ReadAllBytes(path);
var ms = new MemoryStream(bytes);
dst = (Bitmap)Image.FromStream(ms);
dst.Tag = path;
// Check for an incorrect pixel format which indicates the loading failed
if (dst.PixelFormat == System.Drawing.Imaging.PixelFormat.DontCare)
Expand Down

0 comments on commit 77e7f98

Please sign in to comment.