How to load an image, draw on it with mouse like in paint prog and then save?

I have been reading this forum for the past few days. What i need is during run-time, to allow user to load an image, using his mouse to draw on it exactly like what he can do in paint programme.... a bit of scribble here and there and finally to save the completed picture.

So far after much searching around, I'd only manage to find the closest match with the following code, however, it only draw solid line and doesn't draw according to the path of mouse moved.

Appreciate if anyone can help, thanks!

Public Class Form6
Private mBmpSrce As Image
Private mBmpDest As Image
Private mDrawing As Boolean
Private mLastPoint As Point
Private Sub Form6_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 Form6_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 Form6_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 Form6_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(mBmpDest, 0, 0)
End Sub
End Class

[1775 byte] By [Apple123] at [2008-1-3]
# 1

The following code should get you started:

Code Snippet

Public Class Form1

Dim MD As Boolean

Dim LastPos As Point

Dim P As Pen = New Pen(Color.Red, 1)

Private Sub PictureBox1_MouseDown(ByVal sender As Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles PictureBox1.MouseDown

MD = True

LastPos = e.Location

End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles PictureBox1.MouseMove

If Not MD Then Return

If PictureBox1.Image Is Nothing Then Return

Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)

g.DrawLine(P, LastPos, e.Location)

PictureBox1.Invalidate()

LastPos = e.Location

End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles PictureBox1.MouseUp

MD = False

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

Try

PictureBox1.Image = New Bitmap(OpenFileDialog1.FileName)

Catch

MsgBox("Couldn't load image.")

End Try

End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button2.Click

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

PictureBox1.Image.Save(SaveFileDialog1.FileName)

End If

End Sub

End Class

You need a Form with a two Buttons, a PictureBox, an OpenFileDialog and a SaveFileDialog.

JohnWein at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

John, thank you so much!

It's exactly what I am looking for.

Apple123 at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Another extension to this question.

I would want to open a file for user to make amendments by drawing on it. Afterwhich, I would wan to overwrite and save the file back to same path and file name. I wanted to delete the original file and then save the most current one.

Code:

Kill(Filename)

picDisplay.image.save(fFilename)

I keep getting an error msg below:

The process cannot access the file "C:\Data\20070101.jpg" because it is being used by another process.

Any other way that I can get around it?

Thanks!

Apple123 at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

Change the Button2_Click event to:

Code Snippet

Private Sub Button2_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button2.Click

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

Try

PictureBox1.Image.Save(SaveFileDialog1.FileName)

Catch ex As Exception

Dim FN As String = SaveFileDialog1.FileName & "tmp"

PictureBox1.Image.Save(FN)

PictureBox1.Image.Dispose()

Kill(SaveFileDialog1.FileName)

Rename(FN, SaveFileDialog1.FileName)

End Try

End If

End Sub

JohnWein at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

thanks

Apple123 at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

Thanks. I tried. However, I am still getting the same error message. Below is my code:

Dim FN As String = FileName & "tmp"
If My.Computer.FileSystem.FileExists(FileName) = True Then
If MsgBox("File already existed. Do you want to replace?", vbYesNo) = vbYes Then
' picDisplay.= Nothing

' Kill(FileName)
picDisplay.Image.Save(FN)
picDisplay.Dispose()
picDisplay = Nothing
Kill(FileName) '*****************error msg here
Rename(FN, FileName)
End If
End If

I wonder why? any suggestions?

Apple123 at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7
Your code is the same as your previous code. Why don't you want to use the code I supplied?
JohnWein at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

Hi John,

I copied exactly your codes and it still gives me error on the line:

Kill(SaveFileDialog1.Filename)

'The process cannot access the file "c:\temp\533.jpg" because it is being used by another process.

Any suggestion on how I can overcome this?

Apple123 at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

When you load the file into the PictureBox, load it using a FileStream. See:

http://support.microsoft.com/default.aspx/kb/309482

JohnWein at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10

John,

Thanks, it works now! But it is strange why by calling picturebox.dispose doesn't release the file so that we can overrite it.

Anyhow, using FileStream works now.Thank so much!

Apple123 at 2007-9-25 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...