BitmapData

References
public abstract class BitmapData : IDisposable
Bitmap data information when accessing a Bitmap's data directly
Remarks: The bitmap data is accessed through Bitmap.Lock(), which locks the data for direct access using the Data pointer. Ensure you call dispose the instance when you are done accessing or writing the data, otherwise the bitmap may be left in an unusable state.

Constructors

BitmapData

#
protected BitmapData(Image image, nint data, int scanWidth, int bitsPerPixel, object controlObject, bool premultipliedAlpha)
Initializes a new instance of the BitmapData class
image (Image)Image this data is for
data (nint)Pointer to the bitmap data
scanWidth (int)Width of each scan row, in bytes
bitsPerPixel (int)Bits per pixel
controlObject (object)Platform specific object for the bitmap data (if any)
premultipliedAlpha (bool)true to specify that the RGB components are premultiplied with the alpha component, false otherwise.

BitmapData

#
protected BitmapData(Image image, nint data, int scanWidth, int bitsPerPixel, object controlObject)
Initializes a new instance of the BitmapData class
image (Image)Image this data is for
data (nint)Pointer to the bitmap data
scanWidth (int)Width of each scan row, in bytes
bitsPerPixel (int)Bits per pixel
controlObject (object)Platform specific object for the bitmap data (if any)

Properties

BitsPerPixel

#
public int BitsPerPixel { get; }
Gets the bits per pixel

BytesPerPixel

#
public int BytesPerPixel { get; }
Gets the bytes per pixel

ControlObject

#
public object ControlObject { get; }
Gets the platform-specific control object for the bitmap data

Data

#
public nint Data { get; }
Gets the pointer to the data of the bitmap
Remarks: This does not include any headers, etc. so it directly points to the beginning of the data. Each row may not be on a pixel boundary, so to increment to the next row, use the ScanWidth to increment the pointer to the next row.

Flipped

#
public virtual bool Flipped { get; }
Gets a value indicating that the data is flipped (upside down)
Remarks: Some platforms may handle memory bitmaps in a flipped fashion, such that the top of the image is at the bottom of the data. If this is true, then the starting row of the data is the bottom row of the image.

Image

#
public Image Image { get; }
Gets the image this data is for

PremultipliedAlpha

#
public bool PremultipliedAlpha { get; }
Gets a value indicating the RGB components are premultiplied with the alpha component
Remarks: Note that the TranslateDataToArgb(int) and TranslateArgbToData(int) take this into account when translating data. Otherwise, you can use this value do determine how to do your own translation.

ScanWidth

#
public int ScanWidth { get; }
Gets the width (in bytes) of each scan line (row) of pixel data
Remarks: When advancing to the next row, use this to increment the pointer. The number of bytes for each row might not be equivalent to the bytes per pixel multiplied by the width of the image.

Methods

Dispose

#
public void Dispose()
Releases all resource used by the BitmapData object.
Remarks: Call Dispose() when you are finished using the BitmapData. The Dispose() method leaves the BitmapData in an unusable state. After calling Dispose(), you must release all references to the BitmapData so the garbage collector can reclaim the memory that the BitmapData was occupying.

Dispose

#
protected virtual void Dispose(bool disposing)
Disposes the brush
disposing (bool)If set to true dispose was called explicitly, otherwise specify false if calling from a finalizer

GetPixel

#
public abstract Color GetPixel(int x, int y)
Gets the color of the pixel at the specified coordinates.
x (int)The x coordinate to get the color from.
y (int)The y coordinate to get the color from.
The color of the pixel.

GetPixel

#
public Color GetPixel(Point position)
Gets the color of the pixel at the specified position
position (Point)Position to get the color of the pixel.
The color of the pixel.

GetPixels

#
public IEnumerable<Color> GetPixels()
Gets an enumerable of pixels for each scan line from top to bottom.
Remarks: You can use this to translate the pixel data into an array, e.g.
int[] argbData = bitmapData.GetPixels().Select(p => p.ToArgb()).ToArray();

SetPixel

#
public abstract void SetPixel(int x, int y, Color color)
Sets the pixel color at the specified coordinates.
x (int)The x coordinate of the pixel to set.
y (int)The y coordinate of the pixel to set.
color (Color)Color to set the pixel to.

SetPixel

#
public void SetPixel(Point position, Color color)
Sets the pixel color at the specified position.

SetPixels

#
public void SetPixels(IEnumerable<Color> pixels)
Use this to set the pixels of the bitmap from an array or other source.
Remarks: For example, you can use this to set the pixel data from an int array like so:
int[] myData;
bitmapData.SetPixels(myData.Select(Color.FromArgb));
This will set all the pixels up till the end of the enumeration. If there isn't enough data to fill the bitmap entirely, it will not set any additional pixels.
pixels (IEnumerable<Color>)Enumerator that returns each pixel of the bitmap from top to bottom

TranslateArgbToData

#
public abstract int TranslateArgbToData(int argb)
Translates a 32-bit ARGB value to the platform specific pixel format value
Remarks: Use this method to translate an ARGB (Alpha in most significant) to the value required by the bitmap for the pixel. Each platform can have a different pixel format, and this allows you to abstract setting the data directly. The ARGB value can be easily retrieved using Color.ToArgb(). For non-alpha bitmaps, the alpha component will be ignored
argb (int)ARGB pixel value to translate into the platform-specific format
Platform-specific format of the pixels that can be set directly onto the data

TranslateDataToArgb

#
public abstract int TranslateDataToArgb(int bitmapData)
Translates the platform specific pixel format to a 32-bit ARGB value
Remarks: Use this method to translate an value from the bitmap data to a 32-bit ARGB (Alpha in most significant byte). Each platform can have a different pixel format, and this allows you to abstract getting the data into a 32-bit colour. The ARGB value can be easily handled using Color. For non-alpha bitmaps, the alpha component will be ignored
bitmapData (int)Platform specific bitmap data for a pixel to translate
Translated ARGB value from the bitmap data