C#C
C#2y ago
16 replies
SamwiseTheBrave

✅ An issue with converting byte array to a Bitmap with 32bppArgb image format

Hi everyone. I need help (desperately).
I'm creating a simple raycaster game, which means I need to clear screen and fill it again every frame.
I draw by puting color data into array and converting this array into a bitmap.
The following code sets bitmap pixel data to data taken from array.

Rectangle copyRect = new Rectangle(0, 0, Width, Height);
BitmapData data = imageBuffer.LockBits(copyRect, ImageLockMode.ReadWrite, imageBuffer.PixelFormat);
IntPtr bPointer = data.Scan0;
Marshal.Copy(buffer, 0, bPointer, buffer.Length);
imageBuffer.UnlockBits(data);

Width
and
Height
are variables specifying size of the bitmap I want to edit.
imageBuffer
is a Bitmap I want to edit.
buffer
is my pixel color data (
byte[]
), stored in this order: BGRA. This array is reset every frame by setting all elements to zero.

Next, I draw
imageBuffer
to the screen. It works fine as long as
imageBuffer.PixelFormat
is
Format32bppRgb
.
And here comes my issue: when I change
imageBuffer.PixelFormat
to
Format32bppArgb
, weird thing happens (as shown on the second picture). Image is glitched in a odd way, and the same happens to strings in the left up corner, which aren't being drawn on the buffer, but on the graphics, after the
imageBuffer
is modified.

Some interesting notes (all experiments done with
Format32bppArgb
pixel format):
- when I change order in the
buffer
to ABGR I get less blurry effect and blurrines disappears when camera is not moving.
- when I change order in the
buffer
to ARGB, blur gets stronger, but also disappears after few seconds of standing still

In both cases colors are off, because I changed order of channels in the buffer.

On the first picture you can see how it looks with
Format32bppRgb
.
On the second picture you see a glitched image buffer, using
Format32bppRgb
.
obraz.png
obraz.png
Was this page helpful?