473,467 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

69 New Member
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 2399
GaryTexmo
1,501 Recognized Expert Top Contributor
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
Chocolade
69 New Member
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 Recognized Expert Top Contributor
What would happen if, instead of dragging down and right to draw the rectangle, you dragged up and left?
May 12 '11 #4
Chocolade
69 New Member
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 Recognized Expert Top Contributor
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.