将数组写入位图 c++

Writing array into bitmap c++

本文关键字:位图 c++ 数组      更新时间:2023-10-16

我在再次将数组写入位图时遇到问题,所以基本上我从.bmp文件中读取数据,在那里我得到像素的数据,我想使 B 和 R = 0,所以这是我的代码:

int row_padded = ( Picture.biWidth*3 + 3) & (~3);
unsigned char* data = new unsigned char [row_padded];
unsigned char tmp;
 for(int i = 0; i < Picture.biHeight; i++)
{
    fread(data, sizeof(unsigned char), row_padded, plik);
    for(int j = 0; j < Picture.biWidth*3; j += 3)
    {
        data[j] = 0;
        data[j+2] = 0;
    }
}

现在,当我的 B 和 R = 0 时,我想再次将其保存到同一个文件中,所以我使用:

for(int j = 0; j< Picture.biHeight; j++)
{
    fwrite(data,1,Picture.biWidth, f);
}

但没有任何效果。

fwrite(data,1,Picture.biWidth, f)必须fwrite(data,1,row_padded, f)否? 否则只写入前三分之一的字节

注意:根据定义sizeof(unsigned char)是1