繁体   English   中英

C ++位图编辑

[英]C++ bitmap editing

我试图打开一个位图文件,对其进行编辑,然后将编辑后的版本另存为新文件。 最终这将使使用隐写术变得混乱。 我正在尝试保存位图信息,但是保存的文件无法打开。 编译或运行时无错误。 它可以正常打开,其余功能正常工作。

void cBitmap::SaveBitmap(char * filename)
{
    // attempt to open the file specified
    ofstream fout;

    // attempt to open the file using binary access
    fout.open(filename, ios::binary);

    unsigned int number_of_bytes(m_info.biWidth * m_info.biHeight * 4);
    BYTE red(0), green(0), blue(0);

    if (fout.is_open())
    {
        // same as before, only outputting now
        fout.write((char *)(&m_header), sizeof(BITMAPFILEHEADER));
        fout.write((char *)(&m_info), sizeof(BITMAPINFOHEADER));

        // read off the color data in the bass ackwards MS way
        for (unsigned int index(0); index < number_of_bytes; index += 4)
        {
            red = m_rgba_data[index];
            green = m_rgba_data[index + 1];
            blue = m_rgba_data[index + 2];

            fout.write((const char *)(&blue), sizeof(blue));
            fout.write((const char *)(&green), sizeof(green));
            fout.write((const char *)(&red), sizeof(red));
        }


    }
    else
    {
        // post file not found message
        cout <<filename << " not found";
    }
    // close the file
    fout.close();
}

您在每个RGB行之后都缺少填充字节。 每行必须是4字节的倍数。

另外,您是否应该编写24或32位bmp文件? 如果您正在编写24位,则只是缺少填充。 如果您正在编写32位,那么您将丢失每个额外的字节(alpha)。 没有足够的信息来解决您的代码示例,而没有编写支持所有可能选项的完整bmp编写器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM