drawing directly onto picturebox




Im making a signature capture and have been looking at examples, got
it working ok but i have a problem

im captureing all the points so i can redraw the image at will which i
have code for in the paint event of a picture box

if i try to redraw the image every time there is a new point in the
image then it works but starts to slow down the point taking and the
image drawing starts to flicker after the first few points.

if i dont refresh the image untill the mouse up event it works
flawlessly but it is not very good usability for soneone trying to
sign the screen as they have to lift the pen to see there progress

i have made an application in a normal win32 app and this code solves
my problem

Dim oldX As Integer
Dim oldY As Integer
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
oldX = e.X
oldY = e.Y
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Not e.Button = Windows.Forms.MouseButtons.Left Then Exit
Dim g As Graphics = PictureBox1.CreateGraphics
g.DrawLine(Pens.Black, oldX, oldY, e.X, e.Y)
oldX = e.X
oldY = e.Y
End Sub

but when i try the same in a compact framework app i get a not
supported exception.

Anyone got any ideas??




Simon Hart [MVP]

The CreateGraphics method is only supported on CF 3.5. Out of interest
instead of re-writting what other have already written, have you looked at
Rob Tiffany's Windows Mobile Accelerator. This set of libraries contains a
signature capture control with source code.


Thanks for your reply Simon.

The finished product will be for sale, so i realy want to make sure
that i am not stealing anyones source for my own gains, hence recoding
it myself!!

All i really need to do is just draw the new segment onto the
picturebox rather than redrawing the entire signature.

I have the code to redraw the entire thing (in the paint event) should
there be any problems with start ment etc forcing a repaint.

I am just struggling to create a graphics element and link it to the
picturebox from outside of the picturebox paint event

i have tried

dim g as graphics = graphics.fromHdc(pbSig.Handle)

which compiles but again gives me an error at runtime.


dim g as graphics = graphics.fromImage(pbSig.image)

Also compiles but does not like the fact my picturebox has no image
assigned to it.

anyone got any ideas how i can add a single line to a picturebox
without having to repaint the entire contents??


Peter Foot

The problem with your code is the pbSig.Handle is a windows handle and
Graphics.FromHDC requires a device context. You could P/Invoke the native
GetDC method in this case pass the window handle and then use the returned
DC to create your Graphics instance.





Thanks for your reply!! the code works fine in normal .net but when i
try it in the compact framework it does not.

I also tried turning on double buffering which is not supported

I have found a solution to my problem.

If i create a global bitmap the size of my picture box then on my
mousemove event just add the line from last point to this point, then
just asign the bitmap to the picture box.

It works very fast and gives me smooth tracing no matter how much is
already there and no matter how fast you move the pen(mouse)

It is far simpler than any of the methods i have found on the web,
doesnt involve overriding anything and you dont have to save a big
list of point either!!!

Thanks for the help guys :)

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question