Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将点坐标添加到矩形一侧的点F列表中?_C#_.net_Winforms - Fatal编程技术网

C# 如何将点坐标添加到矩形一侧的点F列表中?

C# 如何将点坐标添加到矩形一侧的点F列表中?,c#,.net,winforms,C#,.net,Winforms,我正在画一个矩形,在实时绘制矩形的同时,它也会在每个矩形的左、右、上、下边缘填充绿色点。 现在我将其设置为每个矩形边36个点: 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;

我正在画一个矩形,在实时绘制矩形的同时,它也会在每个矩形的左、右、上、下边缘填充绿色点。 现在我将其设置为每个矩形边36个点:

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;
        }
    }
}
然后在鼠标向上移动事件中,当我使用断点时,我看到列表点有4000多个点,其中许多点的坐标是相同的


例如,我只需要得到矩形绿点坐标的左边缘,因此点的末端应该只有36个坐标,以及从一个边缘到另一个边缘的距离。

我认为列表中有这么多点的原因是您正在调用
鼠标移动
上的
DrawRectangle()
方法,点击左键,每移动一个点都会调用它,它会将36点添加到你的点列表中

在代码中,您只是不断地向列表中添加点,但并没有从列表中清除值

<> P> >在输入代码< > DRAWEXPLILE()/CUT>方法时,应考虑清除列表,然后在代码>鼠标上事件> /代码>中检查。
private List<PointF> points = new List<PointF>();
PointF pointf = new PointF(Math.Min(RectStartPoint.X, pnt.X) - 3, y - 3);
points.Add(pointf);