public static string CropImage(byte[] fileContent, CropImageModel model, out Exception exc, string outputFilePath = "") { try { if (string.IsNullOrEmpty(outputFilePath) || string.IsNullOrWhiteSpace(outputFilePath)) { throw new Exception("آدرس خروجی فایل را وارد نمایید"); } Rectangle cropArea = new Rectangle(model.X, model.Y, model.Width, model.Height); return(CropImage(fileContent, cropArea, out exc, outputFilePath)); } catch (Exception ex) { exc = ex; return(""); } }
/// <summary> /// This Method will get uploaded image and show full image to crop. /// </summary> /// <param name="width"> Ideal width for image </param> /// <param name="height">Ideal height for image </param> /// <param name="imagename">Image name as guid.</param> /// <returns></returns> public ActionResult Index(int width, int height, string imagename, string FileuploaderCss) { var model = new CropImageModel(); model.ImageName = imagename; var image = System.Drawing.Image.FromFile(Server.MapPath("~/WebData/Cropped/" + imagename)); if (image != null && (image.Width < width || image.Height < height)) { var dimensionwidth = 0; var dimensionheight = 0; dimensionwidth = (image.Width < width) ? width + 100 : image.Width + 100; dimensionheight = (image.Height < height) ? height + 100 : image.Height + 100; var szDimensions = new Size(dimensionwidth, dimensionheight); Bitmap resizedImg = new Bitmap(szDimensions.Width, szDimensions.Height); Graphics gfx = Graphics.FromImage(resizedImg); gfx.FillRectangle(Brushes.White, 0, 0, resizedImg.Width, resizedImg.Height); // Paste source image on blank canvas, then save it as .png var xrectangle = (dimensionwidth) / 2; var yrectangle = (dimensionheight) / 2; var ximage = image.Width / 2; var yimage = image.Height / 2; gfx.DrawImageUnscaled(image, xrectangle - ximage, yrectangle - yimage); image.Dispose(); image = null; resizedImg.Save(Request.PhysicalApplicationPath + "WebData\\Cropped\\" + imagename); resizedImg.Dispose(); gfx.Dispose(); } else { image.Dispose(); image = null; } model.FileUploaderCss = FileuploaderCss; model.Imagepath = "~/WebData/Cropped/" + imagename; model.Height = height; model.Width = width; return(View(model)); }
public ActionResult Index(CropImageModel model, string command) { try { if (string.IsNullOrEmpty(command)) { try { var image = System.Drawing.Image.FromFile(Server.MapPath("~/WebData/Cropped/" + model.ImageName)); var cropcords = new Rectangle(model.Xaxis, model.Yaxis, model.Width, model.Height); string cfname, cfpath; //Added on 18-08-2016 for image size optimization var bitMap = new Bitmap(cropcords.Width, cropcords.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); bitMap.SetResolution(60, 60); var grph = Graphics.FromImage(bitMap); grph.CompositingQuality = CompositingQuality.HighQuality; grph.SmoothingMode = SmoothingMode.HighQuality; grph.InterpolationMode = InterpolationMode.HighQualityBicubic; grph.DrawImage(image, new Rectangle(0, 0, bitMap.Width, bitMap.Height), cropcords, GraphicsUnit.Pixel); //--------------------------------------------------------------------------- cfname = "crop_" + model.ImageName; cfpath = Path.Combine(Server.MapPath("~/WebData/Cropped"), cfname); bitMap.Save(cfpath); if (string.IsNullOrEmpty(model.FileUploaderCss)) { TempData["CroppedImage"] = "~/WebData/Cropped/" + cfname; } else { TempData[model.FileUploaderCss] = "~/WebData/Cropped/" + cfname; } model.IsFileCroped = true; image.Dispose(); bitMap.Dispose(); grph.Dispose(); try { System.IO.File.Delete(Server.MapPath("~/WebData/Cropped/" + model.ImageName)); } catch { } return(View(model)); } catch { return(View(model)); } } else { try { TempData["CroppedImage"] = null; if (!string.IsNullOrEmpty(model.FileUploaderCss)) { TempData[model.FileUploaderCss] = null; } System.IO.File.Delete(Server.MapPath("~/WebData/Cropped/" + model.ImageName)); } catch (Exception ex) { } model.IsCancelled = true; return(View(model)); } } catch (Exception ex) { return(new ContentResult() { Content = "Please try again later. <br />" + ex.Message }); } }
public ActionResult Index(CropImageModel model, string command) { try { if (string.IsNullOrEmpty(command)) { try { var image = System.Drawing.Image.FromFile(Server.MapPath("~/WebData/Cropped/" + model.ImageName)); var cropcords = new Rectangle(model.Xaxis, model.Yaxis, model.Width, model.Height); string cfname, cfpath; var bitMap = new Bitmap(cropcords.Width, cropcords.Height); var grph = Graphics.FromImage(bitMap); grph.DrawImage(image, new Rectangle(0, 0, bitMap.Width, bitMap.Height), cropcords, GraphicsUnit.Pixel); cfname = "crop_" + model.ImageName; cfpath = Path.Combine(Server.MapPath("~/WebData/Cropped"), cfname); bitMap.Save(cfpath); if (string.IsNullOrEmpty(model.FileUploaderCss)) { TempData["CroppedImage"] = "~/WebData/Cropped/" + cfname; } else { TempData[model.FileUploaderCss] = "~/WebData/Cropped/" + cfname; } model.IsFileCroped = true; image.Dispose(); bitMap.Dispose(); grph.Dispose(); try { System.IO.File.Delete(Server.MapPath("~/WebData/Cropped/" + model.ImageName)); } catch { } return(View(model)); } catch { return(View(model)); } } else { try { TempData["CroppedImage"] = null; if (!string.IsNullOrEmpty(model.FileUploaderCss)) { TempData[model.FileUploaderCss] = null; } System.IO.File.Delete(Server.MapPath("~/WebData/Cropped/" + model.ImageName)); } catch (Exception ex) { } model.IsCancelled = true; return(View(model)); } } catch (Exception ex) { return(new ContentResult() { Content = "Please try again later. <br />" + ex.Message }); } }