I'm drawing onto a PictureBox, using the code

Dim XPrev, YPrev, XCur, YCur As Integer

    Private Sub picCanvas_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
        'Setting the previous points for line drawing
        XPrev = e.X
        YPrev = e.Y
    End Sub

    Private Sub picCanvas_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
        'Setting the current points for line drawing
        XCur = e.X
        YCur = e.Y

        'Graphics Variables
        Dim p As New Pen(Color.Red, 10)
        Dim g As Graphics = picCanvas.CreateGraphics

        g.DrawLine(p, XPrev, YPrev, XCur, YCur)
    End Sub

Although i have no idea how to save this. I've tried picCanvas.Image.Save("C:\img.png", Imaging.ImageFormat.Png) , but i get a null reference exception.
any help would be appreciated,

8 Years
Discussion Span
Last Post by Teme64

It seems that picCanvas.Image is nothing (your code probably works fine until you refer to Image object). You can test that:

If picCanvas.Image Is Nothing Then
  MessageBox.Show("No image object", "Error", MessageBoxButtons.OK)
  picCanvas.Image.Save("C:\img.png", Imaging.ImageFormat.Png)
End If

If this is the case, initialize PictureBoxes Image object:

picCanvas.Image = Image.FromFile("C:\MyCanvas.png")

If you do not want any predefined image, following code might work better for you:

Dim NewCanvas As Bitmap
NewCanvas = New Bitmap(200, 200)
picCanvas.Image = Image.FromHbitmap(NewCanvas.GetHbitmap)

Now you can set Image's size, color etc. attributes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.