Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pls, pls , how to draw point on picturebox using vb.net with small example as you can , thinks
Posted

VB
Public Class Lines
	Public startPoint As New Point()
	Public endPoint As New Point()

End Class

Private l As New Lines()
Private allLines As New List(Of Lines)()

Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs)
	'collect endPoint when mouse moved
	If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
		l.endPoint = e.Location
		'Line completed
		allLines.Add(l)
		Me.pictureBox1.Invalidate()
	End If

End Sub

Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs)
	'collect startPoint when left mouse clicked
	If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
		l = New Lines()
		l.startPoint = e.Location
	End If
End Sub

Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs)
	For Each aLine As var In allLines
		e.Graphics.DrawLine(Pens.Blue, aLine.startPoint, aLine.endPoint)
	Next
End Sub


Hope this helps
 
Share this answer
 
This is a double bad idea. First, do you really need to draw one point? I doubt it. Usually, you need to draw many points, so drawing anything point by point is prohibitively slow. If you need it, you need to do it only on a bitmap, using this:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

There is a similar approach for WPF, which you are probably not using here.

Another big misconception is using PictureBox. You never ever need to use this control for anything except a static image. In principle you could, but it would be totally redundant, not helping you at all, only wasting your development time, and run-time resources. You need to render some image on a bitmap or on screen. PictureBox is nothing but a middleman for this, to simplify the chores, but only for the simplest situation. I will explain what to do instead, if you need to draw anything on screen, which always means in some control. Draw in this control, nowhere else! Please see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

For more detail, please see:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

As I say, if you really need to draw something point-by-point with many points, you still should better draw on a bitmap (a bitmap, not PictureBox!), and then draw it, if you need, using System.Drawing.Graphics.DrawImage. Note that in this case you should not use double buffering, at least is the situation where this bitmap is all you draw.

—SA
 
Share this answer
 
hello!

Drop A PictureBox on Form
Click and Hold left mouse key to draw points an Right mouse click to Clear!
Copy bellow Code and RUN!!!


Imports System.Drawing.Bitmap

Public Class Form1

Dim bmp As Bitmap

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
End Sub

Private Sub Clear(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
bmp = New System.Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
Me.PictureBox1.Image = bmp
End If
End Sub


Private Sub DrawPoint(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
bmp.SetPixel(e.X, e.Y, Color.Red)
Me.PictureBox1.Image = bmp
End If

End Sub
End Class
 
Share this answer
 
Comments
Deepu S Nair 28-Dec-14 5:55am    
Two solutions for same question?please remove one.Also you are answering old question
Member 10607919 29-Dec-14 3:13am    
dear, Deepu S Nair
It was my recent question and i didn't find a good answer.
So, it was new solution for me!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900