示例#1
0
        public string SaveProductImage(Image image, string fileName, Product product, IImageFormat imageFormat)
        {
            var pathFileStorageDirectory = AppSettingConfigurationHelper.GetSection("PathFileStorageDirectory").Value;

            if (pathFileStorageDirectory == null || pathFileStorageDirectory.Equals(string.Empty))
            {
                throw new Exception("File configuration must have App Setting with key PathFileStorageDirectory");
            }

            var pathDirectoryProduct = Path.Combine(pathFileStorageDirectory, "Images", "Products", product.Id.ToString());

            var directoryProductInfo = new DirectoryInfo(pathDirectoryProduct);

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

            var resizedImage = ImageResizer.FixedSize(image, 600, 600);
            var filePath     = Path.Combine(directoryProductInfo.FullName, string.Format("{0}{1}", fileName, imageFormat.GetFileExtension()));

            var encoder = imageFormat.GetEncoder();

            resizedImage.Save(filePath, encoder);
            resizedImage.Dispose();
            return(Path.Combine("Images", "Products", product.Id.ToString(), fileName + imageFormat.GetFileExtension()));
        }
示例#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);
        }