Exemple #1
0
        public string SaveImage(HttpPostedFileBase imageFile)
        {
            string fileName      = Guid.NewGuid().ToString() + ".jpg";
            string fullPathImage = Server.MapPath(ImageConfig.ProductImagePath) + "\\" + fileName;

            using (Bitmap bmp = new Bitmap(imageFile.InputStream))
            {
                var readyImage = Image_Helper.CreateImage(bmp, 450, 450);
                if (readyImage != null)
                {
                    readyImage.Save(fullPathImage, ImageFormat.Jpeg);
                    return(fileName);
                }
            }
            return("no image");
        }
        public ActionResult Create(CreateProductVewModel model, HttpPostedFileBase imageFile)
        {
            string fileName      = Guid.NewGuid().ToString() + ".jpg";
            string fullPathImage = Server.MapPath(ImageConfig.ProductImagePath) + "\\" + fileName;

            using (Bitmap bmp = new Bitmap(imageFile.InputStream))
            {
                var readyImage = Image_Helper.CreateImage(bmp, 450, 450);
                if (readyImage != null)
                {
                    readyImage.Save(fullPathImage, ImageFormat.Jpeg);
                    Product newProduct = new Product
                    {
                        ImageName = fileName,
                        Name      = model.Name,
                        Price     = model.Price
                    };
                    _context.products.Add(newProduct);
                    _context.SaveChanges();
                }
            }
            return(RedirectToAction("Index", "Home"));
        }