2017-06-18 45 views
0

我在这里有圆形图像,我想要做的是将某些颜色放在特定的位置。例如,当我点击button1时,圆形的左侧将填充红色,当我点击button2时,右侧也会被填充,当我再次点击button1时,颜色将被删除,等等......如何在图像上添加一些颜色

我已经做了一些研究关于它,并找出了两种方法来做到这一点。首先是让这个圆圈和另一个图像相交。其次是绘制,并使用C#中的图形类。..

现在,我的问题是,是否有另一种可能的方式来做到这一点?什么是最好的方法?

P.S:这个的目的是为牙齿图。 :)

enter image description here

+1

你能告诉我们你的代码这么远? – mjwills

+1

使用多个图像可能是最简单的方法。 – moritzg

+1

有很多种方法。 “最好的”取决于你的规格,不,对于我们来说,“齿形图”对我们来说并不是一个有用的规范。一个基本问题是:你能否以几何形状描述形状?他们(你的情况)5 GraphicsPath显然是最好的选择。如果你不能实现floodfill ..请看看[这篇文章](https://stackoverflow.com/questions/38969309/use-fillpath-with-mouse-input-as-flood-fill-画之间的路径/ 38969673#38969673),看看你是否适合你的情况!其他[你可能会发现有用的帖子..](https://stackoverflow.com/search?q=user%3A3152130+floodfill) – TaW

回答

4

下面是基于qing`s发布可调整大小,可点击,用户控件。您可以直接点击区域来切换它们,或通过代码更改它们。

enter image description here

public partial class ToothChart : UserControl 
{ 

    public ToothChart() 
    { 
     InitializeComponent(); 
     this.DoubleBuffered = true; 
    } 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 

     if (this.ParentForm != null) 
     { 
      this.ParentForm.FormClosing += (s, evt) => { OnHandleDestroyed(new EventArgs()); }; 
     } 
    } 

    protected override void OnHandleDestroyed(EventArgs e) 
    { 
     base.OnHandleDestroyed(e); 

     if (this._pathTop != null) 
     { 
      this._pathTop.Dispose(); 
      this._pathTop = null; 
     } 
     if (this._pathRight != null) 
     { 
      this._pathRight.Dispose(); 
      this._pathRight = null; 
     } 
     if (this._pathBottom != null) 
     { 
      this._pathBottom.Dispose(); 
      this._pathBottom = null; 
     } 
     if (this._pathLeft != null) 
     { 
      this._pathLeft.Dispose(); 
      this._pathLeft = null; 
     } 
     if (this._pathCenter != null) 
     { 
      this._pathCenter.Dispose(); 
      this._pathCenter = null; 
     } 
    } 

    private GraphicsPath _pathTop = null; 
    private GraphicsPath _pathLeft = null; 
    private GraphicsPath _pathBottom = null; 
    private GraphicsPath _pathRight = null; 
    private GraphicsPath _pathCenter = null; 

    private bool _TopRegion = false; 
    public bool TopRegion 
    { 
     get 
     { 
      return _TopRegion; 
     } 
     set 
     { 
      if (_TopRegion != value) 
      { 
       _TopRegion = value; 
       this.Invalidate(); 
      } 
     } 
    } 

    private bool _RightRegion = false; 
    public bool RightRegion 
    { 
     get 
     { 
      return _RightRegion; 
     } 
     set 
     { 
      if (_RightRegion != value) 
      { 
       _RightRegion = value; 
       this.Invalidate(); 
      } 
     } 
    } 

    private bool _BottomRegion = false; 
    public bool BottomRegion 
    { 
     get 
     { 
      return _BottomRegion; 
     } 
     set 
     { 
      if (_BottomRegion != value) 
      { 
       _BottomRegion = value; 
       this.Invalidate(); 
      } 
     } 
    } 

    private bool _LeftRegion = false; 
    public bool LeftRegion 
    { 
     get 
     { 
      return _LeftRegion; 
     } 
     set 
     { 
      if (_LeftRegion != value) 
      { 
       _LeftRegion = value; 
       this.Invalidate(); 
      } 
     } 
    } 

    private bool _CenterRegion = false; 
    public bool CenterRegion 
    { 
     get 
     { 
      return _CenterRegion; 
     } 
     set 
     { 
      if (_CenterRegion != value) 
      { 
       _CenterRegion = value; 
       this.Invalidate(); 
      } 
     } 
    } 

    protected override void OnSizeChanged(EventArgs e) 
    { 
     base.OnSizeChanged(e); 
     if (this.IsHandleCreated && this._pathTop != null) 
     { 
      this.UpdateRegions(); 
     } 
    } 

    private void UpdateRegions() 
    { 
     int diameterBig = Math.Min(this.Width, this.Height) - 10; 
     int diameterSmall = Math.Min(this.Width, this.Height)/3; 
     if (diameterBig > 0 && diameterSmall > 0) 
     { 
      Point _centerPoint = new Point(this.Width/2, this.Height/2); 
      Rectangle rectangle = new Rectangle(_centerPoint.X - diameterBig/2, _centerPoint.Y - diameterBig/2, diameterBig, diameterBig); 
      Rectangle rectangle2 = new Rectangle(_centerPoint.X - diameterSmall/2, _centerPoint.Y - diameterSmall/2, diameterSmall, diameterSmall); 

      _pathTop.Reset(); 
      _pathTop.AddArc(rectangle, 225, 90); 
      _pathTop.AddArc(rectangle2, -45, -90); 

      _pathLeft.Reset(); 
      _pathLeft.AddArc(rectangle, 135, 90); 
      _pathLeft.AddArc(rectangle2, -135, -90); 

      _pathBottom.Reset(); 
      _pathBottom.AddArc(rectangle, 45, 90); 
      _pathBottom.AddArc(rectangle2, -225, -90); 

      _pathRight.Reset(); 
      _pathRight.AddArc(rectangle, -45, 90); 
      _pathRight.AddArc(rectangle2, -315, -90); 

      _pathCenter.Reset(); 
      _pathCenter.AddEllipse(rectangle2); 

      this.Invalidate(); 
     } 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     if (this.IsHandleCreated) 
     { 
      if (this._pathTop == null) 
      { 
       this._pathTop = new GraphicsPath(); 
       this._pathRight = new GraphicsPath(); 
       this._pathBottom = new GraphicsPath(); 
       this._pathLeft = new GraphicsPath(); 
       this._pathCenter = new GraphicsPath(); 
       this.UpdateRegions(); 
      } 

      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
      if (this.TopRegion) 
      { 
       e.Graphics.FillPath(Brushes.Blue, _pathTop); 
      } 
      e.Graphics.DrawPath(Pens.Black, _pathTop); 

      if (this.RightRegion) 
      { 
       e.Graphics.FillPath(Brushes.DarkRed, _pathRight); 
      } 
      e.Graphics.DrawPath(Pens.Black, _pathRight); 

      if (this.BottomRegion) 
      { 
       e.Graphics.FillPath(Brushes.Teal, _pathBottom); 
      } 
      e.Graphics.DrawPath(Pens.Black, _pathBottom); 

      if (this.LeftRegion) 
      { 
       e.Graphics.FillPath(Brushes.Yellow, _pathLeft); 
      } 
      e.Graphics.DrawPath(Pens.Black, _pathLeft); 

      if (this.CenterRegion) 
      { 
       e.Graphics.FillPath(Brushes.LightGreen, _pathCenter); 
      } 
      e.Graphics.DrawPath(Pens.Black, _pathCenter); 
     }   
    } 

    protected override void OnMouseClick(MouseEventArgs e) 
    { 
     base.OnMouseClick(e); 

     Point p = new Point(e.X, e.Y); 

     if (this._pathTop.IsVisible(p)) 
     { 
      this.TopRegion = !this.TopRegion; 
     } 
     else if (this._pathRight.IsVisible(p)) 
     { 
      this.RightRegion = !this.RightRegion; 
     } 
     else if (this._pathBottom.IsVisible(p)) 
     { 
      this.BottomRegion = !this.BottomRegion; 
     } 
     else if (this._pathLeft.IsVisible(p)) 
     { 
      this.LeftRegion = !this.LeftRegion; 
     } 
     else if (this._pathCenter.IsVisible(p)) 
     { 
      this.CenterRegion = !this.CenterRegion; 
     } 
    } 

} 
+0

嗨@Idle_Mind,其实我试图了解你的代码,但它似乎有点难我。我已经尝试过它,它的工作..你介意,如果我问,如果万一,我想在圆上添加一个颜色? –

+0

你的意思是内圈,中圈?你想让它变成彩色还是可点击? –

+1

这里的好东西。我通常不会用预先编写的代码来喂食答案,也不会解释,但这是相当高质量的工作。我建议的唯一的事情就是你重写Dispose(Control implements IDisposable)并确保你将GraphicsPath对象作为成员变量存储。这不会自动发生,并且GraphicsPath对象不会从System.ComponentModel.Component派生,因此您不能简单地将它们添加到Control的Components集合中,让它们由基类的Dispose方法自动处理,因此您需要自己做。 –

相关问题