does anybody know what am i doing wrong. I'm trying to change pixel color by (x, y) coordinates on 1D byte array, but i cannot succeed, pixels are scattered everywhere around.
This is my file header:
file.Write(
new byte[62]
{
0x42, 0x4d,
0x3e, 0xf4, 0x1, 0x0,
0x0, 0x0, 0x0, 0x0,
0x3e, 0x0, 0x0, 0x0,
0x28, 0x0, 0x0, 0x0,
0xe8, 0x3, 0x0, 0x0,
0xe8, 0x3, 0x0, 0x0,
0x1, 0x0,
0x1, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0xf4, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0xff, 0x0, 0x0, 0x0,
0x0, 0x0, 0xff, 0x0});
This is my byte array:
int width = 1000;
int height = 128;
var t = new byte[width * height];
This is the method to set pixel (I'm pretty sure the calculation is wrong, I might need to include more of my map information, but I'm not sure what):
static void SetPixel(byte[] img, int x, int y, int width, int height)
{
if (x >= 0 && x < width && y >= 0 && y < height)
{
Console.WriteLine($"{x} {y}");
int index = (y * width + x);
img[index] = 0x1;
}
}
Lastly, here's how I call the method:
SetPixel(t, 1, 0, width, height);
I've tried many solutions I've found online, none of them helps. Tried playing around with width, height.
If I interpret the data correctly, you are creating a bitmap file. You have set the value
Bits per Pixelto 1; however, you are adding one byte per pixel. Therefore, you should set this value to 8 (one byte = 8 bits). However, this requires a color palette to be stored.If you really want 1-bit colors (monochrome), then you must divide the byte size by 8 and set pixels by setting single bits inside the bytes.
The row size must be a multiple of 4 bytes. This function calculates it:
We create the pixel array with:
We could calculate the file size manually, but we can get the corresponding bytes like this...
(You could also use the modern BinaryPrimitives Class instead of
BitConverter.)... and set them in the file header like this:
We set a pixel with:
Also, you have set
Image Heightin the DIB header to 1000 = 0xe8, 0x03 instead of 128 = 0x80.I just made tests and this seems to work so far.
See also: