Skip to content

Commit 77e7f98

Browse files
authored
Bitmap(file) locks the file. (#4994)
* 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>
1 parent 54223a1 commit 77e7f98

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Microsoft.ML.ImageAnalytics/ImageLoader.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ private Delegate MakeGetterImageDataViewType(DataViewRow input, int iinfo, Func<
237237
if (!string.IsNullOrWhiteSpace(_parent.ImageFolder))
238238
path = Path.Combine(_parent.ImageFolder, path);
239239

240-
dst = new Bitmap(path) { Tag = path };
240+
// to avoid locking file, use the construct below to load bitmap
241+
var bytes = File.ReadAllBytes(path);
242+
var ms = new MemoryStream(bytes);
243+
dst = (Bitmap)Image.FromStream(ms);
244+
dst.Tag = path;
241245

242246
// Check for an incorrect pixel format which indicates the loading failed
243247
if (dst.PixelFormat == System.Drawing.Imaging.PixelFormat.DontCare)

0 commit comments

Comments
 (0)