仅在文件格式为图像格式时才移动文件

时间:2014-11-04 07:59:33

标签: vb.net

我只是想确定我是否遗漏了代码中的某些内容。我想在将文件从一个文件夹移动到另一个文件夹之前验证该文件是图像。我准备了这个功能,并使用如下所示的。你能告诉我它没问题吗?我需要一些处理或其他任何东西或它刚刚退出。非常感谢,欢呼。

Function IsValidImage(filename As String) As Boolean
    Try
        Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
        img.Dispose()
    Catch generatedExceptionName As OutOfMemoryException
        ' Image.FromFile throws an OutOfMemoryException  
        ' if the file does not have a valid image format or 
        ' GDI+ does not support the pixel format of the file. 
        ' 
        Return False
    End Try
    Return True
End Function

If IsValidImage("c:\path\to\your\file.ext") Then
    'do something
    '
Else
    'do something else
    '
End If

2 个答案:

答案 0 :(得分:0)

让文件路径为"D:\web\sample\Image\my.Image.png",然后您可以使用以下代码检查文件是否是图像文件:

    Dim filepath As String = "D:\web\sample\Image\my.Image.png"
    Dim imageExtensions() As String = {"bmp", "gif", "jpg", "png", "psd", "psp", "thm", "tif", "yuv"}
    Dim pat As String = "\\(?:.+)\\(.+)\.(.+)"
    Dim r As Regex = New Regex(pat)
    Dim m As Match = r.Match(filepath)
    If imageExtensions.Contains(m.Groups(2).Captures(0).ToString()) Then
        MsgBox("valid Image")
    End If

答案 1 :(得分:0)

你是在正确的道路上,但你应该决定你想要获得什么:

如果您想开发一种方法,告诉您文件的内容被视为.Net识别的图片图像,您的方法就完全没问题。

我从你的(稍微)过度工作的代码开始:

Function IsValidImage(filename As String) As Boolean
    Try
        Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)

            ' the file could be opened as image so it is valid
            Return True

        End Using
    Catch notValidException As OutOfMemoryException

        ' Image.FromFile throws an OutOfMemoryException  
        ' if the file does not have a valid image format or 
        ' GDI+ does not support the pixel format of the file. 
        ' 
        Return False

    End Try
    ' Every other Exception is considered not to be able too look whether the
    ' file is an Image or not, so it should be thrown to outside
End Function

不要将此方法误判为使用Exception作为控制流,如果文件内容无法用于创建图像,则只会对引发的方法作出反应。这使得框架决定文件是否对Image有效。

让我们稍微改进您的方法,以便您可以确定文件中的图像是否超过给定大小:

Function IsValidImage(filename As String, maxSize As Size) As Boolean
    Try
        Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)

            ' Returns True if the image is smaller or equal than maxSize
            Return img.Size.Width <= maxSize.Width AndAlso
                img.Size.Height <= maxSize.Height

        End Using
    Catch notValidException As OutOfMemoryException

        Return False

    End Try
End Function

这也不是对Exceptions的误用。这是快速失败的做法:如果我无法获得Image该文件不是Image。否则,我会检查打开的Image上的维度,使我的结果依赖于使用它完成的操作。

错误使用Exception控制流可以,但不能是以下内容:

Sub CheckImage(filename As String)
    Try
        Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)

        End Using
    Catch notValidException As OutOfMemoryException

        Throw New FileIsNoImageException()

    End Try
End Sub

Try
    CheckImage("c:\path\to\your\file.ext")
    'do something
    '
Catch invalid As FileIsNoImageException
    'do something else
    '
End Try

如果您认为文件不是Image错误,那么即使这种方法也有效。 但通常情况下,你会在函数中执行此操作,这些函数会将Image作为返回值。

但这完全没有去

Sub CheckImage(filename As String)
    Try
        Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)

        Throw New FileIsImageException() ' DON'T DO SUCH A CRAP

        End Using
    Catch notValidException As OutOfMemoryException

        Throw New FileIsNoImageException()

    End Try
End Sub

Try
    CheckImage("c:\path\to\your\file.ext")
Catch valid As FileIsImageException
    'do something
    '
Catch invalid As FileIsNoImageException
    'do something else
    '
End Try

@Tim,请原谅,但我不能再听到这个 Exception使用是一个糟糕的做法故事