ImageManager.cs 25 KB

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