0. Common ways to judge picture files
There are three main ways to determine whether a file is an image:
- Judging by file extension
- Judging by the file header (Magic Number)
- Try to load the file to judge (recommended: use ImageSharp)
1. Judging by file extension
This is the easiest way to determine whether it is an image by judging the extension of the file. Although the implementation is simple, it is not rigorous enough, because the file extension can be forged.
Code Example
public bool IsImageByExtension(string filePath) { // Supported image format extension string[] validExtensions = { ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff", ".webp" }; string extension = (filePath)?.ToLower(); // Get the file extension and convert it to lowercase return (extension); }
Pros and cons
- advantage: Simple implementation and high performance.
- shortcoming: There is no guarantee that the content of the file is indeed a picture and may be forged.
2. Judgment through the file header (Magic Number)
Each file format has a unique binary header (Magic Number). The file type can be more accurately judged by reading the first few bytes of a file and matching it with the Magic Number in common image formats.
Code Example
public bool IsImageByHeader(string filePath) { // Magic Number that defines common image formats var imageHeaders = new Dictionary<string, byte[]> { { "jpg", new byte[] { 0xFF, 0xD8 } }, { "png", new byte[] { 0x89, 0x50, 0x4E, 0x47 } }, { "gif", new byte[] { 0x47, 0x49, 0x46 } }, { "bmp", new byte[] { 0x42, 0x4D } }, { "tiff", new byte[] { 0x49, 0x49, 0x2A, 0x00 } } }; using (FileStream fs = new FileStream(filePath, , )) { byte[] buffer = new byte[4]; // Read the first 4 bytes of the file (buffer, 0, ); // Determine whether the file header matches a certain image format foreach (var header in ) { if (().SequenceEqual(header)) return true; } } return false; }
Pros and cons
- advantage: More reliable, ensuring that the file header matches the image format.
- shortcoming: You need to parse the file contents and take up resources slightly.
3. Use ImageSharp to determine whether the file is an image
As it is deprecated in .NET 6 and above, it is recommended to use the library to determine whether the file is an image. ImageSharp is a cross-platform image processing library that supports multiple image formats and can ensure that files not only match image extensions, but can indeed be parsed into image formats.
Install ImageSharp
Use the following command to installImageSharp
:
dotnet add package
Or in the project.csproj
Added to the file:
<PackageReference Include="" Version="3.0.0" />
Code example: Detect file types using ImageSharp
using ; public bool IsImageByImageSharp(string filePath) { try { // Use to detect whether the file is a valid picture using (var image = (filePath)) { return true; } } catch () { // The format is not supported or not the picture return false; } catch (Exception) { // Other exceptions, such as the file does not exist, etc. return false; } }
Code parsing
-
: If the file is in a supported image format, it will be loaded successfully, otherwise an exception will be thrown.
-
UnknownImageFormatException
: This exception is thrown when the file is not a picture or the format is not supported. - Other exceptions: Capture situations such as file path errors to avoid program crashes.
Pros and cons
- advantage: Rigorous and reliable to ensure that the file content is indeed in the picture format.
- shortcoming: It will take up resources slightly when loading the file.
Supported image formats
ImageSharp
Supports common picture formats:
ImageSharp
Supports common picture formats:
- JPEG (
.jpg
,.jpeg
) - PNG (
.png
) - GIF (
.gif
) - BMP (
.bmp
) - TIFF (
.tiff
) - WEBP (
.webp
)
4. Summary
The above is the detailed content of various methods of using C# to determine whether a file is a picture. For more information on whether a file is a picture, please pay attention to my other related articles!