如何将可读和不可读的图像分离到不同的文件夹中?

How to separate readable and unreadable images into different folders?

我有文件夹 D:\both_img。在该文件夹中,我有大量可读和不可读的 .bmp 图片。

如何将无法读取的图片移动到另一个文件夹?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim openfiled1 As New OpenFileDialog
    If openfiled1.ShowDialog <> DialogResult.Cancel Then
        PictureBox1.Image = Image.FromFile(openfiled1.FileName)
    End If
End Sub

试试下面的代码:

Dim di As New DirectoryInfo("D:\both_img")
Dim fiArr As FileInfo() = di.GetFiles()
Dim fi As FileInfo
For Each fi In fiArr
    Try
      Dim image1 As Bitmap = CType(Image.FromFile(fi.FullName, True), Bitmap)
      fi.MoveTo(validFiledestPath) 'Move to valid file folder
    Catch ex As OutOfMemoryException
      fi.MoveTo(invalidFileDestPath) 'Move to invalid file folder
    End Try
Next fri

此代码迭代所有文件。尝试使用 Image.FromFile 打开它们,如果打开则将它们移动到有效文件夹,否则移动到无效文件夹。
References