You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于点击中心坐标在PictureBox中创建不越界验证框

Solution: Create a Bounded Rectangle from Click Coordinates in PictureBox

Let's work through this step by step to build your desired rectangle that stays within the image bounds, plus save the coordinates for later click validation.

Step 1: Define Rectangle Dimensions

First, set the fixed width and height of your rectangle (since you mentioned "如图所示的矩形" — I'll assume you have specific dimensions in mind). Add these as class-level variables so they're accessible across methods:

private const int TargetRectWidth = 120; // Adjust to match your rectangle's width
private const int TargetRectHeight = 90; // Adjust to match your rectangle's height
private Rectangle _savedRectangle; // Stores the final bounded rectangle for later checks

Step 2: Calculate Bounded Rectangle Coordinates

In your pictureBox1_Click event, we'll calculate the rectangle's position while ensuring it never spills outside the image area. Here's how to handle those boundary checks:

private void pictureBox1_Click(object sender, EventArgs e)
{
    var mouseArgs = (MouseEventArgs)e;
    int centerX = mouseArgs.X;
    int centerY = mouseArgs.Y;

    // Start with initial top-left position based on the clicked center
    int initialLeft = centerX - TargetRectWidth / 2;
    int initialTop = centerY - TargetRectHeight / 2;

    // Adjust left edge to stay within the image (can't go below 0)
    int boundedLeft = Math.Max(0, initialLeft);
    // If the right edge would exceed the image width, shift left to fit
    if (boundedLeft + TargetRectWidth > pictureBox1.Image.Width)
    {
        boundedLeft = pictureBox1.Image.Width - TargetRectWidth;
    }

    // Adjust top edge to stay within the image (can't go below 0)
    int boundedTop = Math.Max(0, initialTop);
    // If the bottom edge would exceed the image height, shift up to fit
    if (boundedTop + TargetRectHeight > pictureBox1.Image.Height)
    {
        boundedTop = pictureBox1.Image.Height - TargetRectHeight;
    }

    // Create and save the final valid rectangle
    _savedRectangle = new Rectangle(boundedLeft, boundedTop, TargetRectWidth, TargetRectHeight);

    // Force the PictureBox to redraw so the rectangle is visible
    pictureBox1.Invalidate();
}

Step 3: Draw the Rectangle on the PictureBox

To let the user see the rectangle you created, handle the Paint event of the PictureBox:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    // Only draw if we've already created the rectangle
    if (_savedRectangle != Rectangle.Empty)
    {
        using (Pen drawPen = new Pen(Color.Blue, 2)) // Use your preferred color/thickness
        {
            e.Graphics.DrawRectangle(drawPen, _savedRectangle);
        }
    }
}

Step 4: Validate Subsequent Clicks

Later, when you need to check if a user's click falls inside the saved rectangle, use the Contains method of the Rectangle class. For example, in another click handler or validation logic:

private void ValidateClick(object sender, EventArgs e)
{
    var mouseArgs = (MouseEventArgs)e;
    Point clickPosition = new Point(mouseArgs.X, mouseArgs.Y);

    if (_savedRectangle.Contains(clickPosition))
    {
        // Click is inside the target rectangle
        MessageBox.Show("Your click is within the rectangle!");
    }
    else
    {
        // Click is outside the target rectangle
        MessageBox.Show("Your click is outside the rectangle.");
    }
}

Quick Notes:

  • If your PictureBox uses SizeMode = StretchImage, you'll need to scale the mouse coordinates to match the original image's dimensions (since stretched images distort pixel positions).
  • Tweak TargetRectWidth and TargetRectHeight to match the exact size of the rectangle from your reference image.

内容的提问来源于stack exchange,提问作者user9248102

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:48:22