Jump to content
  • Advertisement
Sign in to follow this  

Using GDI functions and saving them as a bitmap

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to be able to use GDI functions to make graphics and save it to a bitmap file. However, the bitmap can be much larger than the screen, so I don't think I can just do it on a screen DC and then copy it to a bitmap. The following code is a test to see how to write something to a bitmap. I try to do a square in the top left of the bitmap. There is a square written in the correct-ish area, but it is just a mix of black and white pixels. I am using the bitsperpixel of the DC, which isn't monochrome. Also, the area of the bitmap where the rectangle isn't is solid black. Here is the code:
int Width = 640*Screens;
int Height = 480;

HDC hScreenDC = GetWindowDC(0);
HDC BmpHDC = CreateCompatibleDC(hScreenDC);
HBITMAP hmemBM = CreateCompatibleBitmap(BmpHDC, Width, Height);
HBRUSH hBrush = CreateSolidBrush(RGB(195,55,87));


int BytesPerPixel = (int)GetDeviceCaps(BmpHDC,BITSPIXEL);

HGLOBAL hpxldata = GlobalAlloc(GMEM_FIXED, Width*Height*BytesPerPixel);
void FAR* lpvpxldata = GlobalLock(hpxldata);

bmInfo.bmiHeader.biSize = 40;
bmInfo.bmiHeader.biWidth = Width;
bmInfo.bmiHeader.biHeight = Height;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = BytesPerPixel;//24
bmInfo.bmiHeader.biCompression = 0;
bmInfo.bmiHeader.biSizeImage = 0;
bmInfo.bmiHeader.biXPelsPerMeter = 0;
bmInfo.bmiHeader.biYPelsPerMeter = 0;
bmInfo.bmiHeader.biClrUsed = 0;
bmInfo.bmiHeader.biClrImportant = 0;

bmFileHeader.bfType = 19778;
bmFileHeader.bfSize = (Width*Height*BytesPerPixel) + 40 + 14;
bmFileHeader.bfReserved1 = 0;
bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = 54;

GetDIBits(BmpHDC, hmemBM, 0, Height, lpvpxldata, &bmInfo, DIB_RGB_COLORS);

FILE *fp = fopen("Bitmap.bmp","wb");

ReleaseDC(0, hScreenDC);

Any ideas? Also, after looking around a bit, CreateDIBSection() might be a better alternative? I just have no clue how to implement it, there doesn't seem to be any good documentation. Thank you, Dev578

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!