Advice needed for drawing program

M

Martin Horn

Hi all,

I'm adding basic drawing capability to an application that I am writing, and
I have been having problems with making the graphics output persist when the
application is re-sized,covered etc.

This is the solution I have come up with, but it seems a bit convoluted.
Before I get too far into the development of the program, could someone cast
an eye over my code and tell me if I have approached it in the correct way,
or suggest a better solution.

Many thanks,

Martin Horn. (See below for my code)

Imports System.Windows.Forms

Public Class ComplexInscriptionDesignerForm
Private Drawing As Boolean = False
Private OldX As Integer = 0
Private OldY As Integer = 0
Private PaintObject As Object
Private CanvasBM As System.Drawing.Bitmap
Private Canvas As Graphics

Private Sub PicCanvas_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicCanvas.MouseDown

Drawing = True
OldX = e.X
OldY = e.Y
End Sub

Private Sub PicCanvas_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicCanvas.MouseMove

If Drawing Then
Canvas.DrawLine(Pens.Black, OldX, OldY, e.X, e.Y)
OldX = e.X
OldY = e.Y
PicCanvas.Invalidate()
End If
End Sub

Private Sub PicCanvas_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PicCanvas.MouseUp

Drawing = False
End Sub

Private Sub PicCanvas_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PicCanvas.Paint

e.Graphics.DrawImageUnscaled(CanvasBM, 0, 0)
End Sub

Private Sub ComplexInscriptionDesignerForm_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

CanvasBM = New System.Drawing.Bitmap( _
PicCanvas.ClientSize.Width, _
PicCanvas.ClientSize.Height)

Canvas = Graphics.FromImage(CanvasBM)
End Sub
End Class
 
A

Armin Zingler

Martin Horn said:
I'm adding basic drawing capability to an application that I am
writing, and I have been having problems with making the graphics
output persist when the application is re-sized,covered etc.

This is the solution I have come up with, but it seems a bit
convoluted. Before I get too far into the development of the
program, could someone cast an eye over my code and tell me if I
have approached it in the correct way, or suggest a better solution.

The approach is really good, IMO. I guess the line shouldn't be drawn before
the mouse button is released. Therefore I'd store the line coordinates
(start and end) in variables and paint the line in PicCanvas_Paint (if
drawing=true) additionally to CanvasBM. When the button is released, the
line can be drawn persistently on the bitmap. Thus, I'd write a sub drawing
the line that can be called in mouse-up as well as in piccanvas_paint.

Armin
 
M

Martin Horn

Hi Armin,

thanks for the feedback and tips. It's the first time I've tried something
like this with VB.Net and it's reassuring to know that I'm starting off in
the right direction.

Kind regards,

Martin Horn.
 

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

Top