public as_images GetImage(int id) { var res = new as_images(); res = db.as_images.FirstOrDefault(x => x.id == id); return(res); }
public int SaveImage(as_images item) { try { if (item.id == 0) { db.as_images.Add(item); db.SaveChanges(); } else { try { db.Entry(item).State = EntityState.Modified; db.SaveChanges(); } catch (OptimisticConcurrencyException ex) { RDL.Debug.LogError(ex); } } CacheManager.PurgeCacheItems("as_images_" + item.typeCode + "_" + item.itemID); } catch (Exception ex) { RDL.Debug.LogError(ex); } return(item.id); }
public string GetImageFolderLink(as_images item) { string res = ""; res = string.Format("/uploads/images/{0}/{1}/", item.typeCode, item.itemID); return(res); }
public string GetImageLink(as_images item, bool isThumb = false) { string res = ""; res = string.Format("{0}/{1}{2}{3}", GetImageFolderLink(item), Path.GetFileNameWithoutExtension(item.filename), isThumb ? "_thumb": "", Path.GetExtension(item.filename)); return(res); }
public ActionResult Upload() { int itemID = RDL.Convert.StrToInt(HttpContext.Request.Form["itemID"].ToString(), 0); string code = HttpContext.Request.Form["code"].ToString(); int thumbWidth = RDL.Convert.StrToInt(HttpContext.Request.Form["thumbWidth"].ToString(), 0); int thumbHeight = RDL.Convert.StrToInt(HttpContext.Request.Form["thumbHeight"].ToString(), 0); var mng = new ImagesManager(); var context = HttpContext; try { var file = context.Request.Files["Filedata"]; if (itemID == 0) { return(Content("0")); } if (file != null) { var item = new as_images { ord = 20000, itemID = itemID, filename = file.FileName, id = 0, description = "", name = "", url = "", typeCode = code }; int id = mng.SaveImage(item); if (file.ContentType.ToLower() == "image/jpeg" || file.ContentType.ToLower() == "image/jpg" || file.ContentType.ToLower() == "image/gif" || file.ContentType.ToLower() == "image/png" || file.ContentType.ToLower() == "application/octet-stream") { System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream); mng.UploadImage(item, img, thumbWidth, thumbHeight, file.ContentType.ToLower()); } } return(Content("1")); } catch (Exception ex) { RDL.Debug.LogError(ex); } return(Content("0")); }
internal void UploadImage(as_images item, System.Drawing.Image img, int thumbWidth, int thumbHeight, string contentType = "image/jpeg") { try { var folderPath = HttpContext.Current.Server.MapPath(GetImageFolderLink(item)); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } System.Drawing.Image origin = img; string thumbPath = HttpContext.Current.Server.MapPath(GetImageLink(item, true)); Bitmap thumbBitmap; Graphics originF; if (thumbWidth > 0 && thumbHeight > 0) { // создаем превью int ow = thumbWidth; // ширина картинки int oh = thumbHeight; // высота картинки float prop = System.Convert.ToSingle(origin.Width) / System.Convert.ToSingle(origin.Height); float normalprop = System.Convert.ToSingle(ow) / System.Convert.ToSingle(oh); if (prop > normalprop)// если по ширине длиннее { int hh = oh; int ww = System.Convert.ToInt32(hh * prop); thumbBitmap = new Bitmap(ow, oh); originF = Graphics.FromImage(thumbBitmap); originF.InterpolationMode = InterpolationMode.HighQualityBicubic; originF.PixelOffsetMode = PixelOffsetMode.HighQuality; // размер ow х hh SolidBrush brush31 = new SolidBrush(Color.White); Rectangle rect31 = new Rectangle(0, 0, ww, hh); originF.FillRectangle(brush31, rect31); originF.DrawImage(origin, 0, 0, ww, hh); } else { int ww = ow; int hh = System.Convert.ToInt32(ww / prop); thumbBitmap = new Bitmap(ow, oh); originF = Graphics.FromImage(thumbBitmap); originF.InterpolationMode = InterpolationMode.HighQualityBicubic; originF.PixelOffsetMode = PixelOffsetMode.HighQuality; // размер ow х hh // рисуем белый фон SolidBrush brush31 = new SolidBrush(Color.White); Rectangle rect31 = new Rectangle(0, 0, ww, hh); originF.FillRectangle(brush31, rect31); originF.DrawImage(origin, 0, 0, ww, hh); } EncoderParameters ep = new EncoderParameters(1); ImageCodecInfo icJPG = getCodecInfo(contentType); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); thumbBitmap.Save(thumbPath, icJPG, ep); originF.Dispose(); thumbBitmap.Dispose(); } // создаем оригинальный var originPath = HttpContext.Current.Server.MapPath(GetImageLink(item)); Bitmap originBitmap; var ep1 = new EncoderParameters(1); var icJPG1 = getCodecInfo(contentType); // getCodecInfo - функция, см. выше ep1.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); // качество originBitmap = new Bitmap(img); originBitmap.Save(originPath, icJPG1, ep1); origin.Dispose(); originBitmap.Dispose(); } catch (Exception ex) { RDL.Debug.LogError(ex); } }