SixLabors/ImageSharp · error · ArgumentNullException

source

Error message

source

What it means

GetPixelMemoryGroup on ImageFrame<TPixel> throws ArgumentNullException when the source frame reference is null. It uses the null-coalescing throw pattern: source?.PixelBuffer... ?? throw new ArgumentNullException(nameof(source)).

Solutions

  1. Ensure the ImageFrame<TPixel> reference is non-null before calling GetPixelMemoryGroup
  2. Check how the frame was obtained (indexer, search, factory) and validate the result
  3. Throw/validate early with Guard.NotNull or an explicit null check at your API boundary

Example fix

// before
var group = maybeFrame.GetPixelMemoryGroup();
// after
if (maybeFrame is null) throw new InvalidOperationException("Frame not found");
var group = maybeFrame.GetPixelMemoryGroup();
Defensive patterns

Strategy: type-guard

Validate before calling

if (frame is null) throw new InvalidOperationException("Frame must be resolved before pixel access");

Type guard

bool HasFrame(ImageFrame<TPixel>? frame) => frame is not null;

Try / catch

try { var group = frame.GetPixelMemoryGroup(); }
catch (ArgumentNullException ex) { logger.LogError(ex, "Frame was null"); }

Prevention

When it happens

Trigger: Calling frame.GetPixelMemoryGroup() (or image.Frames.RootFrame chain) where the ImageFrame<TPixel> variable is null — typically a failed lookup, an uninitialized field, or passing null through a generic helper.

Common situations: Storing frames in a dictionary/collection and retrieving with a key that yields null; passing a null frame into a generic pixel-processing helper; calling image.Frames.RootFrame logic on a wrapper that defaulted to null.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/0f8a1ed6450f356f. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Advanced/AdvancedImageExtensions.cs:102

    /// <param name="visitor">The image visitor.</param>
    public static void AcceptVisitor(this ImageFrame source, IImageFrameVisitor visitor)
        => source.Accept(visitor);

    /// <summary>
    /// Gets the representation of the pixels as a <see cref="IMemoryGroup{T}"/> containing the backing pixel data of the image
    /// stored in row major order, as a list of contiguous <see cref="Memory{T}"/> blocks in the source image's pixel format.
    /// </summary>
    /// <param name="source">The source image.</param>
    /// <typeparam name="TPixel">The type of the pixel.</typeparam>
    /// <returns>The <see cref="IMemoryGroup{T}"/>.</returns>
    /// <remarks>
    /// Certain Image Processors may invalidate the returned <see cref="IMemoryGroup{T}"/> and all it's buffers,
    /// therefore it's not recommended to mutate the image while holding a reference to it's <see cref="IMemoryGroup{T}"/>.
    /// </remarks>
    /// <exception cref="ArgumentNullException">Thrown when the <paramref name="source"/> in <see langword="null"/>.</exception>
    public static IMemoryGroup<TPixel> GetPixelMemoryGroup<TPixel>(this ImageFrame<TPixel> source)
        where TPixel : unmanaged, IPixel<TPixel>
        => source?.PixelBuffer.FastMemoryGroup.View ?? throw new ArgumentNullException(nameof(source));

    /// <summary>
    /// Gets the representation of the pixels as a <see cref="IMemoryGroup{T}"/> containing the backing pixel data of the image
    /// stored in row major order, as a list of contiguous <see cref="Memory{T}"/> blocks in the source image's pixel format.
    /// </summary>
    /// <param name="source">The source image.</param>
    /// <typeparam name="TPixel">The type of the pixel.</typeparam>
    /// <returns>The <see cref="IMemoryGroup{T}"/>.</returns>
    /// <remarks>
    /// Certain Image Processors may invalidate the returned <see cref="IMemoryGroup{T}"/> and all it's buffers,
    /// therefore it's not recommended to mutate the image while holding a reference to it's <see cref="IMemoryGroup{T}"/>.
    /// </remarks>
    /// <exception cref="ArgumentNullException">Thrown when the <paramref name="source"/> in <see langword="null"/>.</exception>
    public static IMemoryGroup<TPixel> GetPixelMemoryGroup<TPixel>(this Image<TPixel> source)
        where TPixel : unmanaged, IPixel<TPixel>
        => source?.Frames.RootFrame.GetPixelMemoryGroup() ?? throw new ArgumentNullException(nameof(source));

    /// <summary>

View on GitHub (pinned to 59ce6af6fc)