// TODO: authorize, asyn. public ActionResult _ImageUploader(IEnumerable<HttpPostedFileBase> files, string types) { List<TypeModel> updateTypes = new List<TypeModel>(); List<VMaxCMS.Repositories.image_type> imgT = new List<Repositories.image_type>(); if (!string.IsNullOrEmpty(types)) { updateTypes = JsonConvert.DeserializeObject<List<TypeModel>>(types); if (updateTypes != null && updateTypes.Count != 0) { ImageModel m = new ImageModel(); m.Types = updateTypes; imgT = m.TOImageTypes(); } } List<ImageModel> imgs = new List<ImageModel>(); try { foreach (var file in files) { var res = CheckImg(file); // TODO: return error info. if (res == "ok") { string dt = DateTime.Now.ToString("yyyy-MM"); string path = Path.Combine(HttpContext.Server.MapPath("../Content/UploadImage/"), dt); if (!Directory.Exists(path)) Directory.CreateDirectory(path); Guid name = Guid.NewGuid(); string extension = GetExtensionName(file.FileName).ToLower(); var fileName = name.ToString() + extension; var completePath = Path.Combine(path, fileName); file.SaveAs(completePath); try { using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(completePath)) { int width = sourceImage.Width; int height = sourceImage.Height; int smallWidth; int smallHeight = 768; smallWidth = smallHeight * width / height; using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage( sourceImage, new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel ); g.DrawRectangle(new Pen(Color.White, 35), 0, 0, smallWidth, smallHeight); string tPath = completePath.Replace(".jpg", "_border.jpg"); bitmap.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg); } /* using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Black); int lwidth = (smallWidth - intThumbWidth) / 2; int bheight = (smallHeight - intThumbHeight) / 2; g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel); //g.Dispose(); string tPath = completePath.Replace(".jpg", "_thum.jpg"); bitmap1.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg); } }*/ } } } catch(Exception ex) { Console.WriteLine(ex.ToString()); } // Make a thumbnail. try { using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(completePath)) { int width = sourceImage.Width; int height = sourceImage.Height; int smallWidth; int smallHeight = 146; smallWidth = smallHeight * width / height; using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.White); g.DrawImage( sourceImage, new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.GraphicsUnit.Pixel ); string tPath = completePath.Replace(".jpg", "_thum.jpg"); bitmap.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg); } /* using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Black); int lwidth = (smallWidth - intThumbWidth) / 2; int bheight = (smallHeight - intThumbHeight) / 2; g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel); //g.Dispose(); string tPath = completePath.Replace(".jpg", "_thum.jpg"); bitmap1.Save(tPath, System.Drawing.Imaging.ImageFormat.Jpeg); } }*/ } } } catch { } Image pic = Image.FromFile(completePath); int intWidth = pic.Width; int intHeight = pic.Height; var img = ImageSerivce.AddImage(Path.Combine(dt, fileName), intWidth, intHeight, imgT); // TODO: exception check. var imgModel = ImageModel.FromImage(img); if (!string.IsNullOrEmpty(types)) { imgModel.Types = updateTypes; } imgModel.ImagePathThum = imgModel.ImagePath.Replace(".jpg", "_thum.jpg"); imgs.Add(imgModel); } } } catch (Exception ex) { Console.Write(ex.Message); } // TOOD: Return img id. return Json(new { data = imgs }, JsonRequestBehavior.AllowGet); }
public ActionResult _RemoveImg(ImageModel model) { model.Valid = false; ImageSerivce.UpdateImage(model.ToImage(), model.TOImageTypes()); return Json(null, JsonRequestBehavior.AllowGet); }
public ActionResult _ImageUpdate(ImageModel model) { model.Valid = true; ImageSerivce.UpdateImage(model.ToImage(), model.TOImageTypes()); return null; }