Checking file type in VB.NET?

I have a program for resizing an image, and it works. The problem is that the user selects a file without an image in the file selection dialog box, it crashes. How to check image files?

+3
source share
5 answers

Here's the equivalent of VB.NET 0xA3 , as the OP insisted on a version of 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

You use it as follows:

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

Edit:
I do not recommend checking file extensions. Anyone can save another file (for example, a text document) with the extension .jpgand the trick that you use to display the image.

- JPEG.



JPEG :

+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 :

, Image.FromFile OutOfMemoryException ?

+5

, , :

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"

. , , , ( , ), .

+2

VB # , "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;
}
+2

, .

JPEG , , .

This way, your code will not be so easy to fool if you just look at the extension.

The 163 answer should get you most of the way in that direction.

0
source

Source: https://habr.com/ru/post/1761336/


All Articles