Drawing on a pictureBox and save it to a file

racavi1981

New member
Joined
Mar 20, 2008
Messages
1
Programming Experience
Beginner
Hi, I have a question concerning the way a save an image that I just edited.

I'm doing a project for an oftalmologist, where he does an eye exam on the pacient and writes a report. In one of the parts of the exam, the doctor has to draw some points over an image indicating where the patient has an injury.

So far, I'm drawing the points on the image using the PictureBox1_MouseDown event. the question comes when I want to save the image that I just edited.

here is the code of my form.

VB.NET:
Imports System
Imports System.Drawing.Graphics
Imports System.Drawing.Imaging
Imports System.Windows.Forms

Public Class Form1

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

        Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics()
        Dim rectangle As New System.Drawing.Rectangle(e.X, e.Y, 20, 20)
        Dim sf As StringFormat = CType(StringFormat.GenericTypographic.Clone(), StringFormat)

        If RadioButton1.Checked Then
            graphics.FillEllipse(Brushes.Red, rectangle)
        End If

        If RadioButton2.Checked Then
            graphics.FillRectangle(Brushes.Blue, rectangle)
        End If

        If RadioButton3.Checked Then
            sf.Alignment = StringAlignment.Near
            sf.LineAlignment = StringAlignment.Near
            graphics.DrawString(TextBox2.Text, New Font("Arial", 12, GraphicsUnit.Point), Brushes.Black, New RectangleF(e.X, e.Y, 150, 30), sf)
        End If

    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        TextBox1.Text = ("X=" & e.X & "," & "Y= " & e.Y)

    End Sub

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

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        If RadioButton3.Checked Then
            Label1.Visible = True
            TextBox2.Visible = True
        Else
            Label1.Visible = False
            TextBox2.Visible = False
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim im_rot As Bitmap = PictureBox1.Image

        im_rot.Save("C:\Documents and Settings\Rafael Cano\My Documents\Visual Studio 2005\Projects\imagen\imagenes\prueba5.gif")

    End Sub
   
End Class

I uploaded the image I want to draw on to:D
 

Attachments

  • fondo_ojo.gif
    fondo_ojo.gif
    17.3 KB · Views: 105
CreateGraphics is not for drawing. Please review the attached project. It uses GraphicsPath to record user drawings. Paint event paints all recorded drawings. Undo button deletes the last recorded drawing. "SaveAs" menu creates a new bitmap based on the background image and all drawings are reproduced to this, then saved to file.
 

Attachments

  • vbnet20-MouseDraw.zip
    32.3 KB · Views: 190
It would to be better it fi works but it doesn't (

Just only with yuo picture but i need the code for to save the code from captured picture in the piturebox and draw on it and save this is not good idea yuo did.
 
Then i remove your picture it show me like this when draw in extacly place it show nonses

It does not save in correct position it show nonses and it is in not it's right where it has to be.
 
Hello

You can't save what you have drawn directly on a picture box because this is not the graphic, you have to create a graphic of the picture as well as show the picture in the picturebox and anything that you do on the picture box you do on the graphic.

Also you need to have the graphic on the PictureBox the same size as the actual graphic or things will go wrong on the saved graphic.

You will need two globals:

Dim CurrentBitmap as Bitmap
Dim CurrentGraphic as Graphic

Then in the Form_Load:
Initialise the two variables with:

CurrentBitmap = New Bitmap(PictureBox1.Image)
CurrentGraphic = Graphics.FromImage(CurrentBitmap)

Now in the PictureBox1_MouseDown Sub:

Whenever you draw on the PictureBox, you must also draw on the CurrentGraphic
IE:

graphics.FillRectangle(Brushes.Blue, rectangle)
CurrentGraphics.FillRectangle(Brushes.Blue, rectangle)

I would suggest then that you save the graphic as below, this gives the user options but this is up to you.

Dim GraphicDialog As New SaveFileDialog

GraphicDialog.Title = "Save your BackgroundImage picture as...."
GraphicDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
GraphicDialog.Filter = "Jpeg files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"

Dim DialogResult As DialogResult = GraphicDialog.ShowDialog

If DialogResult = Windows.Forms.DialogResult.OK Then
If GraphicDialog.FileName <> String.Empty Then​
'Set to Jpeg by default.
Dim MyImageFormat As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg

If GraphicDialog.FileName.ToString.ToUpper.EndsWith("JPG") Then​
MyImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg​
ElseIf sfd.FileName.ToString.ToUpper.EndsWith("BMP") Then​
MyImageFormat = System.Drawing.Imaging.ImageFormat.Bmp​
End If

'Save the actual graphic object that you have made changes to.
CurrentBitmap.Save(GraphicDialog.FileName, MyImageFormat) 'This is the object you will be saving in the end..

'If you want to refresh th graphic after the save you will need to reinitiate the graphic.
'To make more changes you can just make the change and re save.
End If​
End If

 
Last edited:
Back
Top