Core API

Core API

Aspose.Imaging FOSS for .NET’s entire public surface is three classes: ImageProbe (the static facade), ImageInfo (the result value), and ImageFormat (the format enum). This page covers how they fit together as a contract, beyond the per-feature tour in Features and Functionalities.


The three-class contract

  • ImageProbe — static, stateless. Every call is independent; there is nothing to configure or dispose.
  • ImageInfo — sealed, immutable. All five properties (Format, Width, Height, BitDepth, FrameCount) are read-only once constructed.
  • ImageFormat — an enum with one value per recognized format, plus Unknown.
using Aspose.Imaging.Foss;

var info = ImageProbe.ProbeFile("photo.jpg");
ImageFormat format = info.Format;   // always set
int? width = info.Width;            // set only when the format's header carries it

Constructing ImageInfo directly

ImageInfo’s constructor is public — only format is required, every other parameter defaults to null. This is useful for unit tests that need a stand-in result without probing a real file:

using Aspose.Imaging.Foss;

// Fake a PNG result for a test double, with no dimensions
var fakeInfo = new ImageInfo(ImageFormat.Png);

// Fake a fully-populated result
var fullInfo = new ImageInfo(ImageFormat.Gif, width: 64, height: 48, bitDepth: 8, frameCount: 3);

There is no corresponding way to mutate an ImageInfo after construction — every property is get-only, so a probed or constructed instance is safe to pass around and cache without defensive copying.


Choosing the right overload

OverloadUse when
ImageProbe.ProbeFile(path)You have a file path and want the full ImageInfo
ImageProbe.Probe(stream)You already have an open Stream (seekable or not) and want the full ImageInfo
ImageProbe.Probe(data)You already have the bytes in memory and want the full ImageInfo
ImageProbe.DetectFormat(stream) / (data)You only need to branch on ImageFormat — skip header parsing beyond the format signature

ProbeFile opens and reads the file itself — there’s no need to open a FileStream first just to call Probe(stream).


Batch-probing a directory

Because ImageProbe never throws, a directory scan can call it in a tight loop with no per-file try/catch:

using Aspose.Imaging.Foss;

foreach (var path in Directory.GetFiles("incoming"))
{
    var info = ImageProbe.ProbeFile(path);

    if (info.Format == ImageFormat.Unknown)
    {
        Console.WriteLine($"{path}: not a recognized image format, skipping");
        continue;
    }

    Console.WriteLine($"{path}: {info.Format} {info.Width}x{info.Height}");
}

Any file whose header doesn’t match one of the 11 recognized formats resolves to ImageFormat.Unknown rather than throwing — the loop above never needs a catch block.


The never-throws contract

This isn’t incidental behavior — it’s a deliberate part of the API contract. If a recognized format’s header is truncated or malformed partway through parsing, ImageProbe still returns an ImageInfo with Format set to the format it did recognize; it does not propagate a parse exception to the caller. Only Format is guaranteed — treat every other property as optional regardless of which format was detected.


Common Issues

IssueCauseFix
Constructed ImageInfo doesn’t match a real probe resultManually-constructed instances don’t validate consistency between fieldsOnly use manual construction for test doubles, not as a substitute for probing real files
Calling Probe when DetectFormat would doUnnecessary header parsing when only the format is neededUse DetectFormat if you never read Width/Height/BitDepth/FrameCount
Unknown format for a file you expect to be supportedHeader doesn’t match any of the 11 recognized signatures, or the file is a different format entirelyConfirm the file against the supported formats list

FAQ

Can I subclass ImageInfo or ImageProbe?

No. ImageInfo is sealed and ImageProbe is static — neither is designed for subclassing or instantiation via inheritance.

Is ImageProbe thread-safe?

ImageProbe holds no mutable state between calls — each call to Probe, ProbeFile, or DetectFormat is independent, making concurrent calls from multiple threads safe.

Do I need to dispose of anything?

No. ImageProbe has no IDisposable resources of its own. If you pass a Stream you opened yourself, you remain responsible for disposing that stream — Probe(stream) does not close it.


API Reference Summary

Class / MethodDescription
ImageProbe.ProbeFile(path)Probe a file by path
ImageProbe.Probe(stream)Probe a Stream
ImageProbe.Probe(data)Probe a byte[]
ImageProbe.DetectFormat(stream)Format only, from a Stream
ImageProbe.DetectFormat(data)Format only, from a byte[]
ImageInfo(format, width, height, bitDepth, frameCount)Public constructor — only format is required
ImageInfo.Format / .Width / .Height / .BitDepth / .FrameCountRead-only result properties
ImageFormatEnum: Unknown, Png, Jpeg, Gif, Bmp, WebP, Ico, Tiff, Psd, Emf, Wmf, Dicom

See Also