SoFunction
Updated on 2025-03-07

C# to determine the format of graphics file

This article briefly describes the method of C# to determine the format of graphics file, which is very practical. Share it for your reference. The specific methods are as follows:

1. Make judgments based on the file extension.
This method is relatively simple, but if someone deliberately changes the file extension, this method will not work.

2. Make judgments through the methods provided by C# itself(Judge a certain type of picture).

Examples are as follows:

bool isJPG = false;
 img = (filesName);
if ( () )
{
isJPG = true;
}

3. Make judgments by reading the file content.

All picture files include: file recognition header and image data.

The file recognition header is used to let the computer determine which file format it is.

JPEG format:

All JPEG files start with the string "0xFFD8" and end with the string "0xFFD9". Based on this, we can determine whether it is a JPEG file.

BMP format:

BMP file starts with the string "0x4D42"

GIF format:

The six gifs are GIF89a or GIF87a

I hope that the method of judging the image format of graphic files (GIF, JPG, PNG) described in this article will be helpful to everyone.