예제 #1
0
        public virtual async Task <bool> GenerateAsync(string imageUrl, bool isRegenerateAll)
        {
            if (string.IsNullOrEmpty(imageUrl))
            {
                throw new ArgumentNullException("imageUrl");
            }

            var thumbnailsParameters = GetThumbnailParameters();

            if (thumbnailsParameters.IsNullOrEmpty())
            {
                return(false);
            }

            var originalImage = await LoadImageAsync(imageUrl);

            var format = GetImageFormat(originalImage);

            foreach (var parameters in thumbnailsParameters)
            {
                var thumbnailUrl = AddAliasToImageUrl(imageUrl, parameters.Alias);
                if (isRegenerateAll || !Exists(thumbnailUrl))
                {
                    //one process only can use an Image object at the same time.
                    Image clone;
                    lock (_progressLock)
                    {
                        clone = (Image)originalImage.Clone();
                    }

                    //Generate a Thumbnail
                    Image thumbnail = null;
                    switch (parameters.Method)
                    {
                    case ResizeMethod.FixedSize:
                        thumbnail = ImageResizer.FixedSize(clone, parameters.Width, parameters.Height, parameters.Color);
                        break;

                    case ResizeMethod.FixedWidth:
                        thumbnail = ImageResizer.FixedWidth(clone, parameters.Width, parameters.Color);
                        break;

                    case ResizeMethod.FixedHeight:
                        thumbnail = ImageResizer.FixedHeight(clone, parameters.Height, parameters.Color);
                        break;

                    case ResizeMethod.Crop:
                        thumbnail = ImageResizer.Crop(clone, parameters.Width, parameters.Height, parameters.AnchorPosition);
                        break;
                    }

                    //Save
                    if (thumbnail != null)
                    {
                        SaveImage(thumbnailUrl, thumbnail, format);
                    }
                    else
                    {
                        throw new ThumbnailGenetationException(string.Format(CultureInfo.InvariantCulture, "Cannot generate thumbnail for image '{0}'.", thumbnailUrl));
                    }
                }
            }
            return(true);
        }