如何根据坐标a List<Point>获取绘制的矩形客户区中的所有像素?

How can i get all pixels in a drawn rectangle client area according to the coordinates a List<Point>?

我在 pictureBox1 绘画事件中有此代码:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (cloudPoints != null)
    {
        if (DrawIt)
        {
            e.Graphics.DrawRectangle(pen, rect);
            int w = rect.Width;
            int h = rect.Height;
            int area = h * w;

            CloudEnteringAlert.pointtocolorinrectangle = cloudPoints;
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb);
            CloudEnteringAlert.Paint(e.Graphics, 1, 200, bmp);
        }
    }   
}

cloudPoints 是一个 List<Point> 包含 pictureBox1 图像中存在的所有像素。 可能是 14000 或 20。 例如,在索引 0 中,我看到 x = 122 y = 34

现在 rect 是我使用鼠标移动绘制的矩形。

然后我将黄色的 paint/color 像素坐标发送给 CloudEnteringAlert Paint 方法。 现在它将 paint/color cloudPoints 列表中的所有像素。

但我想对其进行更改,使其 paint/color 仅显示我绘制的矩形内的像素坐标。

因此,如果 cloudPoints 中的像素存在于所绘制矩形的客户端区域中,则仅发送此坐标。并非 cloudPoints 中的所有坐标只有绘制矩形中的坐标。

使用Rectangle.Contains(Point)方法。所以,如果你想得到矩形内的点,只需过滤列表

var pointsAffected = cloudPoints.Where(pt => rect.Contains(pt))

如果你有矩形的宽度和高度,比如左上角坐标 x0,y0,那么要过滤坐标列表,你可以认为所有坐标 x,y 在所需范围内面积在以下等式中:

(x0 < x < (x0+width)) and (y0 < y < (y0+width)). 

根据这个你可以用这个限制过滤你的像素:类似于@hometoast的答案

cloudPoints.Where(pt => pt.x < (x0+width) && pt.x>x0 &&pt.y>y0 && pt.y< (y0+width)))

对于圆圈和其他图形,您可以使用类似的等式来过滤像素。圆的例子是:(x-x0)^2 + (y-y0)^2 <= r^2