簡體   English   中英

我不知道如何使用我的代碼刪除這些圖像

[英]I can not figure out how to delete these images using my code

我正在檢查圖像的寬度和高度,並嘗試刪除所有不滿足if要求的圖像。 如果需求有效(從12-> 1開始),並且在這種情況下我不能使用f.delete,因為它拋出錯誤“另一個程序正在使用f;無法打開image10.jpg”(12和11不能滿足“如果”要求,則為10。

如何刪除滿足If要求的圖像?

Dim s As New DirectoryInfo("C:\Users\*\source\repos\CinemaSpider\CinemaSpider\bin\Debug")
Dim files As FileInfo() = s.GetFiles("*.jpg")
        For Each f As FileInfo In files
           Dim bmp As New Bitmap(f.FullName)
           If bmp.Width.ToString() < 182 Or bmp.Height.ToString() < 268 Then
              f.Delete()
           End If

您必須先刪除對圖像的引用,然后再嘗試刪除它。 最好的方法是使用using

    Dim s As New DirectoryInfo("C:\Users\*\source\repos\CinemaSpider\CinemaSpider\bin\Debug")
    Dim files As FileInfo() = s.GetFiles("*.jpg")
    For Each f As FileInfo In files
        Dim DoDelete as Boolean= false
        Using  image1 As Image = Image.FromFile(f.FullName)
            If image1.Width < 182 OrElse image1.Height < 268 Then
                DoDelete = True
            End If
        End Using
        if DoDelete Then f.Delete()
    Next

使用Microsoft Shell Controls And Automation COM庫提供的Shell擴展的方法稍有不同。
您無需加載位圖並進行處理就可以收集所需的信息。 這些是由Shell提供的。

與Shell對象類型有關的所有Windows屬性

要使用Shell32名稱空間,您需要在項目中添加對此類型庫的引用:
Project -> References -> COM -> Type Library -> Microsoft Shell Controls And Automation

Imports Shell32

Dim imageFolder As String = "[Insert Your Path]"
Dim shell As New Shell()
Dim items As FolderItems = shell.NameSpace(imageFolder).Items

For Each item As FolderItem2 In items
    If item.ExtendedProperty("Type").ToString().Contains("JPEG") Then
        If CInt(item.ExtendedProperty("System.Image.HorizontalSize")) < 187 OrElse
            CInt(item.ExtendedProperty("System.Image.VerticalSize")) < 268 Then
            File.Delete(item.Path)
        End If
    End If
Next

Marshal.ReleaseComObject(items)
Marshal.FinalReleaseComObject(shell)
Marshal.CleanupUnusedObjectsInCurrentContext()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM