/// <summary> /// Crop the image /// </summary> /// <param name="img">Image property</param> /// <param name="byteArray">The byte[] convertion after the image crop</param> /// <param name="error">Error message</param> public bool GetCroppedImageByte(Imagery img, out byte[] byteArray, out string error) { if (img.AttemptedSize.IsEmpty) { byteArray = new byte[0]; error = "Please set the X and Y coordinates and also the crop size."; return false; } using (var image = Bitmap.FromStream(img.ImageStream)) { var imageBitmap = new Bitmap(image); var rectangle = new Rectangle(img.XCoordinate, img.YCoordinate, img.AttemptedSize.Width, img.AttemptedSize.Height); using (var croppedBmp = imageBitmap.Clone(rectangle, imageBitmap.PixelFormat)) { using (var ms = new MemoryStream()) { croppedBmp.Save(ms, img.ImageFormat); byteArray = ms.ToArray(); error = "No errors found."; imageBitmap.Dispose(); return true; } } } }
/// <summary> /// Resize the image /// </summary> /// <param name="img">Image property</param> /// <param name="byteArray">The byte[] convertion after the image resize</param> /// <param name="error">Error message</param> public bool GetResizedImageByte(Imagery img, out byte[] byteArray, out string error) { if (img.AttemptedSize.IsEmpty) { error = "Please set the desire width and height for the size."; byteArray = new byte[0]; return false; } using (var imgOriginal = Image.FromStream(img.ImageStream)) { //get original width and height of the incoming image var originalWidth = imgOriginal.Width; var originalHeight = imgOriginal.Height; //get the percentage difference in size of the dimension that will change the least var percWidth = ((float)img.AttemptedSize.Width / (float)originalWidth); var percHeight = ((float)img.AttemptedSize.Height / (float)originalHeight); var percentage = Math.Max(percHeight, percWidth); //get the ideal width and height for the resize (to the next whole number) var width = (int)Math.Max(originalWidth * percentage, img.AttemptedSize.Width); var height = (int)Math.Max(originalHeight * percentage, img.AttemptedSize.Height); //actually resize it using (var resizedBmp = new Bitmap(width, height)) { using (var graphics = Graphics.FromImage((Image)resizedBmp)) { graphics.InterpolationMode = InterpolationMode.Default; graphics.DrawImage(imgOriginal, 0, 0, width, height); } // Optimize the bitmap using (var ms = new MemoryStream()) { resizedBmp.Save(ms, img.ImageFormat); error = "No errors found."; byteArray = ms.ToArray(); return true; } } } }
public ActionResult Crop(ImageViewModel viewModel) { viewModel.AttemptedSize = new Size(viewModel.AttemptedWidth, viewModel.AttemptedHeight); if (viewModel.AttemptedSize.IsEmpty) return RedirectToAction("Index"); string imageDir = "Uploads/Images/"; string originalImageName = "original.jpg"; string imagePath = Server.MapPath("/Content/" + imageDir + originalImageName); using (var image = Image.FromFile(imagePath)) { using (var ms = new MemoryStream()) { image.Save(ms, ImageFormat.Jpeg); var img = new Imagery(ms.ToArray(), ImageFormat.Jpeg); image.Dispose(); var imgEdit = new ImageManipulator(); img.SetCroppingCoordinates(viewModel.XCoordinate, viewModel.YCoordinate, viewModel.AttemptedSize.Width, viewModel.AttemptedSize.Height); byte[] imageCropByte; string error; if (imgEdit.GetCroppedImageByte(img, out imageCropByte, out error)) { byte[] optimizeImg = imgEdit.OptimizeImage(new MemoryStream(imageCropByte), ImageFormat.Jpeg, viewModel.ImageQuality); using (var newImageStream = new MemoryStream(optimizeImg)) { using (var newImage = Image.FromStream(newImageStream)) { string mapPath = Server.MapPath("/Content/"); string cropName = "cropped.jpg"; var fileInfo = new FileInfo(mapPath + imageDir + cropName); fileInfo.Delete(); newImage.Save(mapPath + imageDir + cropName); } } } } } return RedirectToAction("Index"); }
public ActionResult Index(ImageViewModel viewModel) { var imageStream = viewModel.UploadFileImage.InputStream; string mapPath = Server.MapPath("/Content/"); string imageDir = "Uploads/Images/"; string originalName = "original.jpg"; int widthLimit = 650; using (var image = Image.FromStream(imageStream)) { if (this.IsImageExist(viewModel, originalName)) { var fileInfo = new FileInfo(mapPath + imageDir + originalName); fileInfo.Delete(); byte[] resizeByte; string error; var img = new Imagery(imageStream, ImageFormat.Jpeg); var imgEdit = new ImageManipulator(); img.SetResize(widthLimit); if (imgEdit.GetResizedImageByte(img, out resizeByte, out error)) { using (var ms = new MemoryStream(resizeByte)) { var newImage = Image.FromStream(ms); newImage.Save(mapPath + imageDir + originalName); newImage.Dispose(); return RedirectToAction("Index"); } } } image.Save(mapPath + imageDir + originalName); } return RedirectToAction("Index"); }