Click here to Skip to main content
11,578,954 members (65,495 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
pls, pls , how to draw point on picturebox using vb.net with small example as you can , thinks
Posted 31-Jan-13 1:23am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
Deepu S Nair at 28-Dec-14 5:55am
   
Two solutions for same question?please remove one.Also you are answering old question
Member 10607919 at 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)

  Print Answers RSS
0 Peter Leow 115
1 Frankie-C 115
2 OriginalGriff 113
3 Afzaal Ahmad Zeeshan 85
4 Richard MacCutchan 85
0 OriginalGriff 933
1 Sergey Alexandrovich Kryukov 740
2 Abhinav S 573
3 F-ES Sitecore 420
4 Dave Kreskowiak 419


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 29 Dec 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100