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