代码之家  ›  专栏  ›  技术社区  ›  Moshe

在vb.net中签入文件类型?

  •  3
  • Moshe  · 技术社区  · 14 年前

    我有一个图像大小调整程序,它工作。问题是,当用户在“文件选择”对话框中选择非图像文件时,它会崩溃。如何检查图像文件?

    5 回复  |  直到 11 年前
        1
  •  7
  •   Community CDub    7 年前

    这是vb.net等价于 0xA3's answer 因为OP坚持使用VB版本。

    Function IsValidImage(filename As String) As Boolean
        Try
            Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
        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
    

    编辑:
    我不建议您检查文件扩展名。任何人都可以使用 .jpg 扩展并欺骗你的应用程序相信它是一个图像。

    最好的方法是使用上面的函数加载图像,或者打开前几个字节并检查jpeg签名。



    您可以在此处找到有关jpeg文件及其标题的更多信息:

        2
  •  5
  •   Community CDub    7 年前

    一个非常原始的检查是简单地尝试加载图像。如果它不是有效的 OutOfMemoryException 将被投掷:

    static bool IsImageValid(string filename)
    {
        try
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
        }
        catch (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;
        }
        return true;
    }
    

    如果我正确理解了你的问题,你的应用程序就会加载图像。因此,简单地将加载操作包装在try/catch块中并不意味着任何额外的开销。对于这种方法的vb.net解决方案,请检查@alex essilfie的答案。

    那些想知道为什么的人 Image.FromFile 正在对无效文件引发OOM应读取的答案 Hans Passant 以下问题:

    Is there a reason Image.FromFile throws an OutOfMemoryException for an invalid image format?

        3
  •  3
  •   Dan Tao    14 年前

    当然,您的第一道防线就是检查文件的扩展名:

    Function IsImageFile(ByVal filename As String) As Boolean
        Dim ext As String = Path.GetExtension(filename).ToLowerInvariant()
    
        ' This supposes your program can deal only with JPG files; '
        ' you could add other extensions here as necessary. '
        Return ext = ".jpg" OrElse ext = ".jpeg"
    End Function
    

    更好的是,正如SLC在评论中建议的那样,设置对话框的 Filter 财产:

    dialog.Filter = "Image files|*.jpg;*.jpeg"
    

    这不是一个保证——理想情况下,你想检查文件本身来验证它是一个图像,理论上,如果文件实际上是图像文件,你也应该能够加载扩展名不正常的文件(可能只是先要求用户确认),但这是一个简单的开始。

        4
  •  2
  •   Jim M    11 年前

    vb和c答案很好,但如果您计划更改或移动文件,则还包含一个“gotcha”:创建的“img”对象将锁定图像文件,除非调用Dispose()方法来释放它。见下文:

    VB
        Function IsValidImage(filename As String) As Boolean
        Try
            Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
            img.dispose()  ' Removes file-lock of IIS
        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
    
    C#
    static bool IsImageValid(string filename)
    {
        try
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(filename);
            img.dispose();   // Removes file-lock of IIS
        }
        catch (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;
        }
        return true;
    }
    
        5
  •  0
  •   John    14 年前

    最可靠的方法是了解需要加载的文件的签名。

    例如,jpeg具有特定的头格式。

    这样,如果您只查看扩展名,您的代码就不会那么容易被愚弄。

    163的答案应该能让你走上这条路。