Pierre M Fiorini Pierre M Fiorini - 5 months ago 51
Vb.net Question

parameter not valid ERROR - VB.Net - Why am I getting this Error?

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

Answer

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
Comments