public ActionResult NewsEdit(int id) { var news = db.News.SingleOrDefault(r => r.ID == id); var image = new UploadImageModel() { InstanceId = news.ID, ParentId = news.ID, IsPrimary = false, CurrentImage = null, Type = "news" }; NewsEditViewModel _news = new NewsEditViewModel { Image = image, Title = news.Title, Content = news.Content, Id = news.ID, ImagePath = news.ImageUrl }; return View(_news); }
//public void DeleteImage(int imageId, string type) //{ // try // { // string image_path = GetImageUploadPatch(type); // switch (type) // { // case "news": // { // var image = db.News.Find(imageId); // foreach (int size in image_sizes) // { // RemoveImage(image.ImagePath, image_path, size); // } // db.PlaceImages.Remove(image); // db.SaveChanges(); // } // break; // case "products": // { // var image = db.ProductImages.Find(imageId); // foreach (int size in image_sizes) // { // RemoveImage(image.ImagePath, image_path, size); // } // db.ProductImages.Remove(image); // db.SaveChanges(); // } // break; // } // } // catch (Exception e) // { // } //} public bool UploadImage(UploadImageModel model) { try { string currentImage = model.CurrentImage; List<string> images = new List<string>(); images.Add(currentImage); //remove related image if (model.IsPrimary == true) { //if (model.Type == "website_logo") //{ // //remove previous logos related to the store model.id // var site = db.WebSites.SingleOrDefault(c => c.PlaceId == model.ParentId); // if (site.SiteLogo != null) // { // var image = site.SiteLogo; // string image_path = string.Format("websites/stores/{0}", model.ParentId); // foreach (int size in image_sizes) // { // RemoveImage(image, image_path, size); // } // } //} //else //{ // RemoveImages(images.ToArray(), model.Type); //} } var save_url = SaveImage(model, model.Type); ImageUrl = save_url; //If we had success so far if (save_url == null) throw new Exception("There no image to save!"); SaveImagePath(model, save_url); return true; } catch (Exception ex) { throw new Exception(ex.Message); } }
public ActionResult EventsEdit(int id) { var events = db.Events.Find(id); var image = new UploadImageModel() { InstanceId = events.ID, ParentId = events.ID, IsPrimary = false, CurrentImage = null, Type = "events" }; EventsEditViewModel _events = new EventsEditViewModel { Image = image, Title = events.Title, Content = events.Content, Id = events.ID, ImagePath = events.ImageUrl }; return View(_events); }
private void SaveImageMethod(UploadImageModel model, Bitmap original, string save_path, int size) { var img = CreateImage(original, model.X, model.Y, model.Width, model.Height); var fn = ThisServer.MapPath(save_path); if (size != 0) { Image imgActual = ScaleBySize(img, size); imgActual.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg); } else { img.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg); } //if (img.Size.Width > size) //{ // Image imgActual = ScaleBySize(img, size); // var fn = Server.MapPath(save_path); // imgActual.Save(fn, System.Drawing.Imaging.ImageFormat.Png); //} //else //{ // var fn = Server.MapPath(save_path); // img.Save(fn, System.Drawing.Imaging.ImageFormat.Png); //} }
private string SaveImage(UploadImageModel model, string type) { //Prepare the needed variables Bitmap original = null; var name = "newimagefile"; var errorField = string.Empty; string image_path = GetImagePath(type); //if (type == "website_logo" || type == "slides" || type == "second_slides") // image_path = image_path + model.ParentId + "\\"; string save_path = null; try { if (model.File != null) // model.IsFile { errorField = "File"; name = Path.GetFileNameWithoutExtension(model.File.FileName); original = Bitmap.FromStream(model.File.InputStream) as Bitmap; //If we had success so far if (original != null) { var fileName = Path.GetFileName(model.File.FileName); string append = String.Format("{0}_{1}_{2}", model.ParentId, model.IsPrimary, airtton.Helpers.IdFromInt.Base64Hash(8)); string finalFileName = String.Format("{0}{1}", append, ".jpg"); save_path = String.Format("{0}_{1}x{2}", image_path, 0, finalFileName); SaveImageMethod(model, original, save_path, 0); var path = Path.Combine(ThisServer.MapPath(image_path), finalFileName); save_path = string.Format("{0}", finalFileName); } return save_path; } } catch (Exception ex) { Debug.WriteLine(ex.Message); return save_path; } return save_path; }
private void SaveImagePath(UploadImageModel model, string save_url) { try { switch (model.Type) { case "news": { var news = db.News.SingleOrDefault(c => c.ID == model.ParentId); if (news != null) { news.ImageUrl = save_url; db.Entry(news).State = EntityState.Modified; } } break; case "events": { var events = db.Events.SingleOrDefault(c => c.ID == model.ParentId); if (events != null) { events.ImageUrl = save_url; db.Entry(events).State = EntityState.Modified; } } break; case "presidentDetail": { var presidentDetails = db.PresidentDetail.SingleOrDefault(c => c.ID == model.ParentId); if (presidentDetails != null) { presidentDetails.ImageUrl = save_url; db.Entry(presidentDetails).State = EntityState.Modified; } } break; } db.SaveChanges(); } catch (Exception ex) { Debug.WriteLine(ex.Message); throw new Exception("fail to save image path to data base", ex); } }