473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ 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 2415
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.Conta ins

Rectangle.Inter sectsWith
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
1228
by: sachin | last post by:
Please read the code snippets belo snippet 1: Drawing rectangle using Device Context ----------------------------------------------------------------[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll") private static extern bool Rectangle IntPtr hdc int ulCornerX, int ulCornerY int lrCornerX, int lrCornerY) public void GetHdcForGDI(PaintEventArgs e
0
1856
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. Now there are two RichTextboxes and when the user draws the Rectangle on first RichTextbox and extends it by holding on if it's end it should be able to be continued on the next RichTextBox. i dont know whether this is possible, but if anyone...
4
2673
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 rectangle drawn on the form or not. It works perfectly as shown below. But it won't work if I change the values of scaleFactor, rotateFactor, translateFactorX, translateFactorY in the program. I would like to 'correct' the values of e.X and e.Y in...
2
13965
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 Rectangle(PictureBox1.Left, PictureBox1.Top, PictureBox1.Height, PictureBox1.Height) Try 'Set the Font
3
1662
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 side but the far side and bottom are not drawn. If I draw a rectangle g.DrawRectangle(blackpen, new rectangle(0,0,11,14) then all four sides are drarwn. I am having trouble understanding why I can't get a rectangle 12 pixels wide by 15 pixels...
1
1712
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 and I'm trying to emulate the look of a mac effect (just for showing some options to my boss) by using 2 gradient rectangles. I'm having a LOT of trouble doing this, however, because I keep getting a 1 or 2 pixel line showing up. Can someone...
7
2132
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 there a way to draw a "smoothed" circle ? Thanks in advance Stefano
6
23060
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 structure in place of defining a RECT structure. I would create the RECT structure definition the same as the Rectangle structure is defined. Is there any problems with using Rectangle in place of RECT for the API functions? Thanks, Mythran
3
4826
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 dy, IntPtr scrollRect, IntPtr clipRect, IntPtr hrgn, ref Rectangle updateRect, uint flags);
2
2987
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 window on another icon, grid, etc. It would be very intuitive for the user if we could grab the default cursor and render this as an image next to the text box. Anyone have any ideas how this could be done?
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.