我遇到了同样的问题,但是通过阅读 BMP 文件格式描述,我编写了一个函数来读取 .BMP 文件并将其存储到数组中。也许这个功能可以帮助你:
unsigned int PIC::BinToNum(char *b,int bytes)
{
    unsigned int tmpx = 0;
    unsigned int pw = 1;
    for(int i=0;i<bytes;i++)
    {
        tmpx += ((unsigned char)b[i]* pw);
        pw = pw * 256;
    }
    return tmpx;
}
int PIC::Open(const char *path)
{
    int pad = 0;
    unsigned int sof = 0;
    unsigned int tx = 0;
    char tmp[4] = {0,0,0,0};
    fstream file;
    file.open(path,ios::in);
    if(file.fail())
    {
        width=height=ColorBits=size=0;
        return -1;
    }
    else
    {
        file.seekg(0,ios::beg);
        file.read(tmp,2);
        if(!(tmp[0] == 66 && tmp[1] == 77))
        {
            width=height=ColorBits=size=0;
            return 0;
        }
        else
        {
            file.seekg(2,ios::beg); // 0x2 size
            file.read(tmp,4);
            size = BinToNum(tmp,4);
            file.seekg(18,ios::beg); // 0x12 width
            file.read(tmp,4);
            width = BinToNum(tmp,4);
            file.seekg(22,ios::beg); // 0x16 height
            file.read(tmp,4);
            height = BinToNum(tmp,4);
            file.seekg(28,ios::beg); // 0x1C Bits per Pixel
            file.read(tmp,2);
            ColorBits = BinToNum(tmp,2);
            file.seekg(10,ios::beg); // 0x0A start offset
            file.read(tmp,4);
            sof=BinToNum(tmp,4);
            file.seekg(34,ios::beg); // 0x22 Padding
            file.read(tmp,4);
            pad = BinToNum(tmp,4);
            pad = (int)(pad / height); // Compute Spacing in each row
            pad = pad - (width*ColorBits/8);
            // Initialize Matrix//
            matrix = new(unsigned int[height*width]);
            for(int h=height-1;h>=0;h--)
            {
                for(int w=0;w<=width-1;w++)
                {
                    file.seekg(sof,ios::beg);
                    file.read(tmp,(int)(ColorBits/8));
                    tx = BinToNum(tmp,(int)(ColorBits/8));
                    matrix[(h*width)+w] = tx;
                    sof+=(int)(ColorBits/8);
                }
                sof +=pad;
            }
        }
    }
    file.close();
    return 1;
}
Note:This functions is member of a class that i named it "PIC"...