BitmapDataFactoryCreateBitmapData(IntPtr, Size, Int32, KnownPixelFormat, Palette, FuncPalette, Boolean, Action) Method

Creates an IReadWriteBitmapData instance wrapping an unmanaged buffer and using the specified parameters.


Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 7.1.0
public static IReadWriteBitmapData CreateBitmapData(
	IntPtr buffer,
	Size size,
	int stride,
	KnownPixelFormat pixelFormat,
	Palette? palette,
	Func<Palette, bool>? trySetPaletteCallback = null,
	Action? disposeCallback = null


buffer  IntPtr
The memory address to be used as the underlying buffer for the returned IReadWriteBitmapData. Make sure there is enough allocated memory for the specified size, stride and pixelFormat; otherwise, accessing pixels may corrupt memory or throw an AccessViolationException. If it points to managed memory make sure it is pinned until the returned bitmap data is disposed.
size  Size
The size of the bitmap data to create in pixels.
stride  Int32
The size of a row in bytes. It allows to have some padding at the end of each row. It can be negative for bottom-up layout (ie. when buffer points to the first pixel of the bottom row).
pixelFormat  KnownPixelFormat
The pixel format in buffer and the bitmap data to create.
palette  Palette
If pixelFormat represents an indexed format, then specifies the desired Palette of the returned IReadWriteBitmapData instance. It determines also the BackColor and AlphaThreshold properties of the result.
trySetPaletteCallback  FuncPalette, Boolean  (Optional)
A delegate to be called when the palette is attempted to be replaced by the TrySetPalette method. If buffer belongs to some custom bitmap implementation, it can be used to update its original palette. This parameter is optional.
Default value: .
disposeCallback  Action  (Optional)
A delegate to be called when the returned IReadWriteBitmapData is disposed or finalized. This parameter is optional.
Default value: .

Return Value

An IReadWriteBitmapData instance wrapping the specified buffer and using the provided parameters.


ArgumentNullExceptionbuffer is IntPtr.Zero.
ArgumentOutOfRangeExceptionsize has a zero or negative width or height
pixelFormat is not one of the valid formats
The absolute value of stride is too small for the specified width and pixelFormat.
ArgumentExceptionbuffer is too small for the specified size, pixelFormat and stride
The absolute value of stride is too small for the specified width and pixelFormat
palette is too large for the specified pixelFormat.

See Also