From 849d2740cf235b0882820235e8e1c0ab845995a8 Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Wed, 1 Apr 2020 13:17:59 -0700 Subject: [PATCH 1/2] 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 --- src/Microsoft.ML.ImageAnalytics/ImageLoader.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs b/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs index 07ecbb37be..5d02a3cab8 100644 --- a/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs +++ b/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs @@ -237,7 +237,9 @@ 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 + using(var bmpTemp = new Bitmap(path)) + dst = new Bitmap(bmpTemp) { Tag = path }; // Check for an incorrect pixel format which indicates the loading failed if (dst.PixelFormat == System.Drawing.Imaging.PixelFormat.DontCare) From 45aeb6c40ad8f89c4de22c07cdf9cda3f26a5d1e Mon Sep 17 00:00:00 2001 From: Gani Nazirov Date: Wed, 1 Apr 2020 15:44:28 -0700 Subject: [PATCH 2/2] Maintain original pixel format from file. --- src/Microsoft.ML.ImageAnalytics/ImageLoader.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs b/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs index 5d02a3cab8..a8120cd524 100644 --- a/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs +++ b/src/Microsoft.ML.ImageAnalytics/ImageLoader.cs @@ -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); + 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)