krebstar
krebstar

Reputation: 4036

How to add padding bytes to a bitmap?

Lets say I have some raster data I want to write to a file.. Now I want to write it as a bmp file..

This data happens to be not DWORD aligned, which, if I understand correctly, needs to be padded with enough bytes to reach the next DWORD..

However, when I try to pad it with this code:

bmFile.Write(0x0, (4-(actualWidth%4)));

I get an error.. If I try to debug (I'm using MSVC++ 6.0), the next statement points to an ASSERT in CFile::Write that asserts the first parameter is NULL.. So this fails..

How should I pad it? should I write out:

bmFile.Write("0x0"(4-(actualWidth%4)));

instead? or would this be treated literally...?

Thanks..

Upvotes: 1

Views: 3271

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992707

Perhaps try:

bmFile.Write("\0\0\0\0", (4-(actualWidth%4)));

Your first example is, as you say, trying to write data pointed to by a null pointer. Your second example would write from the bytes '0', 'x', '0' which have ASCII values 0x30, 0x78, 0x30, which is probably not what you intend.

Upvotes: 3

Related Questions