0

I am checking images for width and height and trying to delete all that are not satisfying the if requirement. The If requirement works (from 12 -> 1) and i can not use f.delete in this case as it throws out an error "f is in use by another program; can not open image10.jpg" (12 and 11 do not satisfy the If requirement, 10 does).

How do i delete images that satisfy the If requirement?

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
Sci00213
  • 23
  • 6
  • 1
    You need to `Dispose()` your bitmap – SLaks Mar 29 '19 at 16:53
  • 3
    Also, get rid of the .ToString, height and width are numbers. – dbasnett Mar 29 '19 at 17:41
  • Another option is look into [Image.GetPropertyItem](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.getpropertyitem?view=netframework-4.7.2). Basically right click on the file and then select "Details", what you see there can be pulled out by using `GetPropertyItem` – Trevor Mar 29 '19 at 18:50

2 Answers2

1

You have to dispose the reference to the image before trying to delete it. Best way is to use 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
Code Pope
  • 5,075
  • 8
  • 26
  • 68
1

A slightly different method, using the Shell extensions provided by the Microsoft Shell Controls And Automation COM library.
You don't need to load a Bitmap and dispose of it to gather the informations you require. These are provided by the Shell.

All Windows Properties related to Shell Object types.

To use the Shell32 namespace, you need to add a reference in your project to this Type Library:
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()
Jimi
  • 29,621
  • 8
  • 43
  • 61