使用GDI+ 处理图片成圆图
注:当前框架对应.net core 2.1中的代码
一、使用剪切路径,圆形剪切
Bitmap bitmap = new Bitmap(filename); Image photo = Image.FromFile(photofile); //绘制图片 Graphics g = Graphics.FromImage(bitmap); //设置路径 GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(new Rectangle(445, 580, 180, 180)); g.SetClip(gp); //绘图 g.DrawImage(photo, 445, 580, 180, 180); //保存生成 bitmap.Save(@"E:\\target.jpg");
绘制结果:
二、使用图像画刷,圆形绘制
注:这种方式,图片的大小是绘制的最小单位,没法放大,可以缩小。
可以平铺处理。
private Image CutEllipse(Image img, Rectangle rec, Size size) { 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)); } } return bitmap; }
Image image = this.pictureBox1.Image; Image newImage = CutEllipse(image, new Rectangle(0, 0, 150, 150), new Size(150, 150)); this.pictureBox2.Image = newImage;
更多: