dotnet/wpf · error · ArgumentNullException
throw new ArgumentNullException(nameof(section));
Error message
throw new ArgumentNullException(nameof(section));
What it means
CreateBitmapSourceFromMemorySection throws ArgumentNullException when the win32 file-mapping handle passed as `section` is IntPtr.Zero. A WPF InteropBitmap must be backed by a real shared-memory section handle, so a null/zero handle cannot be used to create one.
Solutions
- Check the handle returned from CreateFileMapping/CreateDC/etc. for IntPtr.Zero or failure before passing it to CreateBitmapSourceFromMemorySection
- Ensure the shared-memory section is actually created (correct size, valid PAGE_* protection, flProtect not 0)
- If using System.IO.MemoryMappedFiles, open the MemoryMappedFile first and pass its SafeMemoryMappedFileHandle, verifying it is not IsInvalid/IsClosed
- If the source may legitimately be absent, branch on IntPtr.Zero and skip/defer the bitmap creation instead of calling the API
Example fix
// before
IntPtr section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, size, null);
var source = Imaging.CreateBitmapSourceFromMemorySection(section, w, h, PixelFormats.Pbgra32, stride, 0);
// after
IntPtr section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, size, null);
if (section == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateFileMapping failed");
var source = Imaging.CreateBitmapSourceFromMemorySection(section, w, h, PixelFormats.Pbgra32, stride, 0); Defensive patterns
Strategy: validation
Validate before calling
if (section == IntPtr.Zero) throw new InvalidOperationException("Shared-memory section handle not created");
// else safe to call Imaging.CreateBitmapSourceFromMemorySection(...) Type guard
static bool IsValidSectionHandle(IntPtr h) => h != IntPtr.Zero;
Try / catch
try { var bmp = Imaging.CreateBitmapSourceFromMemorySection(section, w, h, fmt, stride, off); }
catch (ArgumentNullException ex) { /* handle missing section handle */ } Prevention
- Always check native CreateFileMapping return values and Marshal.GetLastWin32Error
- Verify SafeMemoryMappedFileHandle.IsInvalid/IsClosed before interop
- Create the section with a nonzero size and valid page protection
When it happens
Trigger: Calling Imaging.CreateBitmapSourceFromMemorySection with a handle that is IntPtr.Zero — typically because CreateFileMapping (or the memory-mapped-file SafeFileHandle) returned null/failed and the failure was never checked before passing the handle on.
Common situations: Interoperating with GDI/memory-mapped buffers shared with another process; the foreign process never created or closed the section, CreateFileMapping failed due to insufficient permissions or memory, or a NativeMemory/MemoryMappedFile handle was default-initialized instead of opened.
Related errors
- ArgumentNullException (buffer/sourceBuffer was IntPtr.Zero)
- 0x80070057
- annotation component
- ArgumentNullException
- ArgumentNullException(argName)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8aebcecfb641a810.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs:106
/// <param name="pixelHeight"></param>
/// <param name="format"></param>
/// <param name="stride"></param>
/// <param name="offset"></param>
/// <remarks>
/// Callers must have UnmanagedCode permission to call this API.
/// </remarks>
public static unsafe BitmapSource CreateBitmapSourceFromMemorySection(
IntPtr section,
int pixelWidth,
int pixelHeight,
Media.PixelFormat format,
int stride,
int offset)
{
if (section == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(section));
}
return new InteropBitmap(section, pixelWidth, pixelHeight, format, stride, offset);
}
}
}
View on GitHub (pinned to 81131a70a4)