How can i pad the edges of a rectangle with points?

James Aharon

I'm drawing a rectangle with the mouse and now i want that while i'm drawing the rectangle it will draw points on each of the rectangle edges bottom,top,keft,right.

This is how it look like when i'm drawing just the rectangle:

직사각형

And i want that while i'm drawing the rectangle in real time to add/pad each edge of the rectangle with X number of points. For example on each edge 10 green points with exactly space between them.

For example i added the points in paint just to show what i mean:

직사각형 패딩

Just the green points should be in the thick of the red lines of the rectangle and on the red lines. The green points should be filled. And there should be exact space between the points.

방금 상단에 몇 점을 그렸지만 왼쪽 하단과 오른쪽에도 있어야합니다.

이것이 내가 이제 정사각형을 직사각형으로 그리는 방법입니다.

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);
            }
        }

사각형을 그리는 동안 페인트 이벤트에서 어떻게 든 필요하며 각 가장자리에 녹색 점을 추가하고 추가합니다.

γηράσκω δ 'αεί πολλά διδασκόμε
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();
    }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I rotate a Rectangle?

분류에서Dev

How can I fit an image to a rectangle in visio?

분류에서Dev

How can I zero-pad numbers in a Batch FOR loop?

분류에서Dev

How can I get the number of common edges in Spark Graphx?

분류에서Dev

How can i draw a rectangle around a label control?

분류에서Dev

How can I determine the original directory that an alias points to?

분류에서Dev

How can I make a rectangle be on top of all other rectangles in javascript canvas?

분류에서Dev

How do I change a white rectangle into a colored rectangle using glut?

분류에서Dev

How Do I Make A Rectangle in GEOS?

분류에서Dev

How do I select a Rectangle that's already been drawn?

분류에서Dev

Can I move a function in C by copying the data a function pointer points to?

분류에서Dev

How do I produce an interpolation function given n data points?

분류에서Dev

android OpenGl How to draw a rectangle

분류에서Dev

how to move and scale image by dragging it's edges?

분류에서Dev

How can I perform an OR with ScalaTest

분류에서Dev

How can I run owncloud?

분류에서Dev

How can I organize this array

분류에서Dev

How can I rewrite this a generic?

분류에서Dev

How can I get a RejectedExecutionException

분류에서Dev

How can i manage this request?

분류에서Dev

How can I get a solution for this?

분류에서Dev

How can I foreach a hashmap?

분류에서Dev

How can I stop the timer?

분류에서Dev

How can I repack JPEG?

분류에서Dev

How i can use a criteria in cicle for i

분류에서Dev

How can I run a desktop in the cloud that I can remotely connect to?

분류에서Dev

If I have apply(i: Int), how can I have map?

분류에서Dev

How can you delete a array of pointers which points to a dynamically allocated objects

분류에서Dev

How can display a little KML 3.5MB file on Google Maps but with around of 1 million of Points

Related 관련 기사

  1. 1

    How can I rotate a Rectangle?

  2. 2

    How can I fit an image to a rectangle in visio?

  3. 3

    How can I zero-pad numbers in a Batch FOR loop?

  4. 4

    How can I get the number of common edges in Spark Graphx?

  5. 5

    How can i draw a rectangle around a label control?

  6. 6

    How can I determine the original directory that an alias points to?

  7. 7

    How can I make a rectangle be on top of all other rectangles in javascript canvas?

  8. 8

    How do I change a white rectangle into a colored rectangle using glut?

  9. 9

    How Do I Make A Rectangle in GEOS?

  10. 10

    How do I select a Rectangle that's already been drawn?

  11. 11

    Can I move a function in C by copying the data a function pointer points to?

  12. 12

    How do I produce an interpolation function given n data points?

  13. 13

    android OpenGl How to draw a rectangle

  14. 14

    how to move and scale image by dragging it's edges?

  15. 15

    How can I perform an OR with ScalaTest

  16. 16

    How can I run owncloud?

  17. 17

    How can I organize this array

  18. 18

    How can I rewrite this a generic?

  19. 19

    How can I get a RejectedExecutionException

  20. 20

    How can i manage this request?

  21. 21

    How can I get a solution for this?

  22. 22

    How can I foreach a hashmap?

  23. 23

    How can I stop the timer?

  24. 24

    How can I repack JPEG?

  25. 25

    How i can use a criteria in cicle for i

  26. 26

    How can I run a desktop in the cloud that I can remotely connect to?

  27. 27

    If I have apply(i: Int), how can I have map?

  28. 28

    How can you delete a array of pointers which points to a dynamically allocated objects

  29. 29

    How can display a little KML 3.5MB file on Google Maps but with around of 1 million of Points

뜨겁다태그

보관