BitmapDataFactoryCreateBitmapDataT(T, Size, Int32, CustomIndexedBitmapDataConfig) Method

Creates an IReadWriteBitmapData instance with a custom indexed pixel format for a preallocated one dimensional array with the specified parameters.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.2.0
C#
public static IReadWriteBitmapData CreateBitmapData<T>(
	T[] buffer,
	Size size,
	int stride,
	CustomIndexedBitmapDataConfig customBitmapDataConfig
)
where T : struct, new()

Parameters

buffer  T
A preallocated array to be used as the underlying buffer for the returned IReadWriteBitmapData. It can be larger than it is required for the specified parameters. If the actual image data starts at some offset use the CreateBitmapDataT(ArraySectionT, Size, Int32, PixelFormatInfo, FuncICustomBitmapDataRowT, Int32, Int32, ActionICustomBitmapDataRowT, Int32, Int32, Palette, FuncPalette, Boolean, Action) overload instead.
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.
customBitmapDataConfig  CustomIndexedBitmapDataConfig
The configuration for the custom pixel format. Either the getter or the setter delegate must be specified. If you can ensure that the delegates don't capture buffer make sure you set the BackBufferIndependentPixelAccess property to .

Type Parameters

T
The type of the elements in buffer.

Return Value

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

Remarks

This method allows creating an IReadWriteBitmapData instance with custom indexed pixel format. You need to specify a PixelFormatInfo and at one or both pixel accessor delegates to be called whenever a pixel is get or set. In this overload these can be done by assigning a CustomIndexedBitmapDataConfig instance to the customBitmapDataConfig parameter. The desired indexed PixelFormatInfo should be set in the PixelFormat property, whereas the getter and setter methods can be assigned to the RowGetColorIndex and RowSetColorIndex properties, respectively.

An indexed custom pixel format can have any BitsPerPixel value between 1 and 16. A typical bits-per-pixel value is a power of two and is not greater than 8; however, any other value can be used if you handle them in the provided delegates.

The getter and setter delegates are always called with an x coordinate meaning the pixel offset in the corresponding row.

  Notes to Implementers

It is highly recommended that the delegates do not use the buffer directly (they don't capture the buffer instance). Instead, they should access the actual data using their ICustomBitmapDataRow argument, which allows reading and writing raw data within the corresponding row, independently from any specific buffer instance. If they do so, then you can set the BackBufferIndependentPixelAccess to true in customBitmapDataConfig, which allows some operations work with better quality and performance.

If the Palette property in customBitmapDataConfig is , then the closest not larger system palette will be used, possibly completed with transparent entries. For example, if PixelFormatInfo.BitsPerPixel is 9 and Palette is , then a Palette with 512 colors will be created where the first 256 colors will be the same as in SystemDefault8BppPalette.

  Note

For that reason it is always recommended to set the Palette property, especially if it has fewer entries than the possible allowed maximum because replacing the palette afterwards by the TrySetPalette extension method allows only to set a palette that has no fewer entries. It's because the TrySetPalette method assumes that the underlying buffer might already have pixels whose indices may turn invalid with a smaller palette.

Exceptions

ArgumentNullExceptionbuffer or customBitmapDataConfig is .
ArgumentOutOfRangeExceptionsize has a zero or negative width or height
-or-
stride is too small for the specified width and PixelFormat.
ArgumentExceptionbuffer is too small for the specified size, PixelFormat and stride
-or-
stride is not a multiple of the size of T
-or-
Palette in customBitmapDataConfig is too large for the specified PixelFormat
-or-
PixelFormat in customBitmapDataConfig is not indexed or its BitsPerPixel is not between 1 and 16.
-or- Neither the getter nor the setter delegate is specified in customBitmapDataConfig.

See Also