Home All Groups Group Topic Archive Search About

Save Image from PictureBox :: Drawing VS.net 2003

Author
4 Jan 2007 7:25 AM
prof_martin
Hi, I got problem, kindly please help. Thank you

error message:
An unhandled exception of type 'System.NullReferenceException' occurred in
Test.exe
Additional information: Object reference not set to an instance of an object.

with following code: ---------------------------------------------------------------------------------
Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
        Me.PictureBox1.Location = New System.Drawing.Point(40, 48)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(256, 192)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 264)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 357)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim g As Graphics = e.Graphics
        g.DrawLine(New Drawing.Pen(Color.Red), 1, 1, 200, 200)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        PictureBox1.Image.Save("c:\test.gif")
    End Sub
End Class ---------------------------------------------------------------------------------

Author
4 Jan 2007 11:31 AM
Kevin Spencer
You have not assigned an Image instance to the Image property of the
PictureBox. All you have done is to override the OnPaint method. The Image
property is null.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.

Show quote
"prof_martin" <profmar***@discussions.microsoft.com> wrote in message
news:13213B26-66C7-4C72-8004-13D0AB56E5F1@microsoft.com...
> Hi, I got problem, kindly please help. Thank you
>
> error message:
> An unhandled exception of type 'System.NullReferenceException' occurred in
> Test.exe
> Additional information: Object reference not set to an instance of an
> object.
>
> with following code:
> ---------------------------------------------------------------------------------
> Public Class Form2
>    Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>    Public Sub New()
>        MyBase.New()
>
>        'This call is required by the Windows Form Designer.
>        InitializeComponent()
>
>        'Add any initialization after the InitializeComponent() call
>
>    End Sub
>
>    'Form overrides dispose to clean up the component list.
>    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
>        If disposing Then
>            If Not (components Is Nothing) Then
>                components.Dispose()
>            End If
>        End If
>        MyBase.Dispose(disposing)
>    End Sub
>
>    'Required by the Windows Form Designer
>    Private components As System.ComponentModel.IContainer
>
>    'NOTE: The following procedure is required by the Windows Form Designer
>    'It can be modified using the Windows Form Designer.
>    'Do not modify it using the code editor.
>    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
>    Friend WithEvents Button1 As System.Windows.Forms.Button
>    <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>        Me.PictureBox1 = New System.Windows.Forms.PictureBox
>        Me.Button1 = New System.Windows.Forms.Button
>        Me.SuspendLayout()
>        '
>        'PictureBox1
>        '
>        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
>        Me.PictureBox1.Location = New System.Drawing.Point(40, 48)
>        Me.PictureBox1.Name = "PictureBox1"
>        Me.PictureBox1.Size = New System.Drawing.Size(256, 192)
>        Me.PictureBox1.TabIndex = 0
>        Me.PictureBox1.TabStop = False
>        '
>        'Button1
>        '
>        Me.Button1.Location = New System.Drawing.Point(32, 264)
>        Me.Button1.Name = "Button1"
>        Me.Button1.TabIndex = 1
>        Me.Button1.Text = "Button1"
>        '
>        'Form2
>        '
>        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
>        Me.ClientSize = New System.Drawing.Size(416, 357)
>        Me.Controls.Add(Me.Button1)
>        Me.Controls.Add(Me.PictureBox1)
>        Me.Name = "Form2"
>        Me.Text = "Form2"
>        Me.ResumeLayout(False)
>
>    End Sub
>
> #End Region
>
>    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
> System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
>        Dim g As Graphics = e.Graphics
>        g.DrawLine(New Drawing.Pen(Color.Red), 1, 1, 200, 200)
>    End Sub
>
>    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>        PictureBox1.Image.Save("c:\test.gif")
>    End Sub
> End Class
> ---------------------------------------------------------------------------------
Author
7 Jan 2007 10:13 AM
prof_martin
Is true, does this solve in .net 2 or higher?

Thanks,
Martin

Show quote
"Kevin Spencer" wrote:

> You have not assigned an Image instance to the Image property of the
> PictureBox. All you have done is to override the OnPaint method. The Image
> property is null.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Bit Player
> http://unclechutney.blogspot.com
>
> Where there's a Will, there's a William.
>
> "prof_martin" <profmar***@discussions.microsoft.com> wrote in message
> news:13213B26-66C7-4C72-8004-13D0AB56E5F1@microsoft.com...
> > Hi, I got problem, kindly please help. Thank you
> >
> > error message:
> > An unhandled exception of type 'System.NullReferenceException' occurred in
> > Test.exe
> > Additional information: Object reference not set to an instance of an
> > object.
> >
> > with following code:
> > ---------------------------------------------------------------------------------
> > Public Class Form2
> >    Inherits System.Windows.Forms.Form
> >
> > #Region " Windows Form Designer generated code "
> >
> >    Public Sub New()
> >        MyBase.New()
> >
> >        'This call is required by the Windows Form Designer.
> >        InitializeComponent()
> >
> >        'Add any initialization after the InitializeComponent() call
> >
> >    End Sub
> >
> >    'Form overrides dispose to clean up the component list.
> >    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
> >        If disposing Then
> >            If Not (components Is Nothing) Then
> >                components.Dispose()
> >            End If
> >        End If
> >        MyBase.Dispose(disposing)
> >    End Sub
> >
> >    'Required by the Windows Form Designer
> >    Private components As System.ComponentModel.IContainer
> >
> >    'NOTE: The following procedure is required by the Windows Form Designer
> >    'It can be modified using the Windows Form Designer.
> >    'Do not modify it using the code editor.
> >    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
> >    Friend WithEvents Button1 As System.Windows.Forms.Button
> >    <System.Diagnostics.DebuggerStepThrough()> Private Sub
> > InitializeComponent()
> >        Me.PictureBox1 = New System.Windows.Forms.PictureBox
> >        Me.Button1 = New System.Windows.Forms.Button
> >        Me.SuspendLayout()
> >        '
> >        'PictureBox1
> >        '
> >        Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window
> >        Me.PictureBox1.Location = New System.Drawing.Point(40, 48)
> >        Me.PictureBox1.Name = "PictureBox1"
> >        Me.PictureBox1.Size = New System.Drawing.Size(256, 192)
> >        Me.PictureBox1.TabIndex = 0
> >        Me.PictureBox1.TabStop = False
> >        '
> >        'Button1
> >        '
> >        Me.Button1.Location = New System.Drawing.Point(32, 264)
> >        Me.Button1.Name = "Button1"
> >        Me.Button1.TabIndex = 1
> >        Me.Button1.Text = "Button1"
> >        '
> >        'Form2
> >        '
> >        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
> >        Me.ClientSize = New System.Drawing.Size(416, 357)
> >        Me.Controls.Add(Me.Button1)
> >        Me.Controls.Add(Me.PictureBox1)
> >        Me.Name = "Form2"
> >        Me.Text = "Form2"
> >        Me.ResumeLayout(False)
> >
> >    End Sub
> >
> > #End Region
> >
> >    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
> > System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
> >        Dim g As Graphics = e.Graphics
> >        g.DrawLine(New Drawing.Pen(Color.Red), 1, 1, 200, 200)
> >    End Sub
> >
> >    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Button1.Click
> >        PictureBox1.Image.Save("c:\test.gif")
> >    End Sub
> > End Class
> > ---------------------------------------------------------------------------------
>
>

AddThis Social Bookmark Button