-1

I'm getting this strange error -

"parameter not valid"

when working with images. I am creating many, many images - up to 50 at a shot. I am unclear what this error could be. Memory? I dispose the bitmaps after I complete computation. Any insights would be helpful. Thanks.

Private Async Function TakePhoto(ByVal keyword As String) As task

    Await Task.Delay(WaitMs)
    'System.Threading.Thread.Sleep(WaitMs)

    Try
        My.Computer.Audio.Play(Directory.GetCurrentDirectory + "\" + "iphone_camera.wav")
        Dim pic = New Bitmap(WebKitBrowser1.Width, WebKitBrowser1.Height)
        WebKitBrowser1.DrawToBitmap(pic, New Rectangle(0, 0, pic.Width, pic.Height))

        Dim fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
        Dim CropRect As New Rectangle(240, 190, 1000, 2800)
        Dim OriginalImage1 = Image.FromFile(fileName)
        Dim CropImage1 = New Bitmap(CropRect.Width, CropRect.Height)
        Using grp = Graphics.FromImage(CropImage1)
            grp.DrawImage(OriginalImage1, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
            OriginalImage1.Dispose()
            CropImage1.Save(fileName)
            CropImage1.Dispose()
        End Using

        fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
        CropRect = New Rectangle(0, 0, 1000, 1400)
        Dim OriginalImage2 = Image.FromFile(fileName)
        Dim CropImage2 = New Bitmap(CropRect.Width, CropRect.Height)
        Using grp = Graphics.FromImage(CropImage2)
            grp.DrawImage(OriginalImage2, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
            OriginalImage2.Dispose()
            'CropImage2.Save(Directory.GetCurrentDirectory + "\" + keyword + "_" + "test_ss_top.bmp")
            CropImage2.Save(Directory.GetCurrentDirectory + "\" + "test_ss_top.bmp")
            CropImage2.Dispose()
        End Using

        fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
        CropRect = New Rectangle(0, 1401, 1000, 1330)
        Dim OriginalImage3 = Image.FromFile(fileName)
        Dim CropImage3 = New Bitmap(CropRect.Width, CropRect.Height)
        Using grp = Graphics.FromImage(CropImage3)
            grp.DrawImage(OriginalImage3, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
            OriginalImage3.Dispose()
            'CropImage3.Save(Directory.GetCurrentDirectory + "\" + keyword + "_" + "test_ss_bottom.bmp")
            CropImage3.Save(Directory.GetCurrentDirectory + "\" + "test_ss_bottom.bmp")
            CropImage3.Dispose()
        End Using
    Catch ex As Exception
        Dim err = ex.Message
    End Try


End Function
Nkosi
  • 235,767
  • 35
  • 427
  • 472
PiE
  • 335
  • 1
  • 7
  • 24
  • did you try debugging and stepping through the code to narrow down where exactly the error is being thrown? – Nkosi Jun 12 '16 at 01:23
  • I can try, but the problem is the program works for a while - like 40 images or so - and then this error suddenly appears so it's hard to step through because you never know when the error will appear. Any suggestions? – PiE Jun 12 '16 at 01:25
  • Check this http://stackoverflow.com/a/13914830/5233410 – Nkosi Jun 12 '16 at 01:26
  • I saw this earlier...I'm not sure I understood why it would work...Let me try this again... – PiE Jun 12 '16 at 01:29
  • Try getting rid of the Try/Catch block so you can run it in the debugger, find out which statement throws the error, and debug it from there. – Blackwood Jun 12 '16 at 01:39
  • This all stems from the order in which you are disposing of your disposable objects. You are manually disposing of then when they could still be in use. Try wrapping what ever is disposable in a `Using` when declared to that is will be disposed of in the correct order. – Nkosi Jun 12 '16 at 01:45
  • 1
    I saw below. I have something to add. You need to learn to debug your own code instead of asking others to do it for you. You have the ability to debug your code better than we do, because you have all of it available to you. If you can't isolate it to a specific area, it's unreasonable to expect us to do so. – Ken White Jun 12 '16 at 04:55

2 Answers2

1

This all stems from the order in which you are disposing of your disposable objects. You are manually disposing of then when they could still be in use. Try wrapping what ever is disposable in a Using when declared to that is will be disposed of in the correct order when it goes out of scope.

For example

Dim fileName = Directory.GetCurrentDirectory + "\" + "test_ss.bmp"
Dim CropRect As New Rectangle(240, 190, 1000, 2800)
Using OriginalImage1 = Image.FromFile(fileName)
    Using CropImage1 = New Bitmap(CropRect.Width, CropRect.Height)
        Using grp = Graphics.FromImage(CropImage1)
            grp.DrawImage(OriginalImage1, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
            CropImage1.Save(fileName)
        End Using
    End Using
End Using
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Ok - let me try this - Should I dispose the images to make sure I don't get an "Out of Memory Error"? What are your thoughts? – PiE Jun 12 '16 at 01:56
  • You dispose of resources that are no longer in use to release any memory that they may be occupying or other resources they may be using. – Nkosi Jun 12 '16 at 01:59
  • This seems to work for now - I need to do more testing though...Thanks – PiE Jun 12 '16 at 04:48
0
Sub image()
    If connection.State = ConnectionState.Closed Then
        connection.Open()
    End If
    Dim arrImage() As Byte
    Dim strImage As String
    Dim myMs As New IO.MemoryStream
    '
    If Not IsNothing(Form2.picPhoto.Image) Then
        Form2.picPhoto.Image.Save(myMs, Form2.picPhoto.Image.RawFormat)
        arrImage = myMs.GetBuffer
        strImage = "?"
    Else
        arrImage = Nothing
        strImage = "NULL"
    End If


    Dim dt As New DataTable
    Dim da As OleDbDataAdapter = New OleDbDataAdapter(" SELECT * FROM fruits WHERE Name_Of_Fruit = '" & ComboBox1.SelectedValue & "'", connection)

    da.Fill(dt)
    If dt.Rows.Count > 0 Then



        If Not IsDBNull(dt.Rows(0).Item("Picture")) Then
            arrImage = CType(dt.Rows(0).Item("Picture"), Byte())
            For Each ar As Byte In arrImage
                myMs.WriteByte(ar)
            Next
            Form2.picPhoto.Image = System.Drawing.Image.FromStream(myMs)
        End If
    Else
        MessageBox.Show("Record Not Found", "")
    End If
End Sub

End Classstrong text

Jonnel
  • 1
  • Code only answers are frowned on. Please try indenting with control or cmd k one more indent level (see ? help in edit bar), and please explain what it is about the code that fixes and and highlight the areas of primary concern or most relevant improvement. – clearlight Feb 10 '17 at 01:09