BitmapDataFactoryCreateBitmapDataT(T, Size, Int32, CustomBitmapDataConfig) Method

Creates an IReadWriteBitmapData instance with a custom non-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,
	CustomBitmapDataConfig 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, CustomBitmapDataConfig) 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  CustomBitmapDataConfig
The configuration for the custom pixel format. At least one getter or 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 pixel format. You need to specify a PixelFormatInfo and at least one delegate setter or getter delegate to be called whenever a pixel is get or set. In this overload these can be done by assigning a CustomBitmapDataConfig instance to the customBitmapDataConfig parameter. The desired PixelFormatInfo should be set in the PixelFormat property, whereas for the getter and setter methods you can select the ones with the color types that fit the best for your pixel format.

  Tip

It is enough to set only one getter and/or setter with the best matching color type. For example, if you set the RowGetColor64 property only, which returns the pixels as Color64 values, then all of the other pixel-reading methods will use this delegate and will convert the result from Color64.

A custom pixel format can have any BitsPerPixel value between 1 and 128. A typical bits-per-pixel value is a power of two; 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.

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.
-or-
WorkingColorSpace in customBitmapDataConfig is not one of the defined values.
ArgumentExceptionbuffer is too small for the specified size, PixelFormat and stride
-or-
stride is not a multiple of the size of T
-or-
PixelFormat in customBitmapDataConfig is indexed or its BitsPerPixel is 0.
-or- None of the pixel getter/setter delegates are specified in customBitmapDataConfig.

See Also