Image.RawFormat to detect stream's format?

Sani Love 160 Reputation points
2023-05-14T23:56:43.09+00:00

Hello,

There's one MemoryStream which is made from a file and it can be in any format, to check if it's an image (just well known most used ones) is using Image.FromStream and Image.RawFormat a reliable method?

If so, here's my function:

Dim img As Image = Nothing
Try
	img = Image.FromStream(inputStream)
	If img IsNot Nothing Then
		Select Case img.RawFormat
			Case Imaging.ImageFormat.Bmp, Imaging.ImageFormat.Emf, etc...
				Return True
			Case Else
				Return False
		End Select
	End If
Catch Exception As Exception
	Return False
End Try

Question: FromStream(Stream, Boolean, Boolean) overload, which has validateImageData, shall be used here?

The docs won't help much:

https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromstream?view=netframework-4.0

Problem:

Error: Operator '=' is not defined for types 'System.Drawing.Imaging.ImageFormat' and 'System.Drawing.Imaging.ImageFormat'.

Can't evaluate the format with Select Case!

Any help is highly appreciated :)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 29,261 Reputation points Microsoft Vendor
    2023-05-15T03:14:57.3733333+00:00

    Hi @Sani Love ,

    Here is an optional way to check the file format: Checking the file's magic numbers.

    Magic numbers are the first bits of a file which uniquely identify the type of file. This makes programming easier because complicated file structures need not be searched in order to identify the file type.

    You can find the magic number in the file format you want by searching.

    For example, The magic numbers for JPEG are FF, D8, FF, E0, and PNG are 89, 50, 4E, 47.

    Imports System.IO
    
    Public Class ImageDetection
        Public Shared Function IsImage(stream As Stream) As Boolean
            Dim buffer As Byte() = New Byte(7) {} ' Read 8 bytes to check magic numbers
            stream.Read(buffer, 0, 8)
    
            If buffer(0) = &HFF AndAlso buffer(1) = &HD8 AndAlso buffer(2) = &HFF AndAlso buffer(3) = &HE0 Then
                Return True
            End If
    
            If buffer(0) = &H89 AndAlso buffer(1) = &H50 AndAlso buffer(2) = &H4E AndAlso buffer(3) = &H47 Then
                Return True
            End If
    
            Return False
        End Function
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful