【代码】C#/asp.net实现将图片裁剪为圆形

2021-07-20 10:31:20  阅读 2996 次 评论 0 条

直接上代码

 public static Bitmap CropToCircle(Image srcImage, Color backGround)
        {
            Bitmap dstImage = new Bitmap(srcImage.Width, srcImage.Height);
            Graphics g = Graphics.FromImage(dstImage);
            //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //下面这个也设成高质量
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //下面这个设成High
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            using (Brush br = new SolidBrush(backGround))
            {
                g.FillRectangle(br, 0, 0, dstImage.Width, dstImage.Height);
            }

            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, dstImage.Width, dstImage.Height);
            g.SetClip(path);
            g.DrawImage(srcImage, 0, 0);

            return dstImage;
        }

调用:

Image sourceImg=...;//这个加载源图片我就不写了
Color bgc = Color.FromArgb(0, Color.Transparent);
Bitmap pic = CropToCircle(sourceImg, bgc);



微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2149
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?