Пример #1
0
        /// <summary>
        /// Generates thumbnails asynchronously
        /// </summary>
        /// <param name="sourcePath">Path to source picture</param>
        /// <param name="destPath">Target for generated thumbnail</param>
        /// <param name="options">Represents generation options</param>
        /// <param name="token">Allows cancel operation</param>
        /// <returns></returns>
        public virtual async Task <ThumbnailGenerationResult> GenerateThumbnailsAsync(string sourcePath, string destPath, IList <ThumbnailOption> options, ICancellationToken token)
        {
            token?.ThrowIfCancellationRequested();

            var originalImage = await _imageService.LoadImageAsync(sourcePath, out var format);

            if (originalImage == null)
            {
                return(new ThumbnailGenerationResult
                {
                    Errors = { $"Cannot generate thumbnail: {sourcePath} does not have a valid image format" }
                });
            }

            var result = new ThumbnailGenerationResult();

            foreach (var option in options)
            {
                var thumbnail    = GenerateThumbnail(originalImage, option);
                var thumbnailUrl = sourcePath.GenerateThumbnailName(option.FileSuffix);

                if (thumbnail != null)
                {
                    await _imageService.SaveImageAsync(thumbnailUrl, thumbnail, format, option.JpegQuality);
                }
                else
                {
                    throw new Exception($"Cannot save thumbnail image {thumbnailUrl}");
                }

                result.GeneratedThumbnails.Add(thumbnailUrl);
            }

            return(result);
        }
        /// <summary>
        /// Generates thumbnails asynchronously
        /// </summary>
        /// <param name="sourcePath">Path to source picture</param>
        /// <param name="destPath">Target for generated thumbnail</param>
        /// <param name="options">Represents generation options</param>
        /// <param name="token">Allows cancel operation</param>
        /// <returns></returns>
        public async Task <ThumbnailGenerationResult> GenerateThumbnailsAsync(string sourcePath, string destPath, IList <ThumbnailOption> options, ICancellationToken token)
        {
            token?.ThrowIfCancellationRequested();

            var originalImage = await _imageService.LoadImageAsync(sourcePath);

            if (originalImage == null)
            {
                return(new ThumbnailGenerationResult
                {
                    Errors = { $"Cannot generate thumbnail: {sourcePath} does not have a valid image format" }
                });
            }

            var result = new ThumbnailGenerationResult();

            var format = _imageService.GetImageFormat(originalImage);

            //one process only can use an Image object at the same time.
            Image clone;

            lock (_progressLock)
            {
                clone = (Image)originalImage.Clone();
            }

            foreach (var option in options)
            {
                var thumbnail    = GenerateThumbnail(clone, option);
                var thumbnailUrl = sourcePath.GenerateThumbnailName(option.FileSuffix);

                if (thumbnail != null)
                {
                    await _imageService.SaveImage(thumbnailUrl, thumbnail, format);
                }
                else
                {
                    throw new Exception($"Cannot save thumbnail image {thumbnailUrl}");
                }

                result.GeneratedThumbnails.Add(thumbnailUrl);
            }

            return(result);
        }