private Image CropImage(Image img, CropImageViewModel cropArgs) { var scale = 1 / cropArgs.Zoom; var cropImg = new Bitmap((int)(cropArgs.Width * scale), (int)(cropArgs.Height * scale)); var radius = scale * cropArgs.CornerRadius; using (var g = Graphics.FromImage(cropImg)) { g.Clear(Color.Transparent); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(img, new RectangleF(0, 0, cropImg.Width, cropImg.Height), new RectangleF((cropArgs.OffsetX * scale), (cropArgs.OffsetY * scale), cropImg.Width, cropImg.Height), GraphicsUnit.Pixel); } switch (cropArgs.Shape) { case CropImageShape.Rectangle: if (cropArgs.CornerRadius > 0) return RoundCorners(cropImg, radius); break; case CropImageShape.Circle: CropEllipse(cropArgs, cropImg); break; case CropImageShape.Polygon: return CropPolygonImage(cropImg, cropArgs, cropImg.Width, cropImg.Height); } return cropImg; }
public Image CropResizeImage(Image img, CropImageViewModel cropArgs) { if (cropArgs.Proportion) { img = ZoomPicture(img, new Size((Int32)cropArgs.Width, (Int32)cropArgs.Height)); } else { img = CropImage(img, cropArgs); img = ResizeImage(img, cropArgs); } return img; }
private void CropEllipse(CropImageViewModel cropArgs, Bitmap cropImg) { var rect = new Rectangle(0, 0, cropImg.Width, cropImg.Height); using (var brush = new TextureBrush(cropImg, rect)) { using (var g = Graphics.FromImage(cropImg)) { g.Clear(Color.Transparent); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.FillEllipse(brush, 0, 0, cropImg.Width, cropImg.Height); } } }
public JsonResult UploadPost(CropImageViewModel cropImageModel) { var file = Request.Files["MyFile"]; if (file != null) { var mem = new MemoryStream(); var ext = Path.GetExtension(file.FileName).ToLower(); if (cropImageModel != null) { using (var img = System.Drawing.Image.FromStream(file.InputStream)) { using (var image = _imageHelper.CropResizeImage(img, cropImageModel)) { image.Save(mem, img.RawFormat); mem.Position = 0; } } } else file.InputStream.CopyTo(mem); var resource = _resourceManager.SaveImage(mem, ext); mem.Dispose(); return Json(new { resource.Name, resource.Url }); } return Json(null); }
private Image CropPolygonImage(Image img, CropImageViewModel cropArgs, int width, int height) { int banjing = width / 2; int poleX = width / 2; int poleY = height / 2; var point = new PointF[cropArgs.Polygons]; var cropImg = new Bitmap(width, height); for (int i = 0; i < cropArgs.Polygons; i++) { point[i].X = (float)(poleX + banjing * Math.Sin(i * 2 * Math.PI / cropArgs.Polygons)); point[i].Y = (float)(poleY - banjing * Math.Cos(i * 2 * Math.PI / cropArgs.Polygons)); } using (Graphics g = Graphics.FromImage(cropImg)) { g.Clear(Color.Transparent); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; using (var gp = new GraphicsPath()) { gp.AddPolygon(point); g.SetClip(gp); g.DrawImage(img, 0, 0); } img.Dispose(); } return cropImg; }
private Image ResizeImage(Image img, CropImageViewModel cropArgs) { var resizeImg = new Bitmap((int)(cropArgs.Width * cropArgs.Scale), (int)(cropArgs.Height * cropArgs.Scale)); using (var g = Graphics.FromImage(resizeImg)) { g.Clear(Color.Transparent); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(img, new Rectangle(0, 0, resizeImg.Width, resizeImg.Height)); } return resizeImg; }