Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox
Drawing a point on a particular picbox Drawing a point on a particular picbox
Drawing a point on a particular picbox
Go Back  Xtreme Visual Basic Talk > > > Drawing a point on a particular picbox


Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2012, 08:19 PM
celtic celtic is offline
Centurion
 
Join Date: Mar 2009
Posts: 132
Default Drawing a point on a particular picbox


Hi all.

I have several picture boxes on a form (frmMain).
My question is, how do I paint a point on a particular picture box at the location specified by the coordinates of the current mouse position? The snippet below shows defining some canvases and pens. But I'm not sure how to capture the mouse position when the mouse is over a particular picture box and how to paint the actual point at that location.
Any help would be appreciated.

Code:
' In a code module I:

' defined graphic objects
Public graph1 As Graphics
Public graph2 As Graphics

' defined some pens
Public thinPen, mediumPen, thickPen As Pen

' created the graphic objects and assigned pens
Gpic1 = frmMain.picBox1.CreateGraphics
Gpic2 = frmMain.picBox2.CreateGraphics

thinPen = New Pen(Color.Black, 1)
mediumPen = New Pen(Color.Black, 3)
thickPen = New Pen(Color.Black, 5)
Reply With Quote
  #2  
Old 02-03-2012, 03:23 AM
DrPunk's Avatar
DrPunkDrawing a point on a particular picbox DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Starting very simply, you have the MouseMove event of a control that gives you the position of the cursor over that control.

So, given a picturebox on a form, we can draw the points that the mouse moves over the control like...

Code:
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    ' Create the graphics to use from the picturebox so that we draw on the picturebox
    Dim g As Graphics = PictureBox1.CreateGraphics
    ' Draw a 1x1 (i.e. 1 pixel) box where the mouse is
    ' The parameter e passed to this event has the location of the mouse
    g.DrawRectangle(Pens.Black, e.Location.X, e.Location.Y, 1, 1)
    ' Clean up
    g.Dispose()
End Sub
Like I said, this is very simple. There's no ON/OFF for the drawing and you'll notice it doesn't react to quick mouse movement very well, but it's a starting point to progress from.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 02-03-2012, 04:29 PM
passel's Avatar
passelDrawing a point on a particular picbox passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Several reasons the code you've started with will have problems.
Since you're using CreateGraphics to "cache" a graphics object to draw directly on the picturebox, the valid lifetime of that graphics object is indeterminate, and will go bad after awhile, as the picturebox's current graphics object is subject to change at any time.

DrPunk's code will work because it creates a new Graphics object from the Picturebox's object at the time he is going to draw, so it is also current at that time, and then disposes of it before leaving the sub routine, but since drawing on a picturebox is not persistent, and Windows, as a kludge I think, periodically sends paint events to all windows, the graphics you draw will be wiped out. Creating and disposing of a graphics object continually also slows things down quite a bit.
The natural place to draw into a picturebox (or any control) is in the Paint event of the control itself. It will pass the current Graphics object to you so you draw using a current object and without having to create it yourself.

To support persistent drawing you need to manage your own bitmap so you have control over it. Once you create your own bitmap and create a Graphics object from it so you can draw regularly on it, the paint event of the picturebox would just be used to transfer the image from the bitmap to the picturebox. There are several ways to do that, but for initial simplicity, just use drawimage to transfer the image from the bitmap to the picturebox's screen area.

Example using one picturebox (and bitmap). It will draw contiguous lines, only when the left mouse button is down. The bitmap is created when the picturebox's client rectangle is resized. If created in the Form_Load, the picturebox may or may not yet exist, and may not report its final size. Putting the code here, should avoid those issues, and if the size is changed or reported more than once on startup, the old objects will be disposed of and new ones created, so the bitmap should reliably match the size of the picturebox's client area.
Code:
Public Class Form1
  Private pic1bmp As Bitmap
  Private gPic1 As Graphics

  Private Sub PictureBox1_ClientSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.ClientSizeChanged
    Dim p As PictureBox = DirectCast(sender, PictureBox) 'convert the general object into a specific class object

    If pic1bmp IsNot Nothing Then  'If we created a bitmap before
      pic1bmp.Dispose()            '  Dispose of it, 
      gPic1.Dispose()              '  and the graphics object used to access it
    End If
    pic1bmp = New Bitmap(p.ClientSize.Width, p.ClientSize.Height)  'Create a new bitmap
    gPic1 = Graphics.FromImage(pic1bmp)                            'and a graphics object to access it
  End Sub

  Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    If pic1bmp IsNot Nothing Then             'If we have a bitmap then
      e.Graphics.DrawImage(pic1bmp, 0, 0)     '  draw it on the picturebox
    End If
  End Sub

  Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    Static lPos As Point                                 'track the last position of the mouse
    If e.Button = Windows.Forms.MouseButtons.Left Then   'If the left mouse button is down then
      gPic1.DrawLine(Pens.Red, lPos, e.Location)         '  Draw a line from the last position to this one.
      PictureBox1.Invalidate()                           '  Have the picturebox redraw itself, when convenient
    End If
    lPos = e.Location                                    'Save the current location so we can draw contiguous lines
  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #4  
Old 02-24-2012, 01:35 PM
celtic celtic is offline
Centurion
 
Join Date: Mar 2009
Posts: 132
Default

Thank you very much Dr Punk and passel!

I have used your suggestions and code examples to good success.

Sorry, I had thought that I had responded to your helpful posts already
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox Drawing a point on a particular picbox
Drawing a point on a particular picbox
Drawing a point on a particular picbox
 
Drawing a point on a particular picbox
Drawing a point on a particular picbox
 
-->