CharlieRussell

I have the following code which allows freehand drawing using a mouse. It uses a panel as drawing surface. I will add the ability to load background images on the panel so can draw on that bmp.

Now after all the drawing, I want to ensure its one image, a bmp, copy to clipboard then paste it into a richtextbox on another form in the program.

Now attempting this I have tried several approaches. None have worked at all.

The online tutorials or samples to study I can find are many years old and many version of basic ago. None work of course.

Here is my base code for drawing.

Any advice, or directing me to a tutorial, sample, or a reference book would be greatly appreciated.

From what I read, this should work. There was some literatur eon not drawing to a control but rather to a bmp using the controls paint event. This was what I could get to work for drawing.

Imports System.Math

Public Class Form1

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)

Private Const MOUSEEVENTF_MOVE As Integer = &H1 ' mouse move

Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2 ' left button down

Private Const MOUSEEVENTF_LEFTUP As Integer = &H4 ' left button up

Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8 ' right button down

Private Const MOUSEEVENTF_RIGHTUP As Integer = &H10 ' right button up

Private Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20 ' middle button down

Private Const MOUSEEVENTF_MIDDLEUP As Integer = &H40 ' middle button up

Private Const MOUSEEVENTF_WHEEL As Integer = &H800 ' wheel button rolled

Private Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000 ' absolute move

Private m_Drawing As Boolean = False

Private m_LastPoint As Point = Nothing

' Start drawing by hand using mouse.

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown

m_Drawing = True

m_LastPoint = New Point(e.X, e.Y)

Dim gr As Graphics = Panel1.CreateGraphics()

End Sub

' Continue drawing.

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove

If Not m_Drawing Then Exit Sub

Dim gr As Graphics = Panel1.CreateGraphics()

gr.DrawLine(Pens.Blue, m_LastPoint, New Point(e.X, e.Y))

m_LastPoint = New Point(e.X, e.Y)

End Sub

' Stop drawing.

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp

m_Drawing = False

End Sub

¡®to refresh the panel, i.e start over

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Panel1.Refresh()

End Sub

¡® exit program

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.Close()

End Sub

End Class

Something like this can be added later to load a bmp as background image for the panel.

' panel1.BackgroundImage = My.Resources.FACE

¡® or could use this next line to let the panel size to loaded bmp size

' panel1.Size = panel1.BackgroundImage.Size



Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

nobugz

Hmm, looks familiar. Drawing into a bitmap is indeed the best way to go. Here's a sample program:

Public Class Form1
Private mBmpSrce As Image
Private mBmpDest As Image
Private mDrawing As Boolean
Private mLastPoint As Point

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mBmpSrce = Bitmap.FromFile("c:\temp\test.bmp")
mBmpDest = New Bitmap(mBmpSrce)
End Sub

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
mDrawing = Not mDrawing
If Not mDrawing Then
'--- Drawn the line, make mBmpSrce the new source image
mBmpSrce = CType(mBmpDest.Clone(), Image)
End If
mLastPoint = e.Location
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If Not mDrawing Then Return
Dim gr As Graphics = Graphics.FromImage(mBmpDest)
gr.DrawImage(mBmpSrce, 0, 0)
gr.DrawLine(Pens.Blue, mLastPoint, e.Location)
gr.Dispose()
Invalidate()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(mBmpDest, 0, 0)
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.OK Then Return
mBmpDest.Save(SaveFileDialog1.FileName)
End Sub

Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
Clipboard.SetImage(mBmpDest)
End Sub
End Class






Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

CharlieRussell

Thanks, that taught me alot.

 

This is what I have now and it works fine.

 

Although I cant get the bmp off the top left corner of the form.

 

location doesnt sem to work. I tried asisgning a new point and didnt work either.

 

 

Public Class Form1

Private mBmpSrce As Image

Private mBmpDest As Image

Private mDrawing As Boolean = False

Private mLastPoint As Point = Nothing

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

' line to load image from directory

' mBmpSrce = Bitmap.FromFile("c:\test.bmp")

' load image from resources

mBmpSrce = My.Resources.Canvas

mBmpDest = New Bitmap(mBmpSrce)

 

' mBmpDest = New Point(Me.Right + 100, 100)

 

 

 

 

 

 

 

End Sub

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown

mDrawing = True

mLastPoint = New Point(e.X, e.Y)

Dim gr As Graphics = Graphics.FromImage(mBmpDest)

 

End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove

If Not mDrawing Then Return

Dim gr As Graphics = Graphics.FromImage(mBmpDest)

 

gr.DrawImage(mBmpDest, 0, 0)

gr.DrawLine(Pens.Blue, mLastPoint, New Point(e.X, e.Y))

mLastPoint = New Point(e.X, e.Y)

gr.Dispose()

Invalidate()

End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

mDrawing = False

End Sub

 

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

e.Graphics.DrawImage(mBmpDest, 0, 0)

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.OK Then Return

mBmpDest.Save(SaveFileDialog1.FileName)

End Sub

Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click

Clipboard.SetImage(mBmpDest)

End Sub

 

Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

mBmpSrce = My.Resources.Canvas

mBmpDest = New Bitmap(mBmpSrce)

Invalidate()

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

Application.Exit()

End Sub

End Class





Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

nobugz

Change the 2nd and 3rd argument of DrawImage in the Paint event. You need to adjust the points in the DrawLine() call to compensate for the offset.





Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

CharlieRussell

i tried that and it did adjust where the bmp displayed. but even altering the linedraw to match. the lines now draw to the right of the mouse cursor :(

thx for your help, appreciate it.





Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

CharlieRussell

At last I found a reference.

It took this:

gr.TranslateTransform(-100, 0)

' assuming: e.Graphics.DrawImage(mBmpDest, 100, 0)

gr.DrawLine(Pens.Blue, mLastPoint, New Point(e.X, e.Y))

mLastPoint = New Point(e.X, e.Y)

to alter placement transformation from world to page co-ordinates

I was unaware there are 3 layers so to speak for co-ordinates.

link: http://msdn2.microsoft.com/en-us/library/aa1hw2kk.aspx

again, I thank you for the teaching.





Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

prog.gabi

I came in in the middle of this discussion, being referred to it from another thread. I am happy with the code given by nobugz on 10-21-2006 12:06 PM UTC. My favorite language is actually C#, but I had no trouble translating, which gave me the same well-functioning program. My problem is, rather, that the situation I am dealing with in the application I am working on has the graphics in a separate panel rather than the main form. I thought that this would require only a minor change, but after two days I have not been able to do this, so I wonder if someone could assist me I would be most grateful!



Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

nobugz

A form and a panel are pretty close to each other. Give us an idea what you have a problem with...





Re: Visual Basic Express Edition Copy image/graphic from panel/drawing surface to richtextbox

prog.gabi

Here is my situation: I began with the code given above by nobugz on 10-21-2006 12:06 PM UTC, translated into C# (by the way: just to make sure, I first compiled it as it was in VB and ran it, to make sure I understood what it was supposed to do; my translation then worked identically, so I know that part is OK). Then my first effort was simply to replace all the occurrences of "Form1" with "Panel1" (I had added a panel by that name to the original control). I could not do that exactly because the panel control does not have a "Load" event; but it seemed to me that the content of the original Form1_Load handler simply contained one-time events without reference to any particular control, so I left that handler as is. Otherwise, I did nothing other than renaming all the Form1 handlers to Panel1 handlers, and associating them with the corresponding Panel1 events.

Oops! As I was describing this to you, it occurred to me that the instruction "Invalidate()" should probably now be replaced by "Panel1.Invalidate()" and the progarm began to function perfectly (at least as far as I can see now)! Thank you very much for asking me the simple Socratic question!