Note
This example requires to reference the KGySoft.Drawing package. When targeting .NET 7 or later it can be executed on Windows only.
T ReadRaw<T>(
int x
)
where T : struct, new()
Function ReadRaw(Of T As {Structure, New}) (
x As Integer
) As T
generic<typename T>
where T : value class, gcnew()
T ReadRaw(
int x
)
abstract ReadRaw :
x : int -> 'T when 'T : struct, new()
This method returns the actual raw underlying data as arbitrary unmanaged value type (a value type is unmanaged if contains no managed references). T can have any size so you using this method can access multiple pixels or individual color channels.
To determine the row width in bytes use the RowSize property of the parent IReadableBitmapData instance.
To determine the actual pixel size use the PixelFormat property of the parent IReadableBitmapData instance.
using (IReadWriteBitmapData bitmapData = BitmapDataFactory.CreateBitmapData(
new Size(1, 1), KnownPixelFormat.Format32bppPArgb))
{
// setting a white pixel with 50% alpha:
bitmapData.SetPixel(0, 0, Color.FromArgb(128, 255, 255, 255));
// reading the raw premultiplied color value:
Console.WriteLine(bitmapData[0].ReadRaw<Color32>(0)); // 80808080 [A=128; R=128; G=128; B=128]
// but reading it by the indexer (or by GetPixel/GetColor) transforms the color back:
Console.WriteLine(bitmapData[0][0]); // 80FFFFFF [A=128; R=255; G=255; B=255]
}
ArgumentOutOfRangeException | x is less than zero or the memory location of the value (considering the size of T) at least partially exceeds the bounds of the current row. |