Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to draw a line on a picture box

how to draw a line on a picture box

Scheduled Pinned Locked Moved Visual Basic
graphicshelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flamingo2
    wrote on last edited by
    #1

    I want to draw a line on a picture box, when mouse key is pressed it will save the current position and when mouse key is released it will draw a line up till that point. but my code is not working, can any body help me out of this. [code] Public Class Form1 Dim x1, y1, x2, y2 As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown x1 = MousePosition.X y1 = MousePosition.Y End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp x2 = MousePosition.X y2 = MousePosition.Y End Sub Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2) End Sub End Class [end code] thanks in advance Fareed

    T C 2 Replies Last reply
    0
    • F flamingo2

      I want to draw a line on a picture box, when mouse key is pressed it will save the current position and when mouse key is released it will draw a line up till that point. but my code is not working, can any body help me out of this. [code] Public Class Form1 Dim x1, y1, x2, y2 As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown x1 = MousePosition.X y1 = MousePosition.Y End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp x2 = MousePosition.X y2 = MousePosition.Y End Sub Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2) End Sub End Class [end code] thanks in advance Fareed

      T Offline
      T Offline
      Thomas Stockwell
      wrote on last edited by
      #2

      It may be best to just create a custom control so that you can manage all the drawing so that it is exactly what you want. The picturebox control is basically only used for displaying pictures and not much else, but a custom control can do what you want and more.

      Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog

      1 Reply Last reply
      0
      • F flamingo2

        I want to draw a line on a picture box, when mouse key is pressed it will save the current position and when mouse key is released it will draw a line up till that point. but my code is not working, can any body help me out of this. [code] Public Class Form1 Dim x1, y1, x2, y2 As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown x1 = MousePosition.X y1 = MousePosition.Y End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp x2 = MousePosition.X y2 = MousePosition.Y End Sub Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2) End Sub End Class [end code] thanks in advance Fareed

        C Offline
        C Offline
        C1AllenS
        wrote on last edited by
        #3

        Hello, You can try the given code. BEGIN CODE Dim MD, MU As Point Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown MD = e.Location End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then MU = e.Location DrawLine() End If End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp MU = e.Location DrawLine() End Sub Public Sub DrawLine() Dim g As Graphics g = Me.PictureBox1.CreateGraphics g.Clear(Me.PictureBox1.BackColor) g.DrawLine(Pens.Blue, MD, MU) g.Dispose() End Sub I hope this will help. Regards,

        Allen Smith ComponentOne LLC www.componentone.com

        F 1 Reply Last reply
        0
        • C C1AllenS

          Hello, You can try the given code. BEGIN CODE Dim MD, MU As Point Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown MD = e.Location End Sub Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then MU = e.Location DrawLine() End If End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp MU = e.Location DrawLine() End Sub Public Sub DrawLine() Dim g As Graphics g = Me.PictureBox1.CreateGraphics g.Clear(Me.PictureBox1.BackColor) g.DrawLine(Pens.Blue, MD, MU) g.Dispose() End Sub I hope this will help. Regards,

          Allen Smith ComponentOne LLC www.componentone.com

          F Offline
          F Offline
          flamingo2
          wrote on last edited by
          #4

          hy thnx Allen, your code works fine . but i also want to draw a rectangle on picture box and for this i need to know the quardinate of mouse down and up position. I also want to use freehand drawer, i dont have any idea abt it. Thanks for the solution again. Fareed

          F 1 Reply Last reply
          0
          • F flamingo2

            hy thnx Allen, your code works fine . but i also want to draw a rectangle on picture box and for this i need to know the quardinate of mouse down and up position. I also want to use freehand drawer, i dont have any idea abt it. Thanks for the solution again. Fareed

            F Offline
            F Offline
            flamingo2
            wrote on last edited by
            #5

            I am trying to draw rectangle but it is drawn just with a single click (with different lengths and widths) also it is going out of pictureBox area But i want it to start from where mouse key is pressed and end where mouse key is released. i think there is something wrong with my quardinate. Waiting for reply. Thanks [code] Dim x1, y1, x2, y2 As Integer Dim MD, MU As Point Dim mywidth As Integer Dim drawtool As String Dim drawpen As Pen Dim g As Graphics Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown MD = e.Location x1 = MD.X y1 = MD.Y End Sub Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp If e.Button = Windows.Forms.MouseButtons.Left Then MU = e.Location x2 = MD.X y2 = MD.Y DrawRectangle() End Sub Public Sub DrawRectangle() gr = PictureBox1.CreateGraphics Dim solidColorBrush As SolidBrush = New SolidBrush(Color.Red) Dim coloredPen As Pen = New Pen(solidColorBrush) coloredPen.Width = 3 coloredPen.Color = Color.Blue Dim drawArea2 As Rectangle = New Rectangle(x1, y1, x2, y2) gr.DrawRectangle(coloredPen, drawArea2) gr.Dispose() End Sub [End code]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups