GDI+在Picturebox上绘了椭圆,要做的事情是:picuturebox上的image落在椭圆外部的都变黑 是不是得遍历 ?怎么判断像素点能否在椭圆外部?还是说有其他好方法
解决方案
20
可以参考一下代码
using (var g = pictureBox1.CreateGraphics()) { GraphicsPath path = new GraphicsPath(); path.AddEllipse(10, 10, 125, 125); path.AddRectangle(new Rectangle(new Point(), pictureBox1.Size)); g.FillPath(Brushes.Black, path); }
20
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace 裁剪 { public partial class Form1 : Form { public Form1() { InitializeComponent(); pictureBox1.Load("http://avatar.csdn.net/B/9/C/1_u011672494.jpg"); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { var el = sender as PictureBox; var g = e.Graphics; using (var path = new GraphicsPath()) { path.AddEllipse(new Rectangle(10, 10, el.Width - 20, el.Height - 20)); path.AddRectangle(el.ClientRectangle); g.FillPath(Brushes.Black, path); } } private void pictureBox2_Paint(object sender, PaintEventArgs e) { var el = sender as PictureBox; var g = e.Graphics; using (var path = new GraphicsPath()) { path.AddEllipse(new Rectangle(10, 10, el.Width - 20, el.Height - 20)); g.Clip = new Region(path); g.DrawImage(pictureBox1.Image, 0, 0); } } } }