2014-09-23 68 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