Пример #1
0
        /// <summary>
        /// Crops whitespace from an image, caches the result, and returns the cached path
        /// </summary>
        /// <param name="originalImagePath">The original image path.</param>
        /// <param name="dateModified">The date modified.</param>
        /// <returns>System.String.</returns>
        private async Task <string> GetCroppedImage(string originalImagePath, DateTime dateModified)
        {
            var name = originalImagePath;

            name += "datemodified=" + dateModified.Ticks;

            var croppedImagePath = CroppedImageCache.GetResourcePath(name, Path.GetExtension(originalImagePath));

            if (!CroppedImageCache.ContainsFilePath(croppedImagePath))
            {
                using (var fileStream = File.OpenRead(originalImagePath))
                {
                    using (var originalImage = (Bitmap)Bitmap.FromStream(fileStream, true, false))
                    {
                        var outputFormat = originalImage.RawFormat;

                        using (var croppedImage = originalImage.CropWhitespace())
                        {
                            await SaveImageToFile(croppedImage, outputFormat, croppedImagePath).ConfigureAwait(false);
                        }
                    }
                }
            }

            return(croppedImagePath);
        }
Пример #2
0
        /// <summary>
        /// Crops whitespace from an image, caches the result, and returns the cached path
        /// </summary>
        /// <param name="originalImagePath">The original image path.</param>
        /// <param name="dateModified">The date modified.</param>
        /// <returns>System.String.</returns>
        private async Task <string> GetCroppedImage(string originalImagePath, DateTime dateModified)
        {
            var name = originalImagePath;

            name += "datemodified=" + dateModified.Ticks;

            var croppedImagePath = CroppedImageCache.GetResourcePath(name, Path.GetExtension(originalImagePath));

            var semaphore = GetLock(croppedImagePath);

            await semaphore.WaitAsync().ConfigureAwait(false);

            // Check again in case of contention
            if (File.Exists(croppedImagePath))
            {
                semaphore.Release();
                return(croppedImagePath);
            }

            try
            {
                using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
                {
                    // Copy to memory stream to avoid Image locking file
                    using (var memoryStream = new MemoryStream())
                    {
                        await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);

                        using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
                        {
                            var outputFormat = originalImage.RawFormat;

                            using (var croppedImage = originalImage.CropWhitespace())
                            {
                                var parentPath = Path.GetDirectoryName(croppedImagePath);

                                if (!Directory.Exists(parentPath))
                                {
                                    Directory.CreateDirectory(parentPath);
                                }

                                using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                                {
                                    croppedImage.Save(outputFormat, outputStream, 100);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // We have to have a catch-all here because some of the .net image methods throw a plain old Exception
                _logger.ErrorException("Error cropping image {0}", ex, originalImagePath);

                return(originalImagePath);
            }
            finally
            {
                semaphore.Release();
            }

            return(croppedImagePath);
        }