In Java i would do something like this
int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
where image is a BufferedImage and then alter the pixels in there and make my own blitting methods but how am i supposed to do something like this in C#? I know i can use a Bitmap to replace BufferedImage in C# but i’m not sure about making a reference to the data like shown above.
You would use
LockbitsandMarshal.Copy:Note: The code is basically the example code from the
LockBitsdocumentation page, but the code has a limitation. It assumes that theStridevalue is positive, i.e. that the image is not stored upside down in memory, eventhough the use ofMath.Abson theStridevalue indicates that the person writing the code was aware that theStridevalue can be negative.For a negative
Stridevalue theScan0can not be used as the starting address of a continuos memory block, as it’s the address of the first scan line. The starting address of the memory block would be the starting address of the last line in the image, not the first.That address would be
bmpData.Scan0 + bmpData.Stride * (bmp.Height - 1)