//将图片裁剪成圆形
private Image CutEllipse(Image img, Rectangle rec, Size size, string imgSavePath) { Bitmap bitmap = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(bitmap)) { using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec)) { br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.FillEllipse(br, new Rectangle(Point.Empty, size)); } } bitmap.Save(imgSavePath, System.Drawing.Imaging.ImageFormat.Png); return null; }
//方法调用
imgPath = "~/submitImages/20180913094627.png"; Image image = Image.FromFile(HttpContext.Current.Server.MapPath(imgPath)); //判断图片是否已经存在,若存在,删除 if (!File.Exists(imgSavePath)) { //File.Delete(Path.GetFullPath(imgSavePath));//删除存在 //将图片裁剪成圆形,并保存到本地 CutEllipse(image, new Rectangle(0, 0, 200, 200), new Size(200, 200), imgSavePath); }
private Image CutEllipse(Image img, Rectangle rec, Size size, string imgSavePath) { Bitmap bitmap = new Bitmap(size.Width, size.Height); using (Graphics g = Graphics.FromImage(bitmap)) { using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec)) { br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height); //br.ScaleTransform(bitmap.Width , bitmap.Height); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.FillEllipse(br, new Rectangle(Point.Empty, size)); } } bitmap.Save(imgSavePath, System.Drawing.Imaging.ImageFormat.Png); return null; }
原文:https://www.cnblogs.com/ciel-shan/p/10908721.html