예제 #1
0
        public void AddImagesToProduct(IEnumerable <HttpPostedFileBase> files, Product product)
        {
            if (files.Any(x => x != null))
            {
                var resizedImages = _productImageService.ResizeImages(files, 1920, 1080);
                var cropedImages  = _productImageService.CropImage(files, 300, 200);

                for (int i = 0; i < files.Count(); i++)
                {
                    if (product.Images.Count <= 5)
                    {
                        Guid id = Guid.NewGuid();

                        var productImage = new ProductImage
                        {
                            Id            = id,
                            ImageName     = product.Name + "Image" + id + ".png",
                            ThumbnailName = product.Name + "Thumbnail" + id + ".png",
                        };

                        product.Images.Add(productImage);

                        using (resizedImages[i])
                            using (cropedImages[i])
                            {
                                resizedImages[i].Save(HostingEnvironment.MapPath("~/Images/") + product.Name + "Image" + id + ".png");
                                cropedImages[i].Save(HostingEnvironment.MapPath("~/Images/") + product.Name + "Thumbnail" + id + ".png");
                            }
                    }
                }
            }
        }