简体   繁体   English

在picturebox上绘制矩形 - 如何限制矩形区域?

[英]Drawing rectangle on picturebox - how to limit area of rectangle?

i'm drawing rectangle on picturebox with mouse events: 我在带有鼠标事件的图片框上绘制矩形:

private void StreamingWindow_MouseDown(object sender, MouseEventArgs e)
    {
              rect = new Rectangle(e.X, e.Y, 0, 0);
              this.Invalidate();       
    }

    private void StreamingWindow_Paint(object sender, PaintEventArgs e)
    {

       if (painting == true)
        {

            using (Pen pen = new Pen(Color.Red, 2))
            {
                e.Graphics.DrawRectangle(pen, rect);
            }
        }
    }

    private void StreamingWindow_MouseMove(object sender, MouseEventArgs e)
    {       
           if (e.Button == MouseButtons.Left)
           {
               // Draws the rectangle as the mouse moves
               rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
           }
           this.Invalidate();     
    }

After drawing rectangle i can capture inside of it, and save as jpg. 绘制矩形后,我可以捕获它,并保存为jpg。

What's my problem? 我的问题是什么?

I can draw retangle which borders are outside area of picturebox: 我可以绘制边框在picturebox区域之外的边框:

在此输入图像描述

How can i limit area of rectangle that border of picturebox is max allowed location of rectangle? 如何限制图片框边框的矩形区域是矩形的最大允许位置?

Sorry for my english, i hope you'll understand my problem :) So as a result i'd like to have something like this: 对不起,我的英文,我希望你能理解我的问题:)所以我希望有这样的东西:

在此输入图像描述

private void StreamingWindow_MouseMove(object sender, MouseEventArgs e)
{       
  if (e.Button == MouseButtons.Left)
  {
    // Draws the rectangle as the mouse moves
    rect = new Rectangle(rect.Left, rect.Top, Math.Min(e.X - rect.Left, pictureBox1.ClientRectangle.Width - rect.Left), Math.Min(e.Y - rect.Top, pictureBox1.ClientRectangle.Height - rect.Top));
  }
  this.Invalidate();     
}

Another way for solving this is preventing rectangle to be drown outside of pictureBox control 解决这个问题的另一种方法是防止矩形被淹没在pictureBox控件之外

private void StreamingWindow_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (e.X < StreamingWindow.Width && Math.Abs(e.Y) < StreamingWindow.Height)
                // Draws the rectangle as the mouse moves
                rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y -rect.Top);
        }
        this.Invalidate();
    }

Somebody can find this solution more useful 有人可以发现这个解决方案更有用

I would say the easiest way to achieve this, and I personally think more natural one from UX perspective, is: after MouseUp check if BottomLeft corner of rectangle is outside of area of picture box, if so, just bring it "back" and align it to the picture box's angle just as you drawed . 我想说实现这一目标的最简单的方法,我个人认为从UX角度来看更自然,是:在MouseUp检查矩形的BottomLeft角落是否在图片框的区域之外,如果是这样,只需将其“返回”并对齐就像你绘制的那样,它与图片框的角度相同

EDIT 编辑

Just to give you an idea what I'm talking about, a pseudocode 只是为了让你知道我在说什么,一个伪代码

    private void StreamingWindow_MouseUp(object sender, MouseEventArgs e)
    {
              if(rect.Right > myPictureBox.ClientRectangle.Right)
              {
                 rect.Width = myPictureBox.Right - rect.Left - someoffset;                     
              }       
              if(rect.Bottom > myPictureBox.ClientRectangle.Bottom)
              {
                 rect.Height= myPictureBox.Bottom - rect.Top - someoffset;                     
              }       
    }

Something like this. 像这样的东西。 But you need to check this. 但你需要检查一下。

Why not setting the rect coords to something 为什么不设置矩形坐标

 rect = new Rectangle(min(e.X, pictureBoxX), min(e.Y, pictureBoxY), 0, 0);

You need to calculate pictureX and pictureY according to the location and position of the picture box. 您需要根据图片框的位置和位置计算pictureX和pictureY。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM