Esempio n. 1
0
        private Image GetCropedImage(Image imgSource, int width, int height)
        {
            bool isSizeChanged = false;
            // 計算截圖範圍
            int cropWidth  = imgSource.Width;
            int cropHeight = imgSource.Height;

            if (width < imgSource.Width && height < imgSource.Height)
            {
                if (1.0 * imgSource.Width / width * height <= imgSource.Height)
                {
                    cropHeight    = (int)(1.0 * imgSource.Width / width * height);
                    isSizeChanged = true;
                }
                else if (1.0 * imgSource.Height / height * width <= imgSource.Width)
                {
                    cropWidth     = (int)(1.0 * imgSource.Height / height * width);
                    isSizeChanged = true;
                }
            }
            else if (width < imgSource.Width && height >= imgSource.Height)
            {
                cropWidth     = (int)(1.0 * imgSource.Height / height * width);
                isSizeChanged = true;
            }
            else if (width >= imgSource.Width && height < imgSource.Height)
            {
                cropHeight    = (int)(1.0 * imgSource.Width / width * height);
                isSizeChanged = true;
            }
            else
            {
                // 原圖
            }

            // 進行截圖及縮圖
            if (isSizeChanged)
            {
                var cropedImage = ImageResizer.Crop(imgSource, cropWidth, cropHeight, AnchorPosition.Center);
                return(ImageResizer.ScaleByFixedSize(cropedImage, width, height));
            }


            return(null);
        }
Esempio n. 2
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);
        }