Saving to file an image drawn on a PictureBox

  • Thread starter Thread starter Harry Mendryk
  • Start date Start date
H

Harry Mendryk

Guest
I am working on an application that draws an image based on user inputs. The drawing works fine but I also want to save the image to a tiff or other image format file. However when I do so I get an error "An unhandled exception of type 'System.NullReferenceException' occurred in Ammonite Model.exe Additional information: Object reference not set to an instance of an object." And in fact no file gets save. I do plan to use a SaveDialog box to perform the actual saving, but I am using the following code until I get it to work:

Private Sub DrawAxis()
Dim Screen As Position
Dim point1 As New Point
Dim point2 As New Point
YCenter = PictureBoxAmmonite.Size.Height / 2
XCenter = PictureBoxAmmonite.Size.Width / 2
PictureBoxAmmonite.Refresh()
Dim g As Graphics = PictureBoxAmmonite.CreateGraphics()
' Create pen.
Dim blackPen As New Pen(Color.Black, 1)
Dim bluePen As New Pen(Color.Blue, 1)
Dim redPen As New Pen(Color.Red, 1)

Call Angle(40, 0, 0, Screen) ' X-axis
point1.X = XCenter - DisplayMagnify * Screen.X : point1.Y = YCenter + DisplayMagnify * Screen.Y
point2.X = XCenter + DisplayMagnify * Screen.X : point2.Y = YCenter - DisplayMagnify * Screen.Y
g.DrawLine(blackPen, point1, point2)

Call Angle(0, 40, 0, Screen) ' Y-axis
point1.X = XCenter - DisplayMagnify * Screen.X : point1.Y = YCenter + DisplayMagnify * Screen.Y
point2.X = XCenter + DisplayMagnify * Screen.X : point2.Y = YCenter - DisplayMagnify * Screen.Y
g.DrawLine(bluePen, point1, point2)

Call Angle(0, 0, 40, Screen) ' Z-axis
a point1.X = XCenter - DisplayMagnify * Screen.X : point1.Y = YCenter + DisplayMagnify * Screen.Y
point2.X = XCenter + DisplayMagnify * Screen.X : point2.Y = YCenter - DisplayMagnify * Screen.Y
g.DrawLine(redPen, point1, point2)

PictureBoxAmmonite.Image.Save("C:\Ammonite Model\Images\test.tif", System.Drawing.Imaging.ImageFormat.Tiff)

g.Dispose()
End Sub

Continue reading...
 
Back
Top