# writing bitmap

This topic is 3268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. So I'm able to actually write the image to a bitmap and it looks correct except that there are thick "borders/padding" on the top and on the right side. Does anyone know what's causing that? The dimension of the image is 200x200. Length is equal to the Width (hence "LengthAndWidth").
BITMAPFILEHEADER bmpfh;
bmpfh.bfReserved1 = bmpfh.bfReserved2 = 0;
bmpfh.bfType = 0x4D42;

bmpih.biWidth = bmpih.biHeight = LengthAndWidth;
bmpih.biPlanes = 1;
bmpih.biBitCount = 24;
bmpih.biCompression = BI_RGB;//0;
bmpih.biSizeImage = sizeof(RGBTRIPLE)*LengthAndWidth*LengthAndWidth;
bmpih.biXPelsPerMeter = bmpih.biYPelsPerMeter = 0;//2400;
bmpih.biClrImportant = 0;
bmpih.biClrUsed = 0;

ofstream writer( OutputFileName, ofstream::binary );

unsigned remainder = LengthAndWidth % 4;

writer.write( (char*)image, bmpih.biSizeImage );
writer.close();


RGBTRIPLE *image = new RGBTRIPLE[LengthAndWidth*LengthAndWidth];

float R, G, B;
for( int j = 0, k = rect.bottom-1; j < rect.bottom; j++, k-- )
for( int i = 0; i < rect.right; i++ )
{
R = G = B = 0.f;

...

image[ k*LengthAndWidth + i ].rgbtRed = R;
image[ k*LengthAndWidth + i ].rgbtGreen = G;
image[ k*LengthAndWidth + i ].rgbtBlue = B;
}


I go through the pixels from top to bottom, but since the image is flipped in a bitmap, I stored the pixel data starting from the other end.

##### Share on other sites
I think you should set the bfSize to the total size of the file (I see you have commented that out). According to the documentation, biSizeImage can be zero, though I don't know if it matters.

You can also use the BITMAPCOREHEADER Structure instead of the BITMAPINFOHEADER, which is a lighter version with fewer options.

##### Share on other sites
Yeah.. so as you might have already guessed, I tried setting the bfSize to the entire file but that didn't work.

##### Share on other sites
If you don't want the image to be stored flipped upside-down you can make the height negative. Just beware that this is only for BI_RGB which you happen to be using.

The grey area at the side and right are suspiciously close to the size you'd be left with if you subtract the total height and width of the non-client area of a window. Thus I suspect you are trying to create a window of exactly 200x200, and then proceed to obtain the client area of this window for your rect which will be reduced to about the proportions in your image. If you want the client rectangle of your window to be 200x200 then you need to create a window larger than that by using AdjustWindowRectEx to calculate the correct window size.

If you check the values of rect.right and rect.bottom with the debugger, you'll see that they are not 200.

##### Share on other sites
Quote:
 Original post by iMalcIf you don't want the image to be stored flipped upside-down you can make the height negative. Just beware that this is only for BI_RGB which you happen to be using.The grey area at the side and right are suspiciously close to the size you'd be left with if you subtract the total height and width of the non-client area of a window. Thus I suspect you are trying to create a window of exactly 200x200, and then proceed to obtain the client area of this window for your rect which will be reduced to about the proportions in your image. If you want the client rectangle of your window to be 200x200 then you need to create a window larger than that by using AdjustWindowRectEx to calculate the correct window size.If you check the values of rect.right and rect.bottom with the debugger, you'll see that they are not 200.

Thank you very much! You were exactly right about everything.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
13
5. 5

• 14
• 10
• 24
• 9
• 57
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007611
• ### Who's Online (See full list)

There are no registered users currently online

×