diff --git a/MediaBrowser.Controller/Drawing/ImageHeader.cs b/MediaBrowser.Controller/Drawing/ImageHeader.cs
index 91b4b93e9b..bca897a4cd 100644
--- a/MediaBrowser.Controller/Drawing/ImageHeader.cs
+++ b/MediaBrowser.Controller/Drawing/ImageHeader.cs
@@ -1,4 +1,6 @@
-using MediaBrowser.Model.Logging;
+using System.Threading.Tasks;
+using MediaBrowser.Common.IO;
+using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
@@ -39,7 +41,7 @@ namespace MediaBrowser.Controller.Drawing
/// The logger.
/// The dimensions of the specified image.
/// The image was of an unrecognised format.
- public static Size GetDimensions(string path, ILogger logger)
+ public static async Task GetDimensions(string path, ILogger logger)
{
try
{
@@ -54,11 +56,17 @@ namespace MediaBrowser.Controller.Drawing
catch
{
logger.Info("Failed to read image header for {0}. Doing it the slow way.", path);
+ }
- using (var fs = File.OpenRead(path))
+ // Buffer to memory stream to avoid image locking file
+ using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
+ {
+ using (var memoryStream = new MemoryStream())
{
+ await fs.CopyToAsync(memoryStream).ConfigureAwait(false);
+
// Co it the old fashioned way
- using (var b = Image.FromStream(fs, true, false))
+ using (var b = Image.FromStream(memoryStream, true, false))
{
return b.Size;
}
diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs
index 3bcd82d88b..2d2bd5ae8a 100644
--- a/MediaBrowser.Controller/Drawing/ImageManager.cs
+++ b/MediaBrowser.Controller/Drawing/ImageManager.cs
@@ -172,48 +172,54 @@ namespace MediaBrowser.Controller.Drawing
try
{
- using (var fileStream = File.OpenRead(originalImagePath))
+ using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
- using (var originalImage = Image.FromStream(fileStream, true, false))
+ // Copy to memory stream to avoid Image locking file
+ using (var memoryStream = new MemoryStream())
{
- var newWidth = Convert.ToInt32(newSize.Width);
- var newHeight = Convert.ToInt32(newSize.Height);
+ await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
- // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
- var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat);
+ using (var originalImage = Image.FromStream(fileStream, true, false))
+ {
+ var newWidth = Convert.ToInt32(newSize.Width);
+ var newHeight = Convert.ToInt32(newSize.Height);
- // Preserve the original resolution
- thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
+ // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
+ using (var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat))
+ {
+ // Preserve the original resolution
+ thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
- var thumbnailGraph = Graphics.FromImage(thumbnail);
+ using (var thumbnailGraph = Graphics.FromImage(thumbnail))
+ {
+ thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
+ thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
+ thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
+ thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
+ thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
- thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
- thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
- thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
- thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
- thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
+ thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
- thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
+ var outputFormat = originalImage.RawFormat;
- var outputFormat = originalImage.RawFormat;
+ using (var outputMemoryStream = new MemoryStream())
+ {
+ // Save to the memory stream
+ thumbnail.Save(outputFormat, outputMemoryStream, quality.Value);
- using (var memoryStream = new MemoryStream())
- {
- // Save to the memory stream
- thumbnail.Save(outputFormat, memoryStream, quality.Value);
+ var bytes = outputMemoryStream.ToArray();
- var bytes = memoryStream.ToArray();
+ var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
- var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
+ // kick off a task to cache the result
+ await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
- // kick off a task to cache the result
- await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
+ await outputTask.ConfigureAwait(false);
+ }
+ }
+ }
- await outputTask.ConfigureAwait(false);
}
-
- thumbnailGraph.Dispose();
- thumbnail.Dispose();
}
}
}
@@ -320,9 +326,7 @@ namespace MediaBrowser.Controller.Drawing
// Cache file doesn't exist no biggie
}
- _logger.Debug("Getting image size for {0}", imagePath);
-
- var size = ImageHeader.GetDimensions(imagePath, _logger);
+ var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false);
// Update the file system cache
File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture));
@@ -468,17 +472,23 @@ namespace MediaBrowser.Controller.Drawing
try
{
- using (var fileStream = File.OpenRead(originalImagePath))
+ using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
- using (var originalImage = (Bitmap)Image.FromStream(fileStream, true, false))
+ // Copy to memory stream to avoid Image locking file
+ using (var memoryStream = new MemoryStream())
{
- var outputFormat = originalImage.RawFormat;
+ await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
- using (var croppedImage = originalImage.CropWhitespace())
+ using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
{
- using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
+ var outputFormat = originalImage.RawFormat;
+
+ using (var croppedImage = originalImage.CropWhitespace())
{
- croppedImage.Save(outputFormat, outputStream, 100);
+ using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
+ {
+ croppedImage.Save(outputFormat, outputStream, 100);
+ }
}
}
}
@@ -547,17 +557,23 @@ namespace MediaBrowser.Controller.Drawing
try
{
- using (var fileStream = File.OpenRead(originalImagePath))
+ using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
- using (var originalImage = Image.FromStream(fileStream, true, false))
+ // Copy to memory stream to avoid Image locking file
+ using (var memoryStream = new MemoryStream())
{
- //Pass the image through registered enhancers
- using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
+ await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
+
+ using (var originalImage = Image.FromStream(memoryStream, true, false))
{
- //And then save it in the cache
- using (var outputStream = new FileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
+ //Pass the image through registered enhancers
+ using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
{
- newImage.Save(ImageFormat.Png, outputStream, 100);
+ //And then save it in the cache
+ using (var outputStream = new FileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
+ {
+ newImage.Save(ImageFormat.Png, outputStream, 100);
+ }
}
}
}
@@ -650,8 +666,6 @@ namespace MediaBrowser.Controller.Drawing
{
var typeName = enhancer.GetType().Name;
- _logger.Debug("Running {0} for {1}", typeName, item.Path ?? item.Name ?? "--Unknown--");
-
try
{
result = await enhancer.EnhanceImageAsync(item, result, imageType, imageIndex).ConfigureAwait(false);