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: -
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: -
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.
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... - 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
This is working: -
(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.
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?
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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
|
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...
| |
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...
|
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
|
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
|
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);
|
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?
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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();...
| |
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...
|
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
| |