logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class ImageHeaderUtils

    Reads the pixel size an image file declares in its header, without decoding it, so a file can be refused for its size before anything allocates memory for its pixels.

    Inheritance
    object
    ImageHeaderUtils
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: Serenity.Shared.Utils
    Assembly: Serenity.Shared.dll
    Syntax
    public static class ImageHeaderUtils
    Remarks

    PNG keeps its size in the first chunk (IHDR) and JPEG in its first start-of-frame segment. Only the declared size is read; nothing here says the rest of the file is a valid image.

    The JPEG walk is as strict as a decoder, so a file cannot show this reader one frame header and a decoder another. After the start-of-image marker it accepts only fill bytes, the standalone markers TEM and RST0-RST7, and the length-carrying segments DHT, DAC, DQT, DRI, APP0-APP15 and COM, skipped by their declared length (which must fit in the file); it stops at the first start-of-frame marker and reads the size there. Any other marker met first — a second start-of-image, an end-of-image, a start-of-scan, a reserved or extension marker, a stuffed FF00 — makes the file unreadable, as does a frame header whose length does not match its component count.

    Methods

    HasJpegSignature(byte[])

    Whether the bytes begin with a JPEG start-of-image marker followed by another marker.

    Declaration
    public static bool HasJpegSignature(byte[] imageBytes)
    Parameters
    Type Name Description
    byte[] imageBytes

    The file's bytes. May be null.

    Returns
    Type Description
    bool

    HasPngSignature(byte[])

    Whether the bytes begin with the PNG file signature.

    Declaration
    public static bool HasPngSignature(byte[] imageBytes)
    Parameters
    Type Name Description
    byte[] imageBytes

    The file's bytes. May be null.

    Returns
    Type Description
    bool

    TryReadDimensions(byte[], out int, out int)

    Reads the size a PNG or JPEG declares.

    Declaration
    public static bool TryReadDimensions(byte[] imageBytes, out int width, out int height)
    Parameters
    Type Name Description
    byte[] imageBytes

    The file's bytes. May be null.

    int width

    The declared width in pixels; zero when the size could not be read.

    int height

    The declared height in pixels; zero when the size could not be read.

    Returns
    Type Description
    bool

    True when the file is a PNG or JPEG whose header declares a size.

    In this article
    © 2026 Serenity. All Rights Reserved