2014-09-24 71 views
-1

我正在繪製一個矩形,並在實時繪製矩形的同時,還填充每個矩形邊緣,左邊,右邊,上邊和下邊,每邊都有綠色的點。 現在我將它設置爲36個點每個矩形邊緣:如何將點座標添加到矩形的一側的PointF列表中?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace mws 
{ 
    public partial class PaddingPoints : Form 
    { 
     private List<PointF> points = new List<PointF>(); 
     private Point RectStartPoint; 
     private Image img; 
     private Image imgClone; 
     private Pen myPen; 
     private int n = 36; //number of points 
     private Bitmap _bmpBU = null; 

     public PaddingPoints() 
     { 
      InitializeComponent(); 

      this.DoubleBuffered = true; 
      _bmpBU = new Bitmap(@"D:\MyWeatherStation-Images-And-Icons\radar090.PNG"); 

      myPen = new Pen(Brushes.Red, 2); 
      //Bitmap to hold the picturebox image 
      img = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
      Graphics g; 
      using (g = Graphics.FromImage(img)) 
      { 
       g.DrawImage(_bmpBU, 0, 0, pictureBox1.Width, pictureBox1.Height); 
      } 

      //image to hold the original picturebox. We need it to clear img to the original 
      //picturebox image 
      imgClone = (Bitmap)img.Clone(); 

      //We draw always on img and then we invalidate 
      pictureBox1.Image = img; 
     } 

     private void PaddingPoints_Load(object sender, EventArgs e) 
     { 

     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      RectStartPoint = e.Location; 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left && e.Location != RectStartPoint) 
      { 
       DrawRectangle(e.Location); 
      } 
     } 

     private void DrawRectangle(Point pnt) 
     { 
      Graphics g = Graphics.FromImage(img); 
      int width, height, i, x, y; 

      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 

      //Clear img from the rectangle we drawn previously 
      g.DrawImage(imgClone, 0, 0); 


      if (pnt.X == RectStartPoint.X || pnt.Y == RectStartPoint.Y) 
      { 
       g.DrawLine(myPen, RectStartPoint.X, RectStartPoint.Y, pnt.X, pnt.Y); 
      } 
      else 
      { 
       g.DrawRectangle(myPen, Math.Min(RectStartPoint.X, pnt.X), Math.Min(RectStartPoint.Y, pnt.Y), 
           Math.Abs(RectStartPoint.X - pnt.X), Math.Abs(RectStartPoint.Y - pnt.Y)); 

       //width of spaces between points 
       width = (int)((Math.Abs(RectStartPoint.X - pnt.X))/(n - 1)); 
       //height of spaces between points 
       height = (int)((Math.Abs(RectStartPoint.Y - pnt.Y))/(n - 1)); 
       //we always want the upper left x, y coordinates as a reference drawing clockwise 
       x = Math.Min(RectStartPoint.X, pnt.X); 
       y = Math.Min(RectStartPoint.Y, pnt.Y); 

       //Drawing the points. change the 3, 6 values for larger ones 
       for (i = 0; i < n - 1; i++) 
       { 
        //Up side 
        g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6)); 
        //Right side 
        g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, y - 3, 6, 6)); 
        //Bottom side 
        g.FillEllipse(Brushes.Green, new Rectangle(x - 3, Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6)); 
        //Left side 
        g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3, 6, 6)); 
        PointF pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3); 
        points.Add(pointf); 
        x += width; 
        y += height; 
       } 
       g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, 
           Math.Min(RectStartPoint.Y, pnt.Y) - 3, 6, 6)); 
       g.FillEllipse(Brushes.Green, new Rectangle(Math.Min(RectStartPoint.X, pnt.X) + Math.Abs(RectStartPoint.X - pnt.X) - 3, 
          Math.Min(RectStartPoint.Y, pnt.Y) + Math.Abs(RectStartPoint.Y - pnt.Y) - 3, 6, 6)); 
      } 

      g.Dispose(); 

      //draw img to picturebox 
      pictureBox1.Invalidate(); 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      int t = points.Count; 
     } 
    } 
} 

結果是:

Rectangle with green points

現在我想做好兩件事在pictureBox1鼠標向上事件:

具有其中一個矩形邊的座標的PointF列表。 例如,如果它是左邊緣,則List應該包含每個點36個座標X,Y。 如果它是頂邊,那麼對於底邊和右邊也是36點並且是相同的。 但是每個時間點只能在列表中獲得一個邊。

而且還有一個變量,將保存距離另一個邊緣的距離: 例如,如果我添加到List點左邊緣的36個點我想獲得int變量的距離在左邊緣和八邊之間。 如果我在列表中添加了右邊緣的36個點,那麼int變量應該保持從右邊到左邊的距離。上下邊緣相同。

取決於我從哪裏得到點。

對於點i的添加到頂部的列表:

private List<PointF> points = new List<PointF>(); 

然後在DrawRectangle的方法I中加入:

PointF pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3); 
points.Add(pointf); 

然後在鼠標向上事件時我使用斷點在那裏,我看到列表點有更多的4000點座標,其中許多都是相同的。

我只需要得到例如矩形綠色點座標的左邊緣,所以點應該只在最後的36個座標以及從一個邊到另一個邊的距離。

回答

0

我想在你的名單有這麼多點的原因是,你在Mouse Move調用DrawRectangle()方法,將被要求按左鍵每點移動點擊,它會增加36點貴點列表。

在你的代碼中,你只是不斷地給列表添加點,但是你沒有清除列表中的值。

因此,當您輸入DrawRectangle()方法時,您應該考慮清除列表,然後在mouse up event中檢查它。