how to draw a line on a picture box
-
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
-
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
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
-
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
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
-
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
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
-
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
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]