Home > Backend Development > C++ > How to Reliably Validate Image File Formats in C#?

How to Reliably Validate Image File Formats in C#?

Barbara Streisand
Release: 2024-12-29 17:57:14
Original
687 people have browsed it

How to Reliably Validate Image File Formats in C#?

How to Validate Image File Format in C#

When loading an image from a file, validation is crucial to prevent errors such as OutOfMemoryExceptions. However, relying solely on file extensions can be unreliable.

Validation Function Prototypes

bool IsValidImage(string fileName);
bool IsValidImage(Stream imageStream);
Copy after login

Solution: Image Validation Based on Byte Signatures

A reliable approach is to check the file's byte signature, which provides information about its format. Here's a C# implementation:

public enum ImageFormat
{
    bmp,
    jpeg,
    gif,
    tiff,
    png,
    unknown
}

public static ImageFormat GetImageFormat(byte[] bytes)
{
    // Byte signatures for various image formats
    var bmp    = Encoding.ASCII.GetBytes("BM");     // BMP
    var gif    = Encoding.ASCII.GetBytes("GIF");    // GIF
    var png    = new byte[] { 137, 80, 78, 71 };    // PNG
    var tiff   = new byte[] { 73, 73, 42 };         // TIFF
    var tiff2  = new byte[] { 77, 77, 42 };         // TIFF
    var jpeg   = new byte[] { 255, 216, 255, 224 }; // jpeg
    var jpeg2  = new byte[] { 255, 216, 255, 225 }; // jpeg canon

    // Compare the byte signatures to the input bytes
    if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
        return ImageFormat.bmp;

    if (gif.SequenceEqual(bytes.Take(gif.Length)))
        return ImageFormat.gif;

    if (png.SequenceEqual(bytes.Take(png.Length)))
        return ImageFormat.png;

    if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
        return ImageFormat.tiff;

    if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
        return ImageFormat.tiff;

    if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
        return ImageFormat.jpeg;

    if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
        return ImageFormat.jpeg;

    return ImageFormat.unknown;
}
Copy after login

Usage Examples

// Example 1: Validate an image using a file path
if (IsValidImage(filePath))
{
    // Load the image safely
}

// Example 2: Validate an image using a stream
Stream imageStream = new FileStream(filePath, FileMode.Open);
if (IsValidImage(imageStream))
{
    // Load the image safely
}
Copy after login

The above is the detailed content of How to Reliably Validate Image File Formats in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template