Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
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

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