Im drawing a rectangle in a picturebox1 how do i find the retangle borders ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    Im drawing a rectangle in a picturebox1 how do i find the retangle borders ?

    I have a code with a mouseUp event.
    In this event im drawing with the mouse a rectangle inside a picturebox1 on image.

    Now the problem is when i draw the rectangle too big so its getting out the borders of hte image or the picturebox1. The thing is its getting out of the borders inside the picturebox1 i just cant see it.

    I want that if the rectangle is out of the borders so it will write something...

    Now the variables: FirstImage is the image the user select to draw on with fileDialoge.
    The variable RectImage is a REctangle wich im using to draw with.

    and the line where i want to check the rectangle borders with the picturebox1/or the image borders is:

    Code:
    if (newLeft > 0 || newTop > 0)
                        
                                   {
                        MessageBox.Show("You are out of region!");
    
                        return;
                     }
    Now this IF statement isnt right. I cant figure out i tried so much cant figure out what to compare and ho to check that if i draw too much big rectangle it will write.


    This is the complete mouseUp event code:

    Code:
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    tempBmp = new Bitmap(FirstImage);
                    float newLeft = ((float)Rect.Left/(float)pictureBox1.Width)*((float)FirstImage.Width);
                    float newRight = ((float)Rect.Right/(float)pictureBox1.Width)*((float)FirstImage.Width);
                    float newTop = ((float)Rect.Top/(float)pictureBox1.Height)*((float)FirstImage.Height);
                    float newBottom = ((float)Rect.Bottom/(float)pictureBox1.Height)*((float)FirstImage.Height);
    
                    RectImage = new Rectangle((int)newLeft, (int)newTop, (int)newRight - (int)newLeft, (int)newBottom - (int)newTop);                
                    
                    if (RectImage.Size.Height > FirstImage.Size.Height ) ||
                        RectImage.Size.Width > pictureBox1.Bounds.Left || RectImage.Width > pictureBox1.Bounds.Right)    //bounds are not ok )
                    {
                        MessageBox.Show("You are out of region!");
    
                        return; 
                    }
    
    
                    
    
                    int i, j;
                    for (i=RectImage.Left;i<RectImage.Right;i++)
                        for (j = RectImage.Top; j < RectImage.Bottom; j++)
                        {
                            Color c = FirstImage.GetPixel(i, j);
                            tempBmp.SetPixel(i, j, c);
                        }
                    pictureBox1.Image = tempBmp;
                    textBox5.Text = "Right " + RectImage.Right + " Bottom " + RectImage.Bottom;
                    textBox4.Text = "Left " + RectImage.Left + " Top " + RectImage.Top;
                    
                    if (FirstImage != null && SecondImage != null)
                    {
                        
                        if (this._dontDrawRect == false)
                        {
                            if (MessageBox.Show("Use this Rectangle for comparing?", "Question",
                             MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                             System.Windows.Forms.DialogResult.Yes)
                            {
                                this._dontDrawRect = true;
                            }
                        }
    
                        
                        if (_dontDrawRect)
                        {
                            if (copy_mode == true)
                            {
                                MessageBox.Show("hi");
                            }
                            else
                            {
                                if (ImagesComparion1.ImageComparison(FirstImage, SecondImage, Rect)) 
                                {
                                    textBox1.Text = ImagesComparion1.textbox1;
                                    textBox2.Text = ImagesComparion1.textbox2;
                                    textBox3.Text = ImagesComparion1.textbox3;
                                    MessageBox.Show("identical");
                                }
                                else
                                {
                                    textBox1.Text = ImagesComparion1.textbox1;
                                    textBox2.Text = ImagesComparion1.textbox2;
                                    textBox3.Text = ImagesComparion1.textbox3;
                                    MessageBox.Show("different");
                                }
                            }
                        }
                    }
                }
                else if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    this._dontDrawRect = false;
    
            }
    The IF in this code isnt good too.


    Thanks.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You need to check to make sure your mouse rectangle is completely inside your panel rect, right?

    To do that, just compare the values you have available on the Rectangle class. Specifically, Left, Right, Top, and Bottom.

    Draw two rectangles on a piece of paper that overlap, then think about what properties you would need to test. In this case, lets say you have two rectangles, R1 and R2. Consider the x-axis. The R2 is contained by R1 if its left edge is greater than R1's left edge, and if its right edge is less than R1's right edge. Likewise, for the y-axis, you can make sure R2's top is greater than R1's top and that its bottom is less than R1's bottom. In code...

    Code:
    if (R2.Left > R1.Left &&
        R2.Right < R1.Right &&
        R2.Top > R1.Top &&
        R2.Bottom < R1.Bottom)
    {
      // R1 contains R2
    }
    That's the math of it, but fortunately there are actually built in methods you can use.

    Rectangle.Conta ins

    Rectangle.Inter sectsWith

    Comment

    • Chocolade
      New Member
      • Aug 2010
      • 69

      #3
      This is working:

      Code:
      (e.X > pictureBox1.Size.Width || e.Y > pictureBox1.Size.Height)
      Since the drawing is all the time start from any point and go to the right or down only i dont need Top and Left.



      Anyway this is working.


      Thanks.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        What would happen if, instead of dragging down and right to draw the rectangle, you dragged up and left?

        Comment

        • Chocolade
          New Member
          • Aug 2010
          • 69

          #5
          Nothing. It dosent draw anything and dosent give any error.

          Why ? Isnt it good ? The reason is cuz i needed this rectangle to mark a text in the center of the image to identify the text location. So it dosent realy matter for now from wich direction i draw the rectangle.

          I guess ill add the top and left sometime.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            No I was just curious. If it doesn't draw anything it's probably just not calculating your rectangle correctly but if it's not desirable behaviour anyway, it's fine. I just wanted you to think about that scenario ;)

            The code you have only covers the case when the release location is past the bottom-right of the picturebox. It's quite possible to define a rectangle clicking inside, then dragging up and left, which means that the final click location could potentially be outside the bounds of the picturebox but up and to the left.

            Comment

            Working...