Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Draw Rectangle/Calculate Coordinates

Posted on 2013-12-26
4
454 Views
Last Modified: 2013-12-27
I have been searching for a few days now and have been unable to find an answer.

I would like to be able to draw rectangle(s) during runtime in my application.

After the rectangle is placed, I am hoping that it would be possible to calculate the coordinates of the corners.

The rectangles will be over a picturebox.

Is this even possible?
0
Comment
Question by:SolveEtCoagula07
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39741345
Well, this is a strange question. Because you are the responsible to draw the rectangles over the picture box, you must know the coordinates before drawing. So, if you know the coordinates, why do you want to calculate them later?

Please, can you explain better what are you trying to do?
0
 
LVL 1

Author Comment

by:SolveEtCoagula07
ID: 39741347
Ah yes....sorry about that.  I want the user to draw it by clicking and then dragging the mouse.
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39741372
Well, even in that case you'll need to control the MouseDown and MouseUp events over the PictureBox, in the MouseDown event you'll need to store the initial coordinate (Point) and in the MouseUp event the final coordinate, so when you draw the rectangle you know exactly the coordinates in which you are drawing it. There's no need to calculate them later.

Calculate the coordinates after drawing is not trivial and depends on the conditions in which the rectangle was drawn. Consider, for example, the rectangle in the rectangle1.png attached file.One-pixel width rectangle black over white.
If you want to calculate the corners in this rectangle you can do easily. Look at the red points (I've enclosed them into circles). Any point in the horizontal lines of the rectangle will have coloured points only to the left and to the right. Also any point in the vertical lines will have coloured points up and below. But the corners are different. For example, the upper-left corner has a coloured point to the right and another one below. So you can (more or less) easily search for the corners looking for these patterns.

But the whole thing complicates if the rectangle is not 1-pixel width, or if it's anti-aliased (not all points have the same color), etc.

Hope that helps.
0
 
LVL 1

Author Closing Comment

by:SolveEtCoagula07
ID: 39742272
Your answer made me think about something and it came to me.  I was able to use the two X and Y coord pairs(top left, bottom right) to calculate the other corners.  I feel that I should be able to complete the rest of this project soon.  Thank  you!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question