我需要使用C ++代码创建monocrome .bmp签名文件,但无法创建

时间:2017-12-26 14:02:09

标签: c++ winapi visual-c++

我需要使用C ++代码创建monocrome .bmp签名文件,但无法创建。下面是代码片段。

bool SaveBMP ( BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile )
{
    // declare bmp structures 
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER info;

    // andinitialize them to zero
    memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) );
    memset ( &info, 0, sizeof (BITMAPINFOHEADER ) );

    // fill the fileheader with data
    bmfh.bfType = 0x4d42;       // 0x4d42 = 'BM'
    bmfh.bfReserved1 = 0;
    bmfh.bfReserved2 = 0;
    bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;
    bmfh.bfOffBits = 0x36;      // number of bytes to start of bitmap bits

    // fill the infoheader

    info.biSize = sizeof(BITMAPINFOHEADER);
    info.biWidth = width;
    info.biHeight = height;
    info.biPlanes = 1;          // we only have one bitplane
    info.biBitCount = 1;        // RGB mode is 24 bits
    info.biCompression = BI_RGB;    
    info.biSizeImage = 0;       // can be 0 for 24 bit images
    info.biXPelsPerMeter = 0x0ec4;     // paint and PSP use this values
    info.biYPelsPerMeter = 0x0ec4;     
    info.biClrUsed = 0;         // we are in RGB mode and have no palette
    info.biClrImportant = 0;    // all colors are important

    // now we open the file to write to
    HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ,
         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
    if ( file == NULL )
    {
        CloseHandle ( file );
        return false;
    }

    // write file header
    unsigned long bwritten;
    if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == false )
    {   
        CloseHandle ( file );
        return false;
    }
    // write infoheader
    if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == false )
    {   
        CloseHandle ( file );
        return false;
    }
    // write image data
    if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == false )
    {   
        CloseHandle ( file );
        return false;
    }

    // and close file
    CloseHandle ( file );

    return true;
}

** //当我运行此代码时,创建的位图文件为空白,未显示签名,只能显示签名文件的背景。 **

1 个答案:

答案 0 :(得分:1)

Monochrom位图需要调色板。在这种情况下,调色板大小为8个字节(2 RGBQUAD个值)

此额外的8个字节应添加到BITMAPFILEHEADER文件大小。

BITMAPFILEHEADER之后,您正在使用BITMAPINFOHEADER,其中不包含有关调色板的信息。您必须直接使用BITMAPINFO而不是BITMAPINFOHEADER

BITMAPINFO* bmpinfo = (BITMAPINFO*)malloc(palette_size + sizeof(BITMAPINFOHEADER));
bmpinfo->bmiHeader = { sizeof(BITMAPINFOHEADER), 
    width, height, 1, bitcount, BI_RGB, 0, 0, 0, 0, 0 };

如果没有为单色位图检索Buffer,那么您不能简单地在此保存功能中更改bitcount并期望一切正常。

if ( file == NULL )// <-error
{
    CloseHandle ( file );
    return false;
}

如果CreateFile失败,则返回值为INVALID_HANDLE_VALUE。您可以从API函数的在线文档中找到这些值。修正后的版本应为

if(file != INVALID_HANDLE_VALUE)
{
    unsigned long bwritten;
    WriteFile(file, &bmfh, sizeof(BITMAPFILEHEADER), &bwritten, NULL);
    WriteFile(file, bmpinfo, 8 + sizeof(BITMAPINFOHEADER), &bwritten, NULL);
    WriteFile(file, Buffer, paddedsize, &bwritten, NULL);
    CloseHandle(file);
    return true;
}
return false;