Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

draw on a line image from X-Y coords of mouse click

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
0
36
US
I want to draw a line in a picture box on an image from the point where I click the mouse to 100,100.
I have this code but don't know where to put the picColorKey_Paint sub so it draws when I click the image. currently it draws when the form is loaded.

Code:
Public Class Form2
    Dim XCoord, YCoord As Integer

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim img As Image = Image.FromFile("C:\FindFAST-old2\Office_Depot640x480.gif")
        Dim TempImg As Image = New Bitmap(img.Width, img.Height)
        Dim ImageDrawer As Graphics = Graphics.FromImage(TempImg)
        Dim Fnt As New Font("Tahoma", 24, FontStyle.Bold)
        'ImageDrawer.DrawString("X", Fnt, Brushes.Red, 300, 312)
        PictureBox1.Image = img

    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        Dim img As Image = Image.FromFile("C:\FindFAST-old2\Office_Depot640x480.gif")
        Dim TempImg As Image = New Bitmap(img.Width, img.Height)
        Dim ImageDrawer As Graphics = Graphics.FromImage(TempImg)

        XCoord = e.X
        YCoord = e.Y
        'MsgBox(e.X & " " & e.Y)
        Me.txtXCoord.Text = XCoord.ToString
        Me.txtYCoord.Text = YCoord.ToString

>>> Draw a line on picture box <<<<< when clicking here

    End Sub

    Private Sub picColorKey_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
>>>> ' this code draws a line at load and since XCoord and YCoord are 0 it goes from the upper left hand corner to 100, 100 <<<<
        Dim objPen As Pen
        Dim objPicGraphics As Graphics = e.Graphics

        objPen = New Pen(Drawing.Color.Red, 3)
        objPen.DashStyle = Drawing2D.DashStyle.Solid
        objPicGraphics.DrawLine(objPen, XCoord, XCoord, 100, 100)

    End Sub


End Class

TIA

DougP
[r2d2] < I Built one
 
Do you want only *1* line from [X, Y] to [100,100] to be drawn? Or you want to draw *many* lines (from wherever clicked on to [100,100]) ?
 
one line, but I would like to be able to click many times. so it would draw a line then if you clicked again it would erase that line and draw the next one. or it could leave them,
but need to be able to erase them all at some point.


DougP
[r2d2] < I Built one
 
Try this code:
Code:
Public Class Form2

    Dim XCoord, YCoord As Integer

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim img As Image = Image.FromFile("C:\FindFAST-old2\Office_Depot640x480.gif")
        Dim TempImg As Image = New Bitmap(img.Width, img.Height)
        Dim ImageDrawer As Graphics = Graphics.FromImage(TempImg)
        Dim Fnt As New Font("Tahoma", 24, FontStyle.Bold)
        PictureBox1.Image = img
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        XCoord = e.X
        YCoord = e.Y
        Me.txtXCoord.Text = XCoord.ToString
        Me.txtYCoord.Text = YCoord.ToString
        Me.PictureBox1.Invalidate()
    End Sub

    Private Sub picColorKey_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim objPen As Pen
        Dim objPicGraphics As Graphics = e.Graphics
        objPen = New Pen(Drawing.Color.Red, 3)
        objPen.DashStyle = Drawing2D.DashStyle.Solid
        objPicGraphics.DrawLine(objPen, XCoord, YCoord, 100, 100)
    End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top