none
Draw line in vb.net RRS feed

  • Question

  •     Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
            Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255), 8)
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Custom
            pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor
            e.Graphics.DrawLine(pen, e.x, e.y, e.x + 10, e.y + 15)
    end sub
    

    Hi Everyone:

    I am using VB.Net 2019.  I have a picturebox with its image set so some picture.  I need to draw a line on top of the image, at the location when the user clicks the mouse (x,y) to (x+10,y+15).  However, I am not exactly how to do this.  I have read that I have to place the actually drawing of the line inside the Paint event, but in there, there is no x or y of the mouse.  and if I try to put it draw inside the click even, I get an error as the e.Drawing does not exist in the click event.  Here is my code.  Of course the last line is full of errors.  Thanks;

    Wednesday, June 5, 2019 11:52 PM

Answers

  • Hi,

    use Picturebox's MouseClick event

      Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
            Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255), 8)
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Custom
            pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor
            Dim gp As Graphics = PictureBox1.CreateGraphics
            gp.DrawLine(pen, e.X, e.Y, e.X + 10, e.Y + 15)
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex Li-MSFT Thursday, June 6, 2019 7:53 AM
    • Marked as answer by booboo_US Friday, June 7, 2019 1:37 AM
    Thursday, June 6, 2019 7:53 AM

All replies

  • Hi

    Here is one way.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		PictureBox1.Image = Image.FromFile("C:\Users\lesha\Documents\VB Resources\Images\Abstract II.jpg")
    	End Sub
    	Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    
    		Static p As Point = New Point
    		If Not p = UserClicked Then p = UserClicked
    
    		e.Graphics.DrawLine(New Pen(Brushes.White, 5), p, New Point(p.X + 10, p.Y + 15))
    	End Sub
    	Dim UserClicked As New Point
    	Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    		UserClicked = e.Location
    		PictureBox1.Invalidate()
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, June 6, 2019 12:27 AM
  • Hi Bob,

    I've made a post just before.
    https://social.msdn.microsoft.com/Forums/en-US/b06083af-c9e6-46ed-b6c4-5f0b21a8df94/drag-and-drop-of-pictures?forum=vbgeneral
    Does this help you? 

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, June 6, 2019 1:07 AM
  • Hi,

    use Picturebox's MouseClick event

      Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
            Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255), 8)
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Custom
            pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor
            Dim gp As Graphics = PictureBox1.CreateGraphics
            gp.DrawLine(pen, e.X, e.Y, e.X + 10, e.Y + 15)
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex Li-MSFT Thursday, June 6, 2019 7:53 AM
    • Marked as answer by booboo_US Friday, June 7, 2019 1:37 AM
    Thursday, June 6, 2019 7:53 AM
  • Thank you Alex.
    Friday, June 7, 2019 1:37 AM