473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. if (newLeft > 0 || newTop > 0)
  2.  
  3.                                {
  4.                     MessageBox.Show("You are out of region!");
  5.  
  6.                     return;
  7.                  }
  8.  
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:

Expand|Select|Wrap|Line Numbers
  1. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  2.         {
  3.             if (e.Button == System.Windows.Forms.MouseButtons.Left)
  4.             {
  5.                 tempBmp = new Bitmap(FirstImage);
  6.                 float newLeft = ((float)Rect.Left/(float)pictureBox1.Width)*((float)FirstImage.Width);
  7.                 float newRight = ((float)Rect.Right/(float)pictureBox1.Width)*((float)FirstImage.Width);
  8.                 float newTop = ((float)Rect.Top/(float)pictureBox1.Height)*((float)FirstImage.Height);
  9.                 float newBottom = ((float)Rect.Bottom/(float)pictureBox1.Height)*((float)FirstImage.Height);
  10.  
  11.                 RectImage = new Rectangle((int)newLeft, (int)newTop, (int)newRight - (int)newLeft, (int)newBottom - (int)newTop);                
  12.  
  13.                 if (RectImage.Size.Height > FirstImage.Size.Height ) ||
  14.                     RectImage.Size.Width > pictureBox1.Bounds.Left || RectImage.Width > pictureBox1.Bounds.Right)    //bounds are not ok )
  15.                 {
  16.                     MessageBox.Show("You are out of region!");
  17.  
  18.                     return; 
  19.                 }
  20.  
  21.  
  22.  
  23.  
  24.                 int i, j;
  25.                 for (i=RectImage.Left;i<RectImage.Right;i++)
  26.                     for (j = RectImage.Top; j < RectImage.Bottom; j++)
  27.                     {
  28.                         Color c = FirstImage.GetPixel(i, j);
  29.                         tempBmp.SetPixel(i, j, c);
  30.                     }
  31.                 pictureBox1.Image = tempBmp;
  32.                 textBox5.Text = "Right " + RectImage.Right + " Bottom " + RectImage.Bottom;
  33.                 textBox4.Text = "Left " + RectImage.Left + " Top " + RectImage.Top;
  34.  
  35.                 if (FirstImage != null && SecondImage != null)
  36.                 {
  37.  
  38.                     if (this._dontDrawRect == false)
  39.                     {
  40.                         if (MessageBox.Show("Use this Rectangle for comparing?", "Question",
  41.                          MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
  42.                          System.Windows.Forms.DialogResult.Yes)
  43.                         {
  44.                             this._dontDrawRect = true;
  45.                         }
  46.                     }
  47.  
  48.  
  49.                     if (_dontDrawRect)
  50.                     {
  51.                         if (copy_mode == true)
  52.                         {
  53.                             MessageBox.Show("hi");
  54.                         }
  55.                         else
  56.                         {
  57.                             if (ImagesComparion1.ImageComparison(FirstImage, SecondImage, Rect)) 
  58.                             {
  59.                                 textBox1.Text = ImagesComparion1.textbox1;
  60.                                 textBox2.Text = ImagesComparion1.textbox2;
  61.                                 textBox3.Text = ImagesComparion1.textbox3;
  62.                                 MessageBox.Show("identical");
  63.                             }
  64.                             else
  65.                             {
  66.                                 textBox1.Text = ImagesComparion1.textbox1;
  67.                                 textBox2.Text = ImagesComparion1.textbox2;
  68.                                 textBox3.Text = ImagesComparion1.textbox3;
  69.                                 MessageBox.Show("different");
  70.                             }
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.             else if (e.Button == System.Windows.Forms.MouseButtons.Right)
  76.                 this._dontDrawRect = false;
  77.  
  78.         }
  79.  
The IF in this code isnt good too.


Thanks.
May 12 '11 #1
5 2397
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1. if (R2.Left > R1.Left &&
  2.     R2.Right < R1.Right &&
  3.     R2.Top > R1.Top &&
  4.     R2.Bottom < R1.Bottom)
  5. {
  6.   // R1 contains R2
  7. }
That's the math of it, but fortunately there are actually built in methods you can use.

Rectangle.Contains

Rectangle.IntersectsWith
May 12 '11 #2
This is working:

Expand|Select|Wrap|Line Numbers
  1. (e.X > pictureBox1.Size.Width || e.Y > pictureBox1.Size.Height)
  2.  
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.
May 12 '11 #3
GaryTexmo
1,501 Expert 1GB
What would happen if, instead of dragging down and right to draw the rectangle, you dragged up and left?
May 12 '11 #4
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.
May 12 '11 #5
GaryTexmo
1,501 Expert 1GB
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.
May 12 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sachin | last post by:
Please read the code snippets belo snippet 1: Drawing rectangle using Device Context...
0
by: Pramod | last post by:
Hi All, I am working on C# windows Application. I want to draw a rectangle on a richtextbox and write some text inside the rectangle, like this many rectangle should be drawn at the user choice....
4
by: Colin McGuire | last post by:
Hi, this is a really simple question I have been banging my head on a brick wall over. The program below changes the background colour of a form depending on whether the cursor is inside a...
2
by: Lou | last post by:
I need to have a Face,Outline and drop shadow. I am close but can't get my code to work. The face and outline work fine but the shadow is not sized correctly??? Dim rec As New...
3
by: Dennis | last post by:
I create a bitmap of 12 pixels wide x 15 pixels high then create a graphics object from the bit map. When I draw a rectangle g.DrawRectangle(blackpen, new rectangle(0,0,12,15)), I get the top and...
1
by: YYZ | last post by:
Sorry for the multipost, but no one was responding in the other thread. If any solution is forthcoming, I will return to the original thread and post it there as well. I've created a usercontrol...
7
by: Stefan0 | last post by:
Hi, I'd like to draw a circle on the screen using GDI+ I'm using the DrawEllipse method with width = height and I set the SmootingMode to SmoothingModeAntiAlias but the circle is "squared". Is...
6
by: Mythran | last post by:
I have a question regarding the RECT structure required for PInvoke calls. I was reviewing the Rectangle structure (System.Drawing.Rectangle) and it occurred to me that I may be able to use this...
3
by: Ivonne Riedel | last post by:
Working on an incremental drawing algorithm I am facing a problem PInvoking ScrollWindowEx: The code is as follows... C#: public static extern int ScrollWindowEx(IntPtr hWnd, int dx, int...
2
by: Tom C | last post by:
I see a lot of posts about creating a cursor from an image but is it possible to do the opposite? We have added code to allow a text box on a statusbar to be dragged to allow dropping an open...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.