---
title: "How can you detect file type using byte-level inspection (e.g., IsPdf, IsImage)?"  
description: "How can you detect file type using byte-level inspection (e.g., IsPdf, IsImage)?"  
author: "ICSM Computer"  
published: 2025-05-16  
updated: 2025-05-25  
canonical: https://www.mindstick.com/forum/161632/how-can-you-detect-file-type-using-byte-level-inspection-e-g-ispdf-isimage  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# How can you detect file type using byte-level inspection (e.g., IsPdf, IsImage)?

How can you [detect](https://www.mindstick.com/forum/1906/detect-when-cd-drive-was-closed) [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) type using byte-level inspection (e.g., `IsPdf`, `IsImage`)?

## Replies

### Reply by Anubhav Sharma

Detecting file types using **byte-level inspection** (also called **magic number checking**) involves reading the beginning bytes of a file (the "magic number") to determine its format. This is more reliable than checking file extensions.

Here's how you can implement file type detection (e.g., `IsPdf`, `IsImage`, etc.) by inspecting the byte content:

## Step-by-Step Overview

1. **Read the header bytes** of the file (usually the first 4–8 bytes).
2. **Match the byte signature** against known file type "magic numbers."
3. Optionally, allow some fuzziness for formats like image types with common variations.

## Common File Type Magic Numbers

| File Type | Magic Number (Hex) | ASCII | Description |
| --- | --- | --- | --- |
| PDF | `25 50 44 46` | `%PDF` | PDF files start with `%PDF` |
| PNG | `89 50 4E 47 0D 0A 1A 0A` | — | PNG image |
| JPEG | `FF D8 FF` | — | JPEG image |
| GIF | `47 49 46 38` | `GIF8` | GIF image (GIF87a/GIF89a) |
| ZIP | `50 4B 03 04` | `PK..` | ZIP archives (also DOCX, XLSX, etc.) |

## Example: Detect PDF and Image Types in C#

```cs
public static class FileTypeDetector
{
    public static bool IsPdf(byte[] bytes)
    {
        // Check if the first 4 bytes are "%PDF"
        return bytes.Length >= 4 &&
               bytes[0] == 0x25 && // %
               bytes[1] == 0x50 && // P
               bytes[2] == 0x44 && // D
               bytes[3] == 0x46;   // F
    }

    public static bool IsPng(byte[] bytes)
    {
        byte[] pngSignature = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
        return bytes.Take(pngSignature.Length).SequenceEqual(pngSignature);
    }

    public static bool IsJpeg(byte[] bytes)
    {
        return bytes.Length >= 3 &&
               bytes[0] == 0xFF &&
               bytes[1] == 0xD8 &&
               bytes[2] == 0xFF;
    }

    public static bool IsGif(byte[] bytes)
    {
        return bytes.Length >= 4 &&
               bytes[0] == 0x47 && // G
               bytes[1] == 0x49 && // I
               bytes[2] == 0x46 && // F
               bytes[3] == 0x38;   // 8
    }

    public static bool IsZip(byte[] bytes)
    {
        return bytes.Length >= 4 &&
               bytes[0] == 0x50 && // P
               bytes[1] == 0x4B && // K
               bytes[2] == 0x03 &&
               bytes[3] == 0x04;
    }

    public static bool IsImage(byte[] bytes)
    {
        return IsPng(bytes) || IsJpeg(bytes) || IsGif(bytes);
    }
}
```

## Usage Example

```cs
byte[] fileBytes = File.ReadAllBytes("example.pdf");

if (FileTypeDetector.IsPdf(fileBytes))
{
    Console.WriteLine("This is a PDF file.");
}
else if (FileTypeDetector.IsImage(fileBytes))
{
    Console.WriteLine("This is an image file.");
}
```

## Benefits

- Works regardless of file extension.
- Safer when dealing with user-uploaded files.
- Can easily be extended to support more file types.


---

Original Source: https://www.mindstick.com/forum/161632/how-can-you-detect-file-type-using-byte-level-inspection-e-g-ispdf-isimage

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
