Dot marking, region restriction and get coordinate problem

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
I need the GDI+ code for marking a dot on certain portion of form? In other words, I want to define a width and height of form portion for marking dot, and finally i wanna get the dot x and y coordinte, I need the command for getting coordinate. For example, mark two dots on the defined form portion and retrieve the x, y coordinate for both dots in order for distance calculation between these dots. Besides that, how to restrict a region for marking dots?

I used DrawingGraphics.FillEllipse(DrawingBrush, 30, 100, 5, 5) to draw a circle and in fact it was expected to be a dot due to its size, however, when I put the value to 5, 5, it appeared as a square instead of circle and this was solved with larger values. Hence, any other ways can be used to maintain the values with the proper appearance of the shape. How to trigger an event such as marking a dot on the form?
 
Last edited:
Right, the easier part. To draw a dot..

VB.NET:
e.Graphics.drawline(new pen(color.black,-1),50,50,51,50)
Should give you 1 pixel wide, one pixel high, dot.

Second, you want to define a width and height portion in which to draw your dot, like a region maybe?, or a graphics path? i'd need a bit more info on what it is your trying to do before i could say what would be best. As for the x,y co-ordinates well you'd know what they are as they will be in your draw line method, however i'm going to assume you want to draw these dots at runtime with a mouse click, in which case you'll need to log the position of the mouse at the time it was clicked. Something like the onMouseDown/Up event. You could store these as class level variables.
 
What I intend is mark the dot on a certain region, for example, a canvas with height 600 and width 300, if I click on wherever the area out of the canvas, the mark of the dot won't be trigger. Now I am encoutering the problem of defining the region and mouse click event, how to code these two? I tried to add the command under the mousedown but it didn't support the e.Graphic under mousedown.

In addition, I know how to fill a particular ellipse with black using few lines coding but is there any way to get the same output in one single line?
 
Last edited:
Coordinate values from GDI+ drawing object

After I used GDI+ to mark a dot on the picturebox, I need the x, y coordinates of all of the dots that have been marked for further processing. By using GDI+, the dot would be treat as part of drawing instead of an object, in this case, how to get each coordinate values?
 
I entered the following code under the picturebox paint mouse down event and picturebox paint event.

If e.Button = MouseButtons.Left Then
X = e.X
Y = e.Y
End If
PictureBox1.Invalidate()


Dim Pen As New System.Drawing.Pen(System.Drawing.Color.BlueViolet)
e.Graphics.DrawEllipse(Pen, X, Y, 7, 7)

After that, it was supposed to draw a particular ellipse or a dot on the picturebox and since i declared X and Y as global variables, even i executed the application without performing mouse down, still there was a dot marked at x=0, y=0. Hence i need to know how to solve this problem? And no matter how many times i clicked, only the initial ellipse moved around with the change of mouse pointer coordinates. Actually i need to draw mutiple ellipses by clicking on the picturebox. May i know how to deal with these two problems?
 
Last edited:
I'm working on a solution for you and i'll post the code back tomorrow. But i really have to spend some time with my wife or she'll kick my A**
 
retkehing, please try to keep your act together.. I collected a couple of these posts of yours where you ask about this same thing, duplicate was deleted. Lets keep this specific problem here shall we? This also helps for people to understand the context of your problem.

When you draw a dot at a certain point (xy coordinate) and want to keep the point information, you must then store that point information somewhere for instance in an array. Dealing with arrays is a very basic thing in all programming languages, you find lots of beginners articles about this if you try your favorite search engine. I answered your array thread about this too :)
 
I am at work now so haven't got back with that code yet but the reason you are only getting one ellipse at a time is because you are only drawing one. When you invalidate the picture box it 'erases the canvac' and then you are telling it to draw an ellipse but in a different place. As johnH as quite rightly said you will need to store the x,y co-ordinates from the mouse clicks into an array and then when you call invalidate, loop through the array and paint an ellipse at every element in the array. With me?
 
I once thought of using array but i am just looking for any other methods, for example, a programming way which may treat the ellipse as an object and later retrieve the coordinate by referring to the object position. I think it is the brute force method to store the value in array when large number of ellipses involve in the problem.

The main problem i am currently facing is the non object oriented programming, each ellipse exists as a part of drawing and pre coordinate must be passed to the array. I want to get rid of this to reach time effectiveness of my application because the coordinates will involve in later complicated processing. It is more time effectiveness to retrieve the coordinate value when the process needs it.
 
Dim X, Y as global integers

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
X = e.X
Y = e.Y
End If
PictureBox1.Invalidate()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim Pen As New System.Drawing.Pen(System.Drawing.Color.BlueViolet)
e.Graphics.DrawEllipse(Pen, X, Y, 5, 5)
End Sub

I am having two problem with the above coding. First, only one ellipse appeared wherever i clicked and its position was change according to new X, Y. Besides, since the X, Y was declared as global var, the ellipse would appear initially without clicking. I tried to define the var as local but i have no idea how to pass the X, Y to picture box paint event?
 
Start a new windows application project, add a picturebox to the form, then add the code below. It seems to be what you ask.
VB.NET:
Dim points As New ArrayList
 
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
  Dim Pen As New System.Drawing.Pen(System.Drawing.Color.BlueViolet)
  For Each pt As Point In points
    e.Graphics.DrawEllipse(Pen, pt.X, pt.Y, 5, 5)
  Next
End Sub
 
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  points.Add(New Point(e.X, e.Y))
  PictureBox1.Refresh()
End Sub
 
I need to know how to retrieve the data from the points. I can't use for loop to display the data from the array and it doesn't support GetUpperBound method, e.g.

Dim i As Integer
For i = 0 To points.GetUpperBound
TextBox1.Text += Environment.NewLine + Int(MousePoint(i))
Next
 
Last edited:
I have successfully done it with for loop, but for the ArrayList, i have question on retrieve the item, e.g.
textbox1.text=points.item(1)

but it stores x and y coordinates at the same time, mere value 1 cannot represent two of them.
 
Back
Top