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
#Initializes a new instance of the BitmapData class
Parameters:
| 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. |
Properties
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
#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.
PremultipliedAlpha
#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
#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
#Disposes the brush
Parameters:
| disposing (bool) | If set to true dispose was called explicitly, otherwise specify false if calling from a finalizer |
GetPixel
#Gets the color of the pixel at the specified coordinates.
GetPixel
#Gets the color of the pixel at the specified
positionParameters:
| position (Point) | Position to get the color of the pixel. |
Returns: The color of the pixel.
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
#Sets the pixel color at the specified coordinates.
SetPixels
#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.Parameters:
| pixels (IEnumerable<Color>) | Enumerator that returns each pixel of the bitmap from top to bottom |
TranslateArgbToData
#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
Parameters:
| argb (int) | ARGB pixel value to translate into the platform-specific format |
Returns: Platform-specific format of the pixels that can be set directly onto the data
TranslateDataToArgb
#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
Parameters:
| bitmapData (int) | Platform specific bitmap data for a pixel to translate |
Returns: Translated ARGB value from the bitmap data