/// <summary> /// Gets the resized image. /// </summary> /// <param name="httpServerUtility">An instance of <see cref="System.Web.HttpServerUtility" />.</param> /// <param name="file">The media file.</param> /// <param name="resizingOptions">The resizing options.</param> /// <param name="targetImageFormat">The target image format.</param> /// <returns>A full file path to a resized image; null if there's no need to resize the image</returns> public static string GetResizedImage(HttpServerUtility httpServerUtility, IMediaFile file, ResizingOptions resizingOptions, ImageFormat targetImageFormat) { Verify.ArgumentNotNull(file, "file"); Verify.That(ImageFormatIsSupported(targetImageFormat), "Unsupported image format '{0}'", targetImageFormat); if (_resizedImagesDirectoryPath == null) { _resizedImagesDirectoryPath = httpServerUtility.MapPath(ResizedImagesCacheDirectory); if (!C1Directory.Exists(_resizedImagesDirectoryPath)) { C1Directory.CreateDirectory(_resizedImagesDirectoryPath); } } string imageKey = file.CompositePath; bool isNativeProvider = file is FileSystemFileBase; string imageSizeCacheKey = "ShowMedia.ashx image size " + imageKey; Size? imageSize = HttpRuntime.Cache.Get(imageSizeCacheKey) as Size?; Bitmap bitmap = null; Stream fileStream = null; try { if (imageSize == null) { fileStream = file.GetReadStream(); Size calculatedSize; if (!ImageSizeReader.TryGetSize(fileStream, out calculatedSize)) { fileStream.Dispose(); fileStream = file.GetReadStream(); bitmap = new Bitmap(fileStream); calculatedSize = new Size { Width = bitmap.Width, Height = bitmap.Height }; } imageSize = calculatedSize; // We can provider cache dependency only for the native media provider var cacheDependency = isNativeProvider ? new CacheDependency((file as FileSystemFileBase).SystemPath) : null; HttpRuntime.Cache.Add(imageSizeCacheKey, imageSize, cacheDependency, DateTime.MaxValue, CacheExpirationTimeSpan, CacheItemPriority.Normal, null); } int newWidth, newHeight; bool centerCrop; bool needToResize = CalculateSize(imageSize.Value.Width, imageSize.Value.Height, resizingOptions, out newWidth, out newHeight, out centerCrop); needToResize = needToResize || resizingOptions.CustomQuality; if (!needToResize) { return(null); } int filePathHash = imageKey.GetHashCode(); string centerCroppedString = centerCrop ? "c" : string.Empty; string fileExtension = _ImageFormat2Extension[targetImageFormat]; string resizedImageFileName = string.Format("{0}x{1}_{2}{3}_{4}.{5}", newWidth, newHeight, filePathHash, centerCroppedString, resizingOptions.Quality, fileExtension); string imageFullPath = Path.Combine(_resizedImagesDirectoryPath, resizedImageFileName); if (!C1File.Exists(imageFullPath) || C1File.GetLastWriteTime(imageFullPath) != file.LastWriteTime) { if (bitmap == null) { fileStream = file.GetReadStream(); bitmap = new Bitmap(fileStream); } ResizeImage(bitmap, imageFullPath, newWidth, newHeight, centerCrop, targetImageFormat, resizingOptions.Quality); if (file.LastWriteTime.HasValue) { C1File.SetLastWriteTime(imageFullPath, file.LastWriteTime.Value); } } return(imageFullPath); } finally { if (bitmap != null) { bitmap.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } } }