2016-06-12 154 views
-1

我得到這個奇怪的錯誤 -參數無效ERROR - VB.Net - 爲什麼我得到這個錯誤?

「參數無效」

與圖像時。我製作了許多許多圖像 - 一次最多可達50張。我不清楚這個錯誤可能是什麼。記憶?我在完成計算後處理位圖。任何見解都會有所幫助。謝謝。

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 
+0

你嘗試調試和單步調試代碼到底在哪被拋出的錯誤來縮小? – Nkosi

+0

我可以嘗試,但問題是程序工作了一段時間 - 比如40張左右 - 然後這個錯誤突然出現,所以很難跨過,因爲你永遠不知道錯誤何時出現。有什麼建議麼? – PiE

+0

檢查這個http://stackoverflow.com/a/13914830/5233410 – Nkosi

回答

1

這一切都源於您處置一次性物品的順序。當他們仍然可以使用時,您正在手動處理。嘗試將聲明爲Using時曾經棄置的物品包裝在超出範圍時按正確的順序處置。

例如

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 
+0

好的 - 讓我試試這個 - 我應該處理這些圖像以確保我沒有收到「內存不足錯誤」?你怎麼看? – PiE

+0

您可以處理不再使用的資源,以釋放它們可能佔用的內存或其他可能使用的資源。 – Nkosi

+0

這似乎現在工作 - 我需要做更多的測試,雖然...謝謝 – PiE

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 

末Classstrong文本

+0

代碼只有答案是皺眉。請嘗試使用control或cmd縮進一個縮進級別(請參閱編輯欄中的幫助),並請解釋修復代碼的相關內容,並突出顯示主要關注或最相關改進的區域。 – clearlight

相關問題