ビットマップの作成と保存


static void savetobmp(char * filename, BITMAPINFOHEADER* bmih, void * buffer, int lBufferLen)
{
    BITMAPFILEHEADER bmpFileHeader;
    ZeroMemory(&bmpFileHeader, sizeof(bmpFileHeader));
    bmpFileHeader.bfType = 'MB';
    bmpFileHeader.bfSize=sizeof(bmpFileHeader)+lBufferLen+sizeof(BITMAPINFOHEADER);
    bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);

    FILE * fout = fopen(filename, "wb");
    fwrite(&bmpFileHeader, 1, sizeof(bmpFileHeader), fout);
    fwrite(bmih, 1, sizeof(*bmih), fout);
    fwrite(buffer, 1, lBufferLen, fout);
    fclose(fout);
}
static void CreateClockBitmap(const char * filename, int width, int height)
{
    HDC hDc = CreateCompatibleDC(NULL);// CDC

    BITMAPINFO bmpInfo; memset(&bmpInfo, 0, sizeof(bmpInfo));  // 
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = (int)width;// 
    bmpInfo.bmiHeader.biHeight = (int)height;// 
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 24; // 24bit 
    bmpInfo.bmiHeader.biCompression = BI_RGB;

    void* pArray = NULL;
    HBITMAP hbmp = CreateDIBSection(NULL, &bmpInfo, DIB_RGB_COLORS, &pArray, NULL, 0);
    //  
    UINT uiTotalBytes = ((width * 24 + 31) & ~31)/8 * height;
    
    //  
    memset(pArray, 0x00, uiTotalBytes);

    HBITMAP hOldBmp = (HBITMAP)SelectObject(hDc, hbmp); // 
    
    //  
    HFONT hFont; LOGFONT lFont; memset(&lFont, 0, sizeof(LOGFONT)); 
    //   
    int nNumerator = ::GetDeviceCaps(hDc, LOGPIXELSY); 
    lFont.lfHeight = ::MulDiv( 25, -nNumerator, 72); 
    lFont.lfWeight = 25; 
    //   
    hFont = ::CreateFontIndirect(&lFont); 
    //   
    HGDIOBJ hOldFont = ::SelectObject(hDc, hFont);

    RECT rc; rc.left = 0; rc.top = 0;
    rc.right = width; rc.bottom = height;
    char timestamp [100] = ""; 
    SYSTEMTIME st; GetLocalTime(&st); sprintf(timestamp, "%02hu:%02hu:%02hu", st.wHour, st.wMinute, st.wSecond);
    //  
    SetTextColor(hDc, RGB(0xff, 0xFF, 0xFF));
    //  
    SetBkColor(hDc, RGB(0x00, 0x00, 0x00));
    //  
    DrawTextA(hDc, timestamp, strlen(timestamp), &rc, DT_CENTER|DT_WORDBREAK);

    //  
    savetobmp(filename, &bmpInfo.bmiHeader, pArray, uiTotalBytes);

    //      
    SelectObject(hDc, hOldBmp);
    SelectObject(hDc, hOldFont);
    DeleteObject(hFont);
    DeleteObject(hbmp);
    DeleteDC(hDc);
    return ;
}