判断是否为图片文件

ASP.NET实现上传图片文件时或某一文件判断是否为图片文件。可以参考下面简单说明。

在System.Drawing.Image名称空间下有一个静态方法FromFile(filename As String)得到的对象类型,就是System.Drawing.Image

如果在转换类型发生异常,那可以判断为所指定的图片文件不是图片格式的文件。最近在专案使用了这个方法,:

IsImage
Public Function IsImage(filePath As StringAs Boolean
            Dim oImg As System.Drawing.Image
            Try
                oImg = System.Drawing.Image.FromFile(filePath)
                oImg.Dispose()
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function
原文地址:https://www.cnblogs.com/insus/p/2385054.html