VB.NET help with drawing

Associate
Joined
30 Nov 2004
Posts
214
Location
Oldbury
Hi

i have an image in VB.Net the image is of a maze idunno what code i need for the user to draw a line with the mouse through the maze i ask my teacher at college teaching me programming but he dont know him self :(
 
Teacher doesn't know? :o. What you want to do is load the image into a picture box. Capture the onClick method of the picture box so when a user clicks on the picture the co-ordinates the click occured at are added to a list. Finally everytime the mouse is clicked pass the list of points to a function which draws a line between each one using GDI+.

I don't code vb.net but C# so I cn't give you exact code, but It would be better for you to learn it anyway but ill give you some of it in c# to help.

Firstly create a default image to display in a picturebox, this can be replaced by using a string path of the image you want to display.
Code:
Bitmap b = new Bitmap(picturebox1.Width,picturebox1.Height);
picturebox1.Image = b;

Next you want to capture the Click event, not sure how you do this in VB.Net so I'll let you figure that one, in C# you simple add picturebox1.Click += new EventHandler(pictureBox1_Click); in the form constructor.
Code:
private void pictureBox1_Click(object sender, EventArgs e)
{
     Point p = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)
     p.X -= picturebox.Left;
     p.Y -= picturebox.Top;  
     pointlist.add(p);     
     if (pointlist.Count > 1)   //a line can be drawn
     {
           //draw a line between the point just clicked (lastpoint) and the point preceeding it
           Point lastpoint = pointlist[pontlist.Count-1]
           Point secondlastpoint = pointlist[pontlist.Count-2]
           Graphics.FromImage(pictureBox1.Image).DrawLine(new Pen(Color.Black), secondlastpoint,lastpoint);
     }
}

Not tested but should be something like that.
 
Last edited:
Back
Top Bottom