ImageManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Drawing;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Drawing;
  14. using System.Drawing.Drawing2D;
  15. using System.Drawing.Imaging;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.Controller.Drawing
  20. {
  21. /// <summary>
  22. /// Class ImageManager
  23. /// </summary>
  24. public class ImageManager : IDisposable
  25. {
  26. /// <summary>
  27. /// Gets the image size cache.
  28. /// </summary>
  29. /// <value>The image size cache.</value>
  30. private FileSystemRepository ImageSizeCache { get; set; }
  31. /// <summary>
  32. /// Gets or sets the resized image cache.
  33. /// </summary>
  34. /// <value>The resized image cache.</value>
  35. private FileSystemRepository ResizedImageCache { get; set; }
  36. /// <summary>
  37. /// Gets the cropped image cache.
  38. /// </summary>
  39. /// <value>The cropped image cache.</value>
  40. private FileSystemRepository CroppedImageCache { get; set; }
  41. /// <summary>
  42. /// Gets the cropped image cache.
  43. /// </summary>
  44. /// <value>The cropped image cache.</value>
  45. private FileSystemRepository EnhancedImageCache { get; set; }
  46. /// <summary>
  47. /// The cached imaged sizes
  48. /// </summary>
  49. private readonly ConcurrentDictionary<string, Task<ImageSize>> _cachedImagedSizes = new ConcurrentDictionary<string, Task<ImageSize>>();
  50. /// <summary>
  51. /// The _logger
  52. /// </summary>
  53. private readonly ILogger _logger;
  54. /// <summary>
  55. /// The _protobuf serializer
  56. /// </summary>
  57. private readonly IProtobufSerializer _protobufSerializer;
  58. /// <summary>
  59. /// The _kernel
  60. /// </summary>
  61. private readonly Kernel _kernel;
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="ImageManager" /> class.
  64. /// </summary>
  65. /// <param name="kernel">The kernel.</param>
  66. /// <param name="protobufSerializer">The protobuf serializer.</param>
  67. /// <param name="logger">The logger.</param>
  68. public ImageManager(Kernel kernel, IProtobufSerializer protobufSerializer, ILogger logger, IServerApplicationPaths appPaths)
  69. {
  70. _protobufSerializer = protobufSerializer;
  71. _logger = logger;
  72. _kernel = kernel;
  73. ImageSizeCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "image-sizes"));
  74. ResizedImageCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "resized-images"));
  75. CroppedImageCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "cropped-images"));
  76. EnhancedImageCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "enhanced-images"));
  77. }
  78. /// <summary>
  79. /// Processes an image by resizing to target dimensions
  80. /// </summary>
  81. /// <param name="entity">The entity that owns the image</param>
  82. /// <param name="imageType">The image type</param>
  83. /// <param name="imageIndex">The image index (currently only used with backdrops)</param>
  84. /// <param name="cropWhitespace">if set to <c>true</c> [crop whitespace].</param>
  85. /// <param name="dateModified">The last date modified of the original image file</param>
  86. /// <param name="toStream">The stream to save the new image to</param>
  87. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  88. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  89. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  90. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  91. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  92. /// <returns>Task.</returns>
  93. /// <exception cref="System.ArgumentNullException">entity</exception>
  94. public async Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, bool cropWhitespace, DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
  95. {
  96. if (entity == null)
  97. {
  98. throw new ArgumentNullException("entity");
  99. }
  100. if (toStream == null)
  101. {
  102. throw new ArgumentNullException("toStream");
  103. }
  104. var originalImagePath = GetImagePath(entity, imageType, imageIndex);
  105. if (cropWhitespace)
  106. {
  107. try
  108. {
  109. originalImagePath = GetCroppedImage(originalImagePath, dateModified);
  110. }
  111. catch (Exception ex)
  112. {
  113. // We have to have a catch-all here because some of the .net image methods throw a plain old Exception
  114. _logger.ErrorException("Error cropping image", ex);
  115. }
  116. }
  117. try
  118. {
  119. // Enhance if we have enhancers
  120. var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex).ConfigureAwait(false);
  121. // If the path changed update dateModified
  122. if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
  123. {
  124. dateModified = File.GetLastWriteTimeUtc(ehnancedImagePath);
  125. originalImagePath = ehnancedImagePath;
  126. }
  127. }
  128. catch
  129. {
  130. _logger.Error("Error enhancing image");
  131. }
  132. var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false);
  133. // Determine the output size based on incoming parameters
  134. var newSize = DrawingUtils.Resize(originalImageSize, width, height, maxWidth, maxHeight);
  135. if (!quality.HasValue)
  136. {
  137. quality = 90;
  138. }
  139. var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality.Value, dateModified);
  140. // Grab the cache file if it already exists
  141. try
  142. {
  143. using (var fileStream = new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  144. {
  145. await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
  146. }
  147. return;
  148. }
  149. catch (FileNotFoundException)
  150. {
  151. // Cache file doesn't exist. No biggie.
  152. }
  153. using (var fileStream = File.OpenRead(originalImagePath))
  154. {
  155. using (var originalImage = Bitmap.FromStream(fileStream, true, false))
  156. {
  157. var newWidth = Convert.ToInt32(newSize.Width);
  158. var newHeight = Convert.ToInt32(newSize.Height);
  159. // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
  160. var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat);
  161. // Preserve the original resolution
  162. thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
  163. var thumbnailGraph = Graphics.FromImage(thumbnail);
  164. thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
  165. thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
  166. thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
  167. thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
  168. thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
  169. thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
  170. var outputFormat = originalImage.RawFormat;
  171. using (var memoryStream = new MemoryStream { })
  172. {
  173. // Save to the memory stream
  174. thumbnail.Save(outputFormat, memoryStream, quality.Value);
  175. var bytes = memoryStream.ToArray();
  176. var outputTask = Task.Run(async () => await toStream.WriteAsync(bytes, 0, bytes.Length));
  177. // Save to the cache location
  178. using (var cacheFileStream = new FileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  179. {
  180. // Save to the filestream
  181. await cacheFileStream.WriteAsync(bytes, 0, bytes.Length);
  182. }
  183. await outputTask.ConfigureAwait(false);
  184. }
  185. thumbnailGraph.Dispose();
  186. thumbnail.Dispose();
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Gets the cache file path based on a set of parameters
  192. /// </summary>
  193. /// <param name="originalPath">The path to the original image file</param>
  194. /// <param name="outputSize">The size to output the image in</param>
  195. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  196. /// <param name="dateModified">The last modified date of the image</param>
  197. /// <returns>System.String.</returns>
  198. private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified)
  199. {
  200. var filename = originalPath;
  201. filename += "width=" + outputSize.Width;
  202. filename += "height=" + outputSize.Height;
  203. filename += "quality=" + quality;
  204. filename += "datemodified=" + dateModified.Ticks;
  205. return ResizedImageCache.GetResourcePath(filename, Path.GetExtension(originalPath));
  206. }
  207. /// <summary>
  208. /// Gets image dimensions
  209. /// </summary>
  210. /// <param name="imagePath">The image path.</param>
  211. /// <param name="dateModified">The date modified.</param>
  212. /// <returns>Task{ImageSize}.</returns>
  213. /// <exception cref="System.ArgumentNullException">imagePath</exception>
  214. public Task<ImageSize> GetImageSize(string imagePath, DateTime dateModified)
  215. {
  216. if (string.IsNullOrEmpty(imagePath))
  217. {
  218. throw new ArgumentNullException("imagePath");
  219. }
  220. var name = imagePath + "datemodified=" + dateModified.Ticks;
  221. return _cachedImagedSizes.GetOrAdd(name, keyName => GetImageSizeTask(keyName, imagePath));
  222. }
  223. /// <summary>
  224. /// Gets cached image dimensions, or results null if non-existant
  225. /// </summary>
  226. /// <param name="keyName">Name of the key.</param>
  227. /// <param name="imagePath">The image path.</param>
  228. /// <returns>Task{ImageSize}.</returns>
  229. private Task<ImageSize> GetImageSizeTask(string keyName, string imagePath)
  230. {
  231. return Task.Run(() => GetImageSize(keyName, imagePath));
  232. }
  233. /// <summary>
  234. /// Gets the size of the image.
  235. /// </summary>
  236. /// <param name="keyName">Name of the key.</param>
  237. /// <param name="imagePath">The image path.</param>
  238. /// <returns>ImageSize.</returns>
  239. private ImageSize GetImageSize(string keyName, string imagePath)
  240. {
  241. // Now check the file system cache
  242. var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".pb");
  243. try
  244. {
  245. var result = _protobufSerializer.DeserializeFromFile<int[]>(fullCachePath);
  246. return new ImageSize { Width = result[0], Height = result[1] };
  247. }
  248. catch (FileNotFoundException)
  249. {
  250. // Cache file doesn't exist no biggie
  251. }
  252. var size = ImageHeader.GetDimensions(imagePath, _logger);
  253. var imageSize = new ImageSize { Width = size.Width, Height = size.Height };
  254. // Update the file system cache
  255. CacheImageSize(fullCachePath, size.Width, size.Height);
  256. return imageSize;
  257. }
  258. /// <summary>
  259. /// Caches image dimensions
  260. /// </summary>
  261. /// <param name="cachePath">The cache path.</param>
  262. /// <param name="width">The width.</param>
  263. /// <param name="height">The height.</param>
  264. private void CacheImageSize(string cachePath, int width, int height)
  265. {
  266. var output = new[] { width, height };
  267. _protobufSerializer.SerializeToFile(output, cachePath);
  268. }
  269. /// <summary>
  270. /// Gets the image path.
  271. /// </summary>
  272. /// <param name="item">The item.</param>
  273. /// <param name="imageType">Type of the image.</param>
  274. /// <param name="imageIndex">Index of the image.</param>
  275. /// <returns>System.String.</returns>
  276. /// <exception cref="System.ArgumentNullException">item</exception>
  277. /// <exception cref="System.InvalidOperationException"></exception>
  278. public string GetImagePath(BaseItem item, ImageType imageType, int imageIndex)
  279. {
  280. if (item == null)
  281. {
  282. throw new ArgumentNullException("item");
  283. }
  284. if (imageType == ImageType.Backdrop)
  285. {
  286. if (item.BackdropImagePaths == null)
  287. {
  288. throw new InvalidOperationException(string.Format("Item {0} does not have any Backdrops.", item.Name));
  289. }
  290. return item.BackdropImagePaths[imageIndex];
  291. }
  292. if (imageType == ImageType.Screenshot)
  293. {
  294. if (item.ScreenshotImagePaths == null)
  295. {
  296. throw new InvalidOperationException(string.Format("Item {0} does not have any Screenshots.", item.Name));
  297. }
  298. return item.ScreenshotImagePaths[imageIndex];
  299. }
  300. if (imageType == ImageType.Chapter)
  301. {
  302. var video = (Video)item;
  303. if (video.Chapters == null)
  304. {
  305. throw new InvalidOperationException(string.Format("Item {0} does not have any Chapters.", item.Name));
  306. }
  307. return video.Chapters[imageIndex].ImagePath;
  308. }
  309. return item.GetImage(imageType);
  310. }
  311. /// <summary>
  312. /// Gets the image date modified.
  313. /// </summary>
  314. /// <param name="item">The item.</param>
  315. /// <param name="imageType">Type of the image.</param>
  316. /// <param name="imageIndex">Index of the image.</param>
  317. /// <returns>DateTime.</returns>
  318. /// <exception cref="System.ArgumentNullException">item</exception>
  319. public DateTime GetImageDateModified(BaseItem item, ImageType imageType, int imageIndex)
  320. {
  321. if (item == null)
  322. {
  323. throw new ArgumentNullException("item");
  324. }
  325. var imagePath = GetImagePath(item, imageType, imageIndex);
  326. return GetImageDateModified(item, imagePath);
  327. }
  328. /// <summary>
  329. /// Gets the image date modified.
  330. /// </summary>
  331. /// <param name="item">The item.</param>
  332. /// <param name="imagePath">The image path.</param>
  333. /// <returns>DateTime.</returns>
  334. /// <exception cref="System.ArgumentNullException">item</exception>
  335. public DateTime GetImageDateModified(BaseItem item, string imagePath)
  336. {
  337. if (item == null)
  338. {
  339. throw new ArgumentNullException("item");
  340. }
  341. if (string.IsNullOrEmpty(imagePath))
  342. {
  343. throw new ArgumentNullException("imagePath");
  344. }
  345. var metaFileEntry = item.ResolveArgs.GetMetaFileByPath(imagePath);
  346. // If we didn't the metafile entry, check the Season
  347. if (!metaFileEntry.HasValue)
  348. {
  349. var episode = item as Episode;
  350. if (episode != null && episode.Season != null)
  351. {
  352. episode.Season.ResolveArgs.GetMetaFileByPath(imagePath);
  353. }
  354. }
  355. // See if we can avoid a file system lookup by looking for the file in ResolveArgs
  356. return metaFileEntry == null ? File.GetLastWriteTimeUtc(imagePath) : metaFileEntry.Value.LastWriteTimeUtc;
  357. }
  358. /// <summary>
  359. /// Crops whitespace from an image, caches the result, and returns the cached path
  360. /// </summary>
  361. /// <param name="originalImagePath">The original image path.</param>
  362. /// <param name="dateModified">The date modified.</param>
  363. /// <returns>System.String.</returns>
  364. private string GetCroppedImage(string originalImagePath, DateTime dateModified)
  365. {
  366. var name = originalImagePath;
  367. name += "datemodified=" + dateModified.Ticks;
  368. var croppedImagePath = CroppedImageCache.GetResourcePath(name, Path.GetExtension(originalImagePath));
  369. if (!CroppedImageCache.ContainsFilePath(croppedImagePath))
  370. {
  371. using (var fileStream = File.OpenRead(originalImagePath))
  372. {
  373. using (var originalImage = (Bitmap)Bitmap.FromStream(fileStream, true, false))
  374. {
  375. var outputFormat = originalImage.RawFormat;
  376. using (var croppedImage = originalImage.CropWhitespace())
  377. {
  378. using (var cacheFileStream = new FileStream(croppedImagePath, FileMode.Create))
  379. {
  380. croppedImage.Save(outputFormat, cacheFileStream, 100);
  381. }
  382. }
  383. }
  384. }
  385. }
  386. return croppedImagePath;
  387. }
  388. /// <summary>
  389. /// Runs an image through the image enhancers, caches the result, and returns the cached path
  390. /// </summary>
  391. /// <param name="originalImagePath">The original image path.</param>
  392. /// <param name="dateModified">The date modified of the original image file.</param>
  393. /// <param name="item">The item.</param>
  394. /// <param name="imageType">Type of the image.</param>
  395. /// <param name="imageIndex">Index of the image.</param>
  396. /// <returns>System.String.</returns>
  397. /// <exception cref="System.ArgumentNullException">originalImagePath</exception>
  398. public async Task<string> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex)
  399. {
  400. if (string.IsNullOrEmpty(originalImagePath))
  401. {
  402. throw new ArgumentNullException("originalImagePath");
  403. }
  404. if (item == null)
  405. {
  406. throw new ArgumentNullException("item");
  407. }
  408. var supportedEnhancers = _kernel.ImageEnhancers.Where(i => i.Supports(item, imageType)).ToList();
  409. // No enhancement - don't cache
  410. if (supportedEnhancers.Count == 0)
  411. {
  412. return originalImagePath;
  413. }
  414. var cacheGuid = GetImageCacheTag(originalImagePath, dateModified, supportedEnhancers, item, imageType);
  415. // All enhanced images are saved as png to allow transparency
  416. var enhancedImagePath = EnhancedImageCache.GetResourcePath(cacheGuid + ".png");
  417. if (!EnhancedImageCache.ContainsFilePath(enhancedImagePath))
  418. {
  419. using (var fileStream = File.OpenRead(originalImagePath))
  420. {
  421. using (var originalImage = Image.FromStream(fileStream, true, false))
  422. {
  423. //Pass the image through registered enhancers
  424. using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
  425. {
  426. //And then save it in the cache
  427. newImage.Save(enhancedImagePath, ImageFormat.Png);
  428. }
  429. }
  430. }
  431. }
  432. return enhancedImagePath;
  433. }
  434. /// <summary>
  435. /// Gets the image cache tag.
  436. /// </summary>
  437. /// <param name="item">The item.</param>
  438. /// <param name="imageType">Type of the image.</param>
  439. /// <param name="imagePath">The image path.</param>
  440. /// <returns>Guid.</returns>
  441. /// <exception cref="System.ArgumentNullException">item</exception>
  442. public Guid GetImageCacheTag(BaseItem item, ImageType imageType, string imagePath)
  443. {
  444. if (item == null)
  445. {
  446. throw new ArgumentNullException("item");
  447. }
  448. if (string.IsNullOrEmpty(imagePath))
  449. {
  450. throw new ArgumentNullException("imagePath");
  451. }
  452. var dateModified = GetImageDateModified(item, imagePath);
  453. var supportedEnhancers = _kernel.ImageEnhancers.Where(i => i.Supports(item, imageType));
  454. return GetImageCacheTag(imagePath, dateModified, supportedEnhancers, item, imageType);
  455. }
  456. /// <summary>
  457. /// Gets the image cache tag.
  458. /// </summary>
  459. /// <param name="originalImagePath">The original image path.</param>
  460. /// <param name="dateModified">The date modified of the original image file.</param>
  461. /// <param name="imageEnhancers">The image enhancers.</param>
  462. /// <param name="item">The item.</param>
  463. /// <param name="imageType">Type of the image.</param>
  464. /// <returns>Guid.</returns>
  465. /// <exception cref="System.ArgumentNullException">item</exception>
  466. public Guid GetImageCacheTag(string originalImagePath, DateTime dateModified, IEnumerable<IImageEnhancer> imageEnhancers, BaseItem item, ImageType imageType)
  467. {
  468. if (item == null)
  469. {
  470. throw new ArgumentNullException("item");
  471. }
  472. if (imageEnhancers == null)
  473. {
  474. throw new ArgumentNullException("imageEnhancers");
  475. }
  476. if (string.IsNullOrEmpty(originalImagePath))
  477. {
  478. throw new ArgumentNullException("originalImagePath");
  479. }
  480. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  481. var cacheKeys = imageEnhancers.Select(i => i.GetType().Name + i.LastConfigurationChange(item, imageType).Ticks).ToList();
  482. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  483. return string.Join("|", cacheKeys.ToArray()).GetMD5();
  484. }
  485. /// <summary>
  486. /// Executes the image enhancers.
  487. /// </summary>
  488. /// <param name="imageEnhancers">The image enhancers.</param>
  489. /// <param name="originalImage">The original image.</param>
  490. /// <param name="item">The item.</param>
  491. /// <param name="imageType">Type of the image.</param>
  492. /// <param name="imageIndex">Index of the image.</param>
  493. /// <returns>Task{EnhancedImage}.</returns>
  494. private async Task<Image> ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, Image originalImage, BaseItem item, ImageType imageType, int imageIndex)
  495. {
  496. var result = originalImage;
  497. // Run the enhancers sequentially in order of priority
  498. foreach (var enhancer in imageEnhancers)
  499. {
  500. var typeName = enhancer.GetType().Name;
  501. _logger.Debug("Running {0} for {1}", typeName, item.Path ?? item.Name ?? "--Unknown--");
  502. try
  503. {
  504. result = await enhancer.EnhanceImageAsync(item, result, imageType, imageIndex).ConfigureAwait(false);
  505. }
  506. catch (Exception ex)
  507. {
  508. _logger.ErrorException("{0} failed enhancing {1}", ex, typeName, item.Name);
  509. throw;
  510. }
  511. }
  512. return result;
  513. }
  514. public void Dispose()
  515. {
  516. Dispose(true);
  517. }
  518. /// <summary>
  519. /// Releases unmanaged and - optionally - managed resources.
  520. /// </summary>
  521. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  522. protected void Dispose(bool dispose)
  523. {
  524. if (dispose)
  525. {
  526. ImageSizeCache.Dispose();
  527. ResizedImageCache.Dispose();
  528. CroppedImageCache.Dispose();
  529. EnhancedImageCache.Dispose();
  530. }
  531. }
  532. }
  533. }