public static void CropImage(string pathFrom, string destinationPath, int Width, int Height, AnchorPosition anchor, ImageFormat format, CropOptions cropOptions) { string pathRead = pathFrom; if (pathFrom == destinationPath) { FileInfo fi = new FileInfo(pathFrom); pathRead = Path.Combine(fi.Directory.FullName, System.Guid.NewGuid().ToString() + Path.GetExtension(pathFrom)); fi.MoveTo(pathRead); } System.Drawing.Image im = System.Drawing.Image.FromFile(pathRead); System.Drawing.Image dest = Crop(im, Width, Height, cropOptions); if (format == ImageFormat.Jpeg) { SaveJpeg(destinationPath, dest, JpegQuality); } else { dest.Save(destinationPath, format); } im.Dispose(); dest.Dispose(); if (pathRead != pathFrom) { File.Delete(pathRead); } RotateImageByExifOrientationData(destinationPath, true); }
public static System.Drawing.Image Crop(System.Drawing.Image imgPhoto, int Width, int Height, CropOptions cropOptions) { Rectangle cropRect = new Rectangle( cropOptions.x, cropOptions.y, cropOptions.width, cropOptions.height); Bitmap target = new Bitmap(Width, Height); using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(imgPhoto, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel); } return(target); }
public static void CropImage(string pathFrom, string destinationPath, int Width, int Height, AnchorPosition anchor, CropOptions cropOptions) { if (cropOptions != null) { CropImage(pathFrom, destinationPath, Width, Height, anchor, ImageFormat.Jpeg, cropOptions); } else { CropImage(pathFrom, destinationPath, Width, Height, AnchorPosition.Custom, ImageFormat.Jpeg); } }