Winform绘制圆形图
1 private void Form_Load(object sender, EventArgs e) 2 { 3 PictureBox pic = new PictureBox(); 4 pic.Name = "pic1"; 5 pic.Size = new System.Drawing.Size(17, 17); 6 pic.BackgroundImage = GetRoundPic(Color.Red); 7 //pic.Paint += DrawPic_Paint; 8 this.Controls.Add(pic); 9 } 10 /// <summary> 11 /// 绘制圆形图,并赋值BackgroundImage 12 /// </summary> 13 public Bitmap GetRoundPic(Color color) 14 { 15 Size size = new Size(16, 16); 16 Bitmap bitmap = new Bitmap(size.Width, size.Height); 17 using (Graphics g = Graphics.FromImage(bitmap)) 18 { 19 using (Brush br = new SolidBrush(color)) 20 { 21 g.SmoothingMode = SmoothingMode.AntiAlias; 22 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 23 g.CompositingQuality = CompositingQuality.HighQuality; 24 g.FillEllipse(br, new Rectangle(Point.Empty, size)); 25 //Pen pen = new Pen(Color.Red); 26 //g.DrawEllipse(pen, new Rectangle(Point.Empty, size)); 27 } 28 } 29 return bitmap; 30 } 31 /// <summary> 32 /// 重绘圆形图 33 /// </summary> 34 private void DrawPic_Paint(object sender, PaintEventArgs e) 35 { 36 int ntemp = 2; 37 //根据样本类型,对应颜色 38 Brush brush = new SolidBrush(Color.Red); 39 Pen pen = new Pen(brush); 40 e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 41 e.Graphics.DrawEllipse(pen, new RectangleF(e.ClipRectangle.Left + ntemp, 42 e.ClipRectangle.Top + 43 ntemp, e.ClipRectangle.Width - (2 * ntemp), 44 e.ClipRectangle.Height - (2 * ntemp))); 45 //实心填充 46 Rectangle r = new Rectangle(e.ClipRectangle.Left + ntemp, e.ClipRectangle.Top + 47 ntemp, e.ClipRectangle.Width - (2 * ntemp), e.ClipRectangle.Height - (2 * ntemp)); 48 e.Graphics.DrawEllipse(pen, r); 49 e.Graphics.FillEllipse(brush, r); 50 }