69,336
社区成员
发帖
与我相关
我的任务
分享
int main()
{
const DWORD uWidth = 720, uHeight = 72;
PBITMAPINFO pbmi = (PBITMAPINFO) LocalAlloc (LPTR, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD)*2);
pbmi->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = uWidth;
pbmi->bmiHeader.biHeight = uHeight;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 1;
pbmi->bmiHeader.biSizeImage = ((uWidth + 31) & ~31) / 8 * uHeight;
pbmi->bmiColors[0].rgbBlue = 0;
pbmi->bmiColors[0].rgbGreen = 0;
pbmi->bmiColors[0].rgbRed = 0;
pbmi->bmiColors[1].rgbBlue = 0;
pbmi->bmiColors[1].rgbGreen = 0;
pbmi->bmiColors[1].rgbRed = 255;
HDC hDC = CreateCompatibleDC(NULL);
void * pvBits = NULL;
HBITMAP hBitmap = CreateDIBSection (hDC, pbmi, DIB_RGB_COLORS, &pvBits, NULL, 0);
SelectObject (hDC, hBitmap);
HFONT hFont = CreateFont (72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "楷体");
SelectObject (hDC, hFont);
BitBlt (hDC, 0, 0, uWidth, uHeight, NULL, 0, 0, BLACKNESS);
string content = "好好学习 天天向上";
TextOut(hDC, 54, 0, content.c_str(), content.length());
BITMAPFILEHEADER bmfh;
bmfh.bfType = *(PWORD) "BM";
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2;
bmfh.bfSize = bmfh.bfOffBits + pbmi->bmiHeader.biSizeImage;
HANDLE hFile = CreateFile ("test.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwWritten;
WriteFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile (hFile, pbmi, sizeof (BITMAPINFOHEADER) + sizeof (RGBQUAD) * 2, &dwWritten, NULL);
WriteFile (hFile, pvBits, pbmi->bmiHeader.biSizeImage, &dwWritten, NULL);
CloseHandle (hFile);
}
DeleteObject (hFont);
DeleteObject (hBitmap);
DeleteDC (hDC);
LocalFree (pbmi);
return 0;
}