public ActionResult Update(Game game, HttpPostedFileBase picture, int categoryId) { try { GameDao gameDao = new GameDao(); GameCategoryDao gameCategoryDao = new GameCategoryDao(); GameCategory gameCategory = gameCategoryDao.GetById(categoryId); game.Category = gameCategory; if (picture != null) { if (picture.ContentType == "image/jpeg" || picture.ContentType == "image/png") { Image image = Image.FromStream(picture.InputStream); Guid guid = Guid.NewGuid(); string imageName = guid.ToString() + ".jpg"; if (image.Height > 200 || image.Width > 200) { Image smallImage = Class.ImageHelper.ScaleImage(image, 200, 200); Bitmap b = new Bitmap(smallImage); b.Save(Server.MapPath("~/uploads/game/" + imageName), ImageFormat.Jpeg); smallImage.Dispose(); b.Dispose(); } else { picture.SaveAs(Server.MapPath("~/uploads/game/" + picture.FileName)); } game.ImageName = imageName; } } gameDao.Update(game); TempData["message-success"] = "Hra" + game.Name + "byla upravena"; } catch (Exception) { throw; } return(RedirectToAction("Index", "Games")); }
public ActionResult Add(Game game, HttpPostedFileBase picture, int categoryId) { if (ModelState.IsValid) { if (picture != null) { if (picture.ContentType == "image/jpeg" || picture.ContentType == "image/png") { Image image = Image.FromStream(picture.InputStream); if (image.Height > 200 || image.Width > 200) { Image smallImage = Class.ImageHelper.ScaleImage(image, 200, 200); Bitmap b = new Bitmap(smallImage); Guid guid = Guid.NewGuid(); string imageName = guid.ToString() + ".jpg"; b.Save(Server.MapPath("~/uploads/game/" + imageName), ImageFormat.Jpeg); smallImage.Dispose(); b.Dispose(); game.ImageName = imageName; } else { picture.SaveAs(Server.MapPath("~/uploads/game/" + picture.FileName)); } } } GameCategoryDao gameCategoryDao = new GameCategoryDao(); GameCategory gameCategory = gameCategoryDao.GetById(categoryId); game.Category = gameCategory; GameDao gameDao = new GameDao(); gameDao.Create(game); TempData["message-success"] = "Hra byla úspěšně vytvořena"; } else { return(View("Create", game)); } return(RedirectToAction("Index")); }