asp.net C# 将图片裁剪成圆形,并保存到本地


//将图片裁剪成圆形

     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);
        } 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM