none
Saving to file an image drawn on a PictureBox RRS feed

  • Question

  • 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

    Saturday, February 2, 2019 10:29 PM

All replies

  • Hello,

    Set a breakpoint before the error occurs then step through the code while hovering over variables to determine which line has a null value. Figure out the issue or come back here and let us know which line caused the error.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Harry Mendryk Sunday, February 3, 2019 12:02 AM
    • Unmarked as answer by Harry Mendryk Sunday, February 3, 2019 12:02 AM
    Saturday, February 2, 2019 10:52 PM
    Moderator
  • The break is at the PictureBoxAmmonite.Image.Save("C:\Ammonite Model\Images\test.tif", System.Drawing.Imaging.ImageFormat.Tiff) line. But of course the Filename parameter is a string and the ImageFormat.Tiff is a constant so neither is a null. But the debugger automatically steps over the Save function so I am unable to see what reference it considers to be null.
    Sunday, February 3, 2019 12:02 AM
  • Hi

    Looks like it is the .Image that is causing the exception as your PictureBox doesn't have an Image - you appear to be drawing directly onto the Picturebox control itself. (g has been declared as the graphics for the PictureBox)


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, February 3, 2019 12:15 AM
    Sunday, February 3, 2019 12:15 AM
  • The break is at the PictureBoxAmmonite.Image.Save("C:\Ammonite Model\Images\test.tif", System.Drawing.Imaging.ImageFormat.Tiff) line. But of course the Filename parameter is a string and the ImageFormat.Tiff is a constant so neither is a null. But the debugger automatically steps over the Save function so I am unable to see what reference it considers to be null.

    Well you have never set the Image property of the PictureBox e.g. Image is Nothing/Null in your case.

    If PictureBox1.Image Is Nothing Then
        Console.WriteLine("Can not save without an image")
    Else
        PictureBox1.Image.Save("MyPic.bmp")
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, February 3, 2019 12:38 AM
    Moderator
  • Okay you appear to be right, when I add your if code (changing it to a MessageBox) I do in fact get the "without an image" message. But perhaps because I am a novice, I do not understand how to set the Image property. My test case is simplistic while my more significant code draws much more complex images using the DrawLine function of the Graphics. What I want to do is draw the image on the screen and if I am satisfied with it save a copy of the image as a tiff or similar file. Perhaps I should add that I have all the data required to redraw the image if it needs to be drawn to some other graphics item before saving the file to tiff.

    Sunday, February 3, 2019 12:50 AM
  • Yes, you are correct. But how should I go about it. I want to draw an image on the screen and when satisfied with it save it to a tiff file.

    Sunday, February 3, 2019 12:52 AM
  • Hi

    Here is one example. Draws to a Bitmap, sets the Bitmap as the PictureBox Image then Saves it as a .tiff image. Of course, this example just draws a couple of lines, but whatever is draw to the Bitmap will be saved.

    ' Form1 with empty PictureBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim blackPen As New Pen(Color.Black, 4)
        Dim bluePen As New Pen(Color.Blue, 4)
        Dim redPen As New Pen(Color.Red, 8)
    
        Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Dim bmpG As Graphics = Graphics.FromImage(bmp)
        With bmpG
          .DrawLine(blackPen, New Point(10, 10), New Point(PictureBox1.Width - 10, PictureBox1.Height \ 2))
          .DrawLine(bluePen, New Point(100, 10), New Point(10, PictureBox1.Height \ 2))
          .DrawLine(redPen, New Point(PictureBox1.Width - 10, 10), New Point(PictureBox1.Width \ -30, PictureBox1.Height - 10))
        End With
        PictureBox1.Image = bmp
        PictureBox1.Image.Save(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestSave.tiff"), Imaging.ImageFormat.Tiff)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, February 3, 2019 2:10 AM
  • Okay when I used your could things are working better. No more null exception errors and I do get a tiff file. Except for the lines drawn with the color pen the contents of the tiff file (as viewed in Photoshop) are black. I'm sure that with some more work on my part I can fix that but it is getting late. Thanks for the help.
    Sunday, February 3, 2019 2:49 AM
  • Hi,

    Do you resolve the issue? If you resolve the issue,  please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 4, 2019 6:53 AM