none
How can I figure out this code? RRS feed

  • Question

  • Hi all,

    I have two forms ,(Form1 and Form2) and tow picturebox(s).

    In the form1 I have a button to capture desktop and move it to form2 , and when I move it to form2 and try to crop it it gives me this error

          

    System.ArgumentNullException was unhandled
      Message="Value cannot be null.
    Parameter name: image"
      ParamName="image"
      Source="System.Drawing"
      StackTrace:at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit)

    Here's the form1.button(Capture and move button)code:-

     Try
                Me.Hide()
                Me.Opacity = 0%
                Me.PictureBox1.Image = cc()
                Form2.PictureBox1.Image = Me.PictureBox1.Image
                Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
                Form2.PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
                Me.Opacity = 100%
                Form2.Show()
                Form2.PictureBox1.Image = Me.PictureBox1.Image
    
                Form2.Button4.Enabled = True
    
            Catch ex As Exception
            End Try
        End Sub

    And here the form2.button(crop button) code:-

      If Selected Then
                Dim Cropped As Image = New Bitmap(Math.Abs(SelectSiz.Width), Math.Abs(SelectSiz.Height))
                Dim g As Graphics = Graphics.FromImage(Cropped)
    
                Dim SrcRect As Rectangle = GetSelection(SelectPos, SelectSiz)
                SrcRect.X += XOffs
                SrcRect.Y += YOffs
                g.DrawImage(source, New Rectangle(0, 0, Cropped.Width, Cropped.Height), SrcRect, GraphicsUnit.Pixel)
                PictureBox1.Image = Cropped
                Button3.Enabled = True
                Button4.Enabled = True
                Button7.Enabled = True
    
            End If
            Selected = False

    Thanks advance,,,

    Sunday, June 11, 2017 2:08 PM

Answers

  • Here's the form1.button(Capture and move button) picture

    The message is quite precise - the variable passed for the 'image' parameter is null.  The method requires an image in order to be able to execute, but you have passed a variable that does not refer to any object.

    https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx

    You need to show the code where you initialise the variable 'source' to an image object.  That code either isn't executed or doesn't do what you think it does.

    • Marked as answer by Max45-1 Monday, June 12, 2017 12:47 AM
    • Unmarked as answer by Max45-1 Monday, June 12, 2017 12:48 AM
    • Marked as answer by Max45-1 Friday, June 23, 2017 2:26 AM
    Sunday, June 11, 2017 10:10 PM

All replies

  • Hi

    You do not show anything that can be compiled, onlky bits of some code. You do niot actually ask a question.

    Please explain what you are trying to do, and show all the code.


    Regards Les, Livingston, Scotland

    Sunday, June 11, 2017 2:56 PM
  • It seems to me thàt you don't know how to use the debugger to look at things their values to find out what is null and lead to something being a null value.
    Sunday, June 11, 2017 3:13 PM
  • Hi

    You do not show anything that can be compiled, onlky bits of some code. You do niot actually ask a question.

    Please explain what you are trying to do, and show all the code.


    Regards Les, Livingston, Scotland

    Here's the form1.button(Capture and move button) picture

    And here the form2.button(crop button) picture

    And here's the form1 whole code:-

    Imports System.Net.Mail
    Public Class Form1
    
        Private Function cc() As Bitmap
    
            Dim s As Screen = Screen.PrimaryScreen
            Dim img As New Bitmap(s.Bounds.Width, s.Bounds.Height)
            Dim gr As Graphics = Graphics.FromImage(img)
            gr.CopyFromScreen(s.Bounds.Location, Point.Empty, s.Bounds.Size)
            Return img
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Me.Opacity = 0%
                Me.Label1.ForeColor = Color.Red
                Me.PictureBox1.Image = cc()
                Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
                PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
                Me.Opacity = 100%
    
            Catch ex As Exception
            End Try
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                Me.Hide()
                Me.Opacity = 0%
                Me.PictureBox1.Image = cc()
                Form2.PictureBox1.Image = Me.PictureBox1.Image
                Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
                Form2.PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
                Me.Opacity = 100%
                Form2.Show()
                Form2.PictureBox1.Image = Me.PictureBox1.Image
    
                Form2.Button4.Enabled = True
    
            Catch ex As Exception
            End Try
        End Sub
    
        
    End Class

    And here's the form2 whole code:-

    Public Class Form2
        Private SelectSiz As Size
        Private SelectPos As System.Drawing.Point
        Private Selecting As Boolean = False
        Private Selected As Boolean = False
        Private source As Image
    
        Dim XOffs As Integer
        Dim YOffs As Integer
    
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
            Selecting = True
            SelectPos = New Point(e.Location)
            SelectSiz = New Size(1, 1)
            Me.Refresh()
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
            If Selecting Then
                SelectSiz = New Size(e.Location.X - SelectPos.X, e.Location.Y - SelectPos.Y)
                Selected = True
                Me.Refresh()
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
            Selecting = False
        End Sub
    
        Private Sub btnCopy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If Selected Then
                Dim Cropped As Image = New Bitmap(Math.Abs(SelectSiz.Width), Math.Abs(SelectSiz.Height))
                Dim g As Graphics = Graphics.FromImage(Cropped)
    
                Dim SrcRect As Rectangle = GetSelection(SelectPos, SelectSiz)
                SrcRect.X += XOffs
                SrcRect.Y += YOffs
                g.DrawImage(source, New Rectangle(0, 0, Cropped.Width, Cropped.Height), SrcRect, GraphicsUnit.Pixel)
                PictureBox1.Image = Cropped
                Button3.Enabled = True
                Button4.Enabled = True
                Button7.Enabled = True
    
            End If
            Selected = False
        End Sub
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
            If Selected Then
                e.Graphics.DrawRectangle(Pens.Black, GetSelection(SelectPos, SelectSiz))
            End If
        End Sub
    
        Public Function GetSelection(ByVal Start As Point, ByVal Range As Size) As Rectangle
            Dim X As Integer = Start.x
            Dim X1 As Integer = Range.Width
            If X1 < 0 Then
                X1 = Math.Abs(X1)
                X -= X1
            End If
            Dim Y As Integer = Start.Y
            Dim Y1 As Integer = range.height
    
            If Y1 < 0 Then
                Y1 = Math.Abs(Y1)
                Y -= Y1
            End If
            Return New Rectangle(X, Y, X1, Y1)
        End Function
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim ofd As New OpenFileDialog
            ofd.Filter = "All Picture Files|*.jpg;*.bmp;*.png;*.gif"
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
            ofd.RestoreDirectory = True
            ofd.Title = "Select image to crop it"
            If ofd.ShowDialog() = DialogResult.OK Then
    
                source = Image.FromFile(ofd.FileName)
                PictureBox1.Image = source
                Button4.Enabled = True
            End If
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    
            Button3.Enabled = False
            Button4.Enabled = False
            Button7.Enabled = False
    
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim img As Image = Me.PictureBox1.Image
            img.RotateFlip(RotateFlipType.Rotate90FlipNone)
            Me.PictureBox1.Image = img
        End Sub
        Private Sub PictureBox1_MouseWhee(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseWheel
            If e.Delta <> 0 Then
                If e.Delta <= 0 Then
                    If PictureBox1.Width < 500 Then Exit Sub 'minimum 500?
                Else
                    If PictureBox1.Width > 2000 Then Exit Sub 'maximum 2000?
                End If
    
                PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
                PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
            End If
    
        End Sub
        Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
            PictureBox1.Focus()
        End Sub
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PictureBox1.Image = source
            Button3.Enabled = False
            Button4.Enabled = False
            Button7.Enabled = False
        End Sub
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            Dim SFD As New SaveFileDialog
            Dim filePath = String.Format("Image{0:yyyyMMddHHmmss}", DateTime.Now)
            Try
                SFD.Title = "Save Image"
                SFD.FileName = filePath
                SFD.Filter = "JPG files (*.jpg)|*.jpg|GIF files (*.gif)|*.gif  |BMP files (*.bmp)|*.bmp|PNG files (*.png)|*.png"
                If SFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SFD.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
                End If
            Catch ex As Exception
            End Try
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            Me.Hide()
            Form1.Show()
    
        End Sub
    End Class

    And here's the error picture

    • Proposed as answer by DA924x Sunday, June 11, 2017 10:18 PM
    • Unproposed as answer by DA924x Sunday, June 11, 2017 10:25 PM
    Sunday, June 11, 2017 8:48 PM
  • Here's the form1.button(Capture and move button) picture

    The message is quite precise - the variable passed for the 'image' parameter is null.  The method requires an image in order to be able to execute, but you have passed a variable that does not refer to any object.

    https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx

    You need to show the code where you initialise the variable 'source' to an image object.  That code either isn't executed or doesn't do what you think it does.

    • Marked as answer by Max45-1 Monday, June 12, 2017 12:47 AM
    • Unmarked as answer by Max45-1 Monday, June 12, 2017 12:48 AM
    • Marked as answer by Max45-1 Friday, June 23, 2017 2:26 AM
    Sunday, June 11, 2017 10:10 PM
  • Comon man, you are being told the parm that is a null value. Did you even bother to use Bing or Google to figure out what that particular parm is about and how to use it? I suspect that it is a parm you have to set with a numeric value, ,  and  you didn't to set the pixel to be used. And since you have not set it, it's value the value is null and it is causing the program to blow up. It doesn't take a rocket scientist here.😎
    Sunday, June 11, 2017 10:16 PM
  • Here's the form1.button(Capture and move button) picture

    The message is quite precise - the variable passed for the 'image' parameter is null.  The method requires an image in order to be able to execute, but you have passed a variable that does not refer to any object.

    https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx

    You need to show the code where you initialise the variable 'source' to an image object.  That code either isn't executed or doesn't do what you think it does.

    OK here I've deleted unnecessary controls and code and this's the new code.

    Now I've

    one form (Form1),

    one picturebox1 ,

    two buttons:-

    1-button1[capture image and put it in the picturebox1  ]

    2-button2[crop the selected part of the image ]

    Imports System.Net.Mail
    
    Public Class Form1
        Private SelectSiz As Size
        Private SelectPos As System.Drawing.Point
        Private Selecting As Boolean = False
        Private Selected As Boolean = False
        Private source As Image
    
        Dim XOffs As Integer
        Dim YOffs As Integer
        Private Function cc() As Bitmap
    
            Dim s As Screen = Screen.PrimaryScreen
            Dim img As New Bitmap(s.Bounds.Width, s.Bounds.Height)
            Dim gr As Graphics = Graphics.FromImage(img)
            gr.CopyFromScreen(s.Bounds.Location, Point.Empty, s.Bounds.Size)
            Return img
        End Function
        Private Sub Capture_image_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Me.Opacity = 0%
                Me.PictureBox1.Image = cc()
                Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
                PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
                Me.Opacity = 100%
    
            Catch ex As Exception
            End Try
    
        End Sub
    
        Private Sub Crop_image_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            If Selected Then
                Dim Cropped As Image = New Bitmap(Math.Abs(SelectSiz.Width), Math.Abs(SelectSiz.Height))
                Dim g As Graphics = Graphics.FromImage(Cropped)
    
                Dim SrcRect As Rectangle = GetSelection(SelectPos, SelectSiz)
                SrcRect.X += XOffs
                SrcRect.Y += YOffs
                g.DrawImage(source, New Rectangle(0, 0, Cropped.Width, Cropped.Height), SrcRect, GraphicsUnit.Pixel)
                PictureBox1.Image = Cropped
    
    
            End If
            Selected = False
        End Sub
       
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
            Selecting = True
            SelectPos = New Point(e.Location)
            SelectSiz = New Size(1, 1)
            Me.Refresh()
        End Sub
    
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
            If Selecting Then
                SelectSiz = New Size(e.Location.X - SelectPos.X, e.Location.Y - SelectPos.Y)
                Selected = True
                Me.Refresh()
            End If
        End Sub
    
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
            Selecting = False
        End Sub
    
        
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
            If Selected Then
                e.Graphics.DrawRectangle(Pens.Black, GetSelection(SelectPos, SelectSiz))
            End If
        End Sub
    
        Public Function GetSelection(ByVal Start As Point, ByVal Range As Size) As Rectangle
            Dim X As Integer = Start.X
            Dim X1 As Integer = Range.Width
            If X1 < 0 Then
                X1 = Math.Abs(X1)
                X -= X1
            End If
            Dim Y As Integer = Start.Y
            Dim Y1 As Integer = Range.Height
    
            If Y1 < 0 Then
                Y1 = Math.Abs(Y1)
                Y -= Y1
            End If
            Return New Rectangle(X, Y, X1, Y1)
        End Function
    
        Private Sub PictureBox1_MouseWhee(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseWheel
            If e.Delta <> 0 Then
                If e.Delta <= 0 Then
                    If PictureBox1.Width < 500 Then Exit Sub 'minimum 500?
                Else
                    If PictureBox1.Width > 2000 Then Exit Sub 'maximum 2000?
                End If
    
                PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)
                PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)
            End If
    
        End Sub
    End Class

    So the issue is that when I capture an image  and bring the image to the picturebox1 then I fail to crop it and gives me this error shown in the picture below



    • Edited by Max45-1 Monday, June 12, 2017 4:08 AM
    Monday, June 12, 2017 4:04 AM
  • OK here I've deleted unnecessary controls and code and this's the new code.

    You haven't addressed the actual problem - the value you are passing for the parameter 'image' is still Nothing.  You need to pass an image object to the method.  You haven't included the code where you assign a value to the 'source' variable.

    Why did you change this code from what was originally provided?

    Monday, June 12, 2017 4:43 AM
  • You are still showing the same problem, with the same cause.

    Graphics.DrawImage Method (Image, Rectangle, Rectangle, GraphicsUnit)
    https://msdn.microsoft.com/en-us/library/ms142040(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    requires that the first argument be an existing Image object which is to
    be drawn. Where in the latest code do you actually set "source" to an
    object?

    In the earlier posted code you do that in the Button2_Click event:

           If ofd.ShowDialog() = DialogResult.OK Then

                source = Image.FromFile(ofd.FileName)

    But is Button2 always clicked before btnCopy_Click ?

    - Wayne

                
    Monday, June 12, 2017 4:47 AM


  • it gives me this error

          

    System.ArgumentNullException was unhandled
      Message="Value cannot be null.
    Parameter name: image"
      ParamName="image"
      Source="System.Drawing"
      StackTrace:at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit)

    Perhaps the simplest way to avoid that exception is to test "source"
    for null before calling DrawImage. If it is null, take whatever action 
    best suits your application design goals. Only call DrawImage with known
    valid arguments.

    - Wayne

    Monday, June 12, 2017 5:03 AM
  • Hi omanxp45-2,

    I tried your code, I click the form 1 "Capture and Edit" button, and I get one screenshot in the PictureBox1 in the form1, be saved into the desktop.

    And then form2 show, the screenshot show  in the picturebox1, I click copy button, save this screenshot.

    I have no error in this process.

    Is my practice right?

    Best Regards,

    Cherry


    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, June 12, 2017 6:04 AM
    Moderator


  • it gives me this error

          

    System.ArgumentNullException was unhandled
      Message="Value cannot be null.
    Parameter name: image"
      ParamName="image"
      Source="System.Drawing"
      StackTrace:at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit)

    Perhaps the simplest way to avoid that exception is to test "source"
    for null before calling DrawImage. If it is null, take whatever action 
    best suits your application design goals. Only call DrawImage with known
    valid arguments.

    - Wayne

    Here the code I've added source to it but still with error

     Me.Opacity = 0%
                Me.PictureBox1.Image = cc()
                Dim filePath = String.Format("image{0:yyyyMMddHHmmss}.png", DateTime.Now)
                PictureBox1.Image.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), (filePath)))
                Me.Opacity = 100%
    
                source = Image.FromFile(PictureBox1.Image)
                PictureBox1.Image = source
    
     

    and here's the error

    Error 1 Value of type 'System.Drawing.Image' cannot be converted to 'String'. C:\Users\TOSHIBA\Desktop\CropImage\RM Screenshot v2.0 -2 - Copy\RM Snapshot\Form1.vb 143 37 RM Screenshot

    Monday, June 12, 2017 7:03 PM
  • and here's the error

    Error 1 Value of type 'System.Drawing.Image' cannot be converted to 'String'. C:\Users\TOSHIBA\Desktop\CropImage\RM Screenshot v2.0 -2 - Copy\RM Snapshot\Form1.vb 143 37 RM Screenshot

    That code appears to be in form 1.  The error you were getting was in form 2.  That code change will not have any effect on the original problem, and appears to have simply created a new error.

    Why have you changed this code from the original that was provided?  What is different in what you are trying to do that made you change the code?

    Monday, June 12, 2017 9:59 PM
  • and here's the error

    Error 1 Value of type 'System.Drawing.Image' cannot be converted to 'String'. C:\Users\TOSHIBA\Desktop\CropImage\RM Screenshot v2.0 -2 - Copy\RM Snapshot\Form1.vb 143 37 RM Screenshot

    That code appears to be in form 1.  The error you were getting was in form 2.  That code change will not have any effect on the original problem, and appears to have simply created a new error.

    Why have you changed this code from the original that was provided?  What is different in what you are trying to do that made you change the code?

    I just wanted to simplify my app, now I've one form
    Monday, June 12, 2017 10:46 PM
  • I just wanted to simplify my app, now I've one form

    Please mark as answers those responses which assisted you in resolving the issue.

    Monday, June 12, 2017 10:58 PM

  • Here the code I've added source to it but still with error

                source = Image.FromFile(PictureBox1.Image)
    
     

    and here's the error

    Error 1 Value of type 'System.Drawing.Image' cannot be converted to 'String'.

    That error and its cause should be self-evident.

    Image.FromFile Method (String)
    https://msdn.microsoft.com/en-us/library/stf701f5(v=vs.110).aspx

    expects a String as an argument.

    PictureBox.Image Property is a System.Drawing.Image type, not a String.

    - Wayne

    Tuesday, June 13, 2017 12:18 AM
  • I just wanted to simplify my app, now I've one form

    Please mark as answers those responses which assisted you in resolving the issue.

    Sure I'll do as soon as my issue be solved
    Tuesday, June 13, 2017 2:56 AM
  • Sure I'll do as soon as my issue be solved

    The error message you posted was created when you divided your application into two forms.  Now that you have put it back to one form either you won't get an error, or the error will change.  What is the code you are now using, and what is the error?

    Why have you changed this code from the original that was provided?  What is different in what you are trying to do that made you change the code?

    Tuesday, June 13, 2017 4:50 AM