0

I am trying to make a drawing app integrated into a program that i am making, but i am having the problem of trying to save the drawing.

Private Sub SaveDrawingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveDrawingToolStripMenuItem.Click
        SaveDrawing.ShowDialog()
        Try
            DrawPad.Image.Save(SaveDrawing.FileName, System.Drawing.Imaging.ImageFormat.Bmp)

        Catch ex As Exception
            MessageBox.Show("Drawing Not Saved")
        End Try
    End Sub

this is what i have so far, but i get my image not saved error every time, when i try commenting out the try statement, i get the following error:
NullRefrenceException was unhandled
Object reference not set to an instance of an object.
Use the "new" keyword to create an object instance.

Another problem that i have, is that i am able to freely draw on my form, but whenever i open a window on top of, or resize the form, everything that i had previously drawn disappears.

The entire code is shown below:

Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class WhiteBoard

    Dim MyPen As New Pen(Color.Black)

    Private Sub SaveDrawingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveDrawingToolStripMenuItem.Click
        SaveDrawing.ShowDialog()
        Try
            DrawPad.Image.Save(SaveDrawing.FileName, System.Drawing.Imaging.ImageFormat.Bmp)

        Catch ex As Exception
            MessageBox.Show("Drawing Not Saved")
        End Try
    End Sub

    Private Sub LoadDrawingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadDrawingToolStripMenuItem.Click
        LoadDrawing.ShowDialog()
        Try
            DrawPad.ImageLocation = LoadDrawing.FileName
        Catch ex As Exception
            MessageBox.Show("Error Occured While Loading")
        End Try
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub NewDrawingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewDrawingToolStripMenuItem.Click
        DrawPad.Image = Nothing
    End Sub

    Private Sub Sizeing_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sizeing.Tick
        DrawPad.Width = Me.Width - 15
        DrawPad.Height = Me.Height - 62
    End Sub


    Private Sub Drawing_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DrawPad.MouseMove
        Static LastPoint As New Point

        If e.Button = Windows.Forms.MouseButtons.Left Then
            DrawPad.CreateGraphics.DrawLine(MyPen, LastPoint.X, LastPoint.Y, e.X, e.Y)
        End If

        LastPoint = New Point(e.X, e.Y)
    End Sub

    Private Sub BackCoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackCoToolStripMenuItem.Click
        PaintBackColor.ShowDialog()
        Try
            DrawPad.BackColor = PaintBackColor.Color
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorBlack.Click
        MyPen.Color = Color.Black
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorBlue.Click
        MyPen.Color = Color.Blue
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorRed.Click
        MyPen.Color = Color.Red
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorGreen.Click
        MyPen.Color = Color.Green
    End Sub


    Private Sub Size1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size1.Click
        MyPen.Width = 1
    End Sub

    Private Sub Size2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size2.Click
        MyPen.Width = 2
    End Sub

    Private Sub Size4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size4.Click
        MyPen.Width = 4
    End Sub

    Private Sub Size6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size6.Click
        MyPen.Width = 6
    End Sub

    Private Sub Size8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size8.Click
        MyPen.Width = 8
    End Sub

    Private Sub Size10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size10.Click
        MyPen.Width = 10
    End Sub

    Private Sub Size12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size12.Click
        MyPen.Width = 12
    End Sub

    Private Sub Size14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Size14.Click
        MyPen.Width = 14
    End Sub

    Private Sub Eraser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Eraser.Click
        MyPen.Color = DrawPad.BackColor
    End Sub

End Class

Any and All help is greatly appreciated
~Matt

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Teme64
0

Use the debugger for the first problem. Set a breakpoint to DrawPad.Image.Save(SaveDrawing.FileName, System.Drawing.Imaging.ImageFormat.Bmp) line (move cursor over the line and press F9 to toggle breakpoint on and off). Run the code until it stops that line. Open the immediate window (press Ctrl+G). In the immediate window, write ? DrawPad and press enter. Write ? DrawPad.Image and press enter.
You should get Nothing from either of the lines (i.e. object). And that object causes "Object reference not set..." error. Then you have to trace your prog and locate the place where you "lose" the object or you haven't initialized it. I hope this helps to solve your first problem.

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.