2014-09-23 67 views
-1

我用鼠標繪製了一個矩形,現在我想要在繪製矩形時繪製矩形,它將在每個矩形邊的底部,頂部,底部,右側繪製點。我如何用點填充矩形的邊緣?

這是怎麼看,當我剛剛繪製矩形,如:

Rectangle

,我想,雖然我畫實時矩形添加/墊的每個邊緣帶有X個點的矩形。例如,在每個邊緣上10個綠點與它們之間具有精確的空間。

例如我加入油漆點只是爲了說明我的意思:

Rectangle Padded

就在綠點應該是在厚的矩形的紅線和紅線。 綠點應填寫。 點之間應該有精確的空間。

我只是在頂部畫了一些點,但它應該在左下角和右側。

這是我現在如何繪製正方形矩形的矩形。

在Form1的頂部:

private Bitmap _bmpBU = null; 
public static Rectangle mRect; 

在構造函數中:

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

pictureBox1鼠標移動事件:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top); 
       pictureBox1.Invalidate(); 
      } 
     } 

pictureBox1鼠標按下事件:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      mRect = new Rectangle(e.X, e.Y, 0, 0); 
      Image iOLd = this.pictureBox1.Image; 
      Bitmap bmp = (Bitmap)_bmpBU.Clone(); 

      this.pictureBox1.Image = bmp; 
      if (iOLd != null) 
       iOLd.Dispose(); 
      pictureBox1.Invalidate(); 
     } 

而且pictureBox1漆事件:

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      using (Pen pen = new Pen(Color.Red, 2)) 
      { 
       e.Graphics.DrawRectangle(pen, mRect); 
      } 
     } 

我需要以某種方式在油漆事件,而它的繪製矩形也會墊,並添加綠點,每個邊緣。

+0

你的例子有點令人困惑,因爲你說你想要10分,但只畫4分。但是你是否說過,你想要一個函數,你可以通過矩形的四個邊(北,南,東,西)之一和一個數字,它會沿着一個邊繪製圓的數量? – Icemanind 2014-09-23 22:56:57

+0

icemanind在我的問題中我提到我用4點上傳的圖像只是我想要做的一個例子。它可以在每個邊緣上4點,或者10或25或100個用戶將決定。而對於第二部分你是對的,我想給一個數字讓我們說12,它將在北部,南部,東部,西部的每個邊緣上在它們之間的相同空間中繪製12個填充點。 – 2014-09-23 23:14:11

回答

0
private Point RectStartPoint; 
private Image img; 
private Image imgClone; 
private Pen myPen; 
private n = 10; //number of points 

你必須初始化這些對象,也許在:

public Form1() 
    { 
     InitializeComponent(); 

     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(imageOfPicturebox, 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 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 = 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)); 

       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(); 
    } 
+0

這不會像OP那樣在PictureBox__上繪製__on。它將__放入Image__中,將其搞亂。如果OP沒問題,那就好了。而且我沒有看到Paint事件。 – TaW 2014-09-24 10:19:34

+0

@TaW * imgClone *有原始圖像。在繪製* img *之前,我們用* imgClone *(參見* gDrawImage(imgClone,0,0); * in * DrawRectangle()* sub)清除它。所以我們不會搞亂它*。我從未畫畫。這不是偏好問題,而是更好的方式來處理您的圖紙。假設你有10個按鈕,每個按鈕繪製不同的形狀。你需要在paint中使用* if if if語句來檢查哪個按鈕被繪製。用我的方法,我清除* img * * imgClone *,* img *和* invalidate *。那簡單的 – 2014-09-24 13:27:27

+0

@TaW和OP的繪製矩形的方法不起作用。 – 2014-09-24 13:32:53

1

這僅僅是數學。添加這一功能:

private void DrawPointsOnRectangle(Graphics g, int numberOfPoints) 
{ 
    var brush = new SolidBrush(Color.DarkGreen); 
    const int rectanglePenWidth = 2; 

    //North & South 
    int spacing = mRect.Width/(numberOfPoints - 1); 

    for (int x = 0; x < numberOfPoints; x++) 
    { 
     g.FillEllipse(brush, mRect.X + (x * spacing) - rectanglePenWidth - 5, mRect.Y - 7, 15, 15); 
     g.FillEllipse(brush, mRect.X + (x * spacing) - rectanglePenWidth - 5, mRect.Y - 7 + mRect.Height, 15, 15);  
    } 

    //East & West 
    spacing = mRect.Height/(numberOfPoints - 1); 

    for (int y = 0; y < numberOfPoints; y++) 
    { 
     g.FillEllipse(brush, mRect.X - rectanglePenWidth - 5, mRect.Y - 7 + (y * spacing), 15, 15); 
     g.FillEllipse(brush, mRect.X - rectanglePenWidth - 5 + mRect.Width, mRect.Y - 7 + (y * spacing), 15, 15); 
    } 
} 

並修改pictureBox1_Paint函數調用新的功能:

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    using (Pen pen = new Pen(Color.Red, 2)) 
    { 
     e.Graphics.DrawRectangle(pen, mRect); 
     DrawPointsOnRectangle(e.Graphics, 5); 
    } 
} 

應該這樣做!您可以將5參數更改爲您想要的每個點上的許多點。

+0

icemanind這個工作很棒。你能否描述或告訴我數字7和15做什麼?我怎樣才能改變點大小更小或更大? – 2014-09-23 23:58:37

+0

例如,如果我希望綠色指向矩形紅線的相同大小。紅線厚度爲2.0f,但綠點大於那個。 – 2014-09-24 00:02:06