直接上代码
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);
微信扫码查看本文
发表评论