2012-10-10 51 views
1

我試圖檢查一組圖像(通常超過50個,每個大約3 MB),以確定它們的方向。 當我已經處理了一堆內容時,出現「內存不足」錯誤。vb.net - 處理大量圖像時內存不足

所以我的問題是如何分別檢查每個圖像以便使用最少的內存?

我真的很新vb.net和編程可言,所以這是我能想出如何做我的任務的唯一方法:

Dim MyFiles As New ArrayList() 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 

    TextBox2.Clear() 

    If CheckBox2.Checked = True Then 

     TextBox2.Text = "Checking the orientation of the images ..." & vbNewLine & vbNewLine 

     For i As Integer = 0 To MyFiles.Count - 1 

      TextBox2.AppendText("Checking " & MyFiles(i) & vbNewLine) 
      If Image.FromFile(MyFiles(i)).Width < Image.FromFile(MyFiles(i)).Height Then 
       TextBox2.AppendText(vbNewLine & "There are images with portrait orientation. Splitting aborted!" & vbNewLine) 
       Return 
      End If 

     Next 

     TextBox2.AppendText(vbNewLine & "All images are with landscape orientation." & vbNewLine) 

    End If 

    'ConvertBMP("C:\test.bmp", ImageFormat.Jpeg) 

End Sub 
+0

你是不配置Image對象,這就是爲什麼最終會出現「內存不足」的原因。 –

回答

1

拋出一個使用()圍繞圖像.FromFile命令。此外,你應該只建立一次圖像,並檢查寬度/高度一次,而不是解碼兩次。

在C#中它應該是這樣的:

using (var img = Image.FromFile(filename)) 
{ 
    if (img.Width < img.Height) 
     doSomething(); 
} 

或在VB.Net(我的VB.Net是有點生疏,但我認爲這是正確的):

Dim img as Image 
Using img = Image.FromFile(filename) 
    If img.Width < img.Height 
     TextBox2.AppendText(vbNewLine & "There are images with portrait orientation. Splitting aborted!" & vbNewLine) 
     Return 
    End If 
End Using 
+0

非常感謝您的快速回復。我會去做。 – user1735880