C++ 无法创建位图图像;fileheader/infoheader错误

C++ 无法创建位图图像;fileheader/infoheader错误,c++,image,bitmap,C++,Image,Bitmap,我正在尝试从一个高性能照相机(Allied Vision Technologies Prosilica GT2750)拍摄的图像创建一个24位图图像。到目前为止,我已经做到了:(注意,VmbUInt32被定义为“typedef unsigned u int32 VmbUInt32_t;”,而VmbUchar_t只是一个无符号字符) 每次运行时,imageSize、imageWidth和imageHeight都是相同的(分别为6054400、2752200),获取它们的函数来自相机的API。它们工

我正在尝试从一个高性能照相机(Allied Vision Technologies Prosilica GT2750)拍摄的图像创建一个24位图图像。到目前为止,我已经做到了:(注意,VmbUInt32被定义为“typedef unsigned u int32 VmbUInt32_t;”,而VmbUchar_t只是一个无符号字符)

每次运行时,imageSize、imageWidth和imageHeight都是相同的(分别为6054400、2752200),获取它们的函数来自相机的API。它们工作正常,只返回整数(至少从我在汽车中看到的情况)

其他资料: 我正在使用VisualStudio2010


TL;DR:“我的文件头”和/或“信息头”有什么问题?

您的文件头中可能缺少两个保留字段

文件头: 4字节BMP文件的大小(以字节为单位) 保留2字节;实际值取决于创建图像的应用程序 保留2字节;实际值取决于创建图像的应用程序
4字节可以找到位图图像数据(像素阵列)的字节的偏移量,即起始地址

打开文件时的表达式
std::ios::binary | | std::ios::out
可能不是您想要的。即使是这样,我已经编写了2或3个使用此代码编写小型bmp的测试程序,它们工作得很好。你能推荐一个替代方案吗?那么你可能在打开文件时使用了位or运算符
|
而不是逻辑or运算符
|
?哦,是的,那只是一个复制/粘贴错误,哈哈,我很糟糕,但是它在包含位运算符的情况下不起作用。我会尝试用sizeof()替换一些神奇的数字表达式或符号名称,但这是一种风格上的挑剔。但是,您使用的标题与wikipedia上的标题不匹配。此外,您可能需要检查正在使用的结构是否已打包。
        VmbUint32_t imageSize = 0;
        pFrame->GetImageSize(imageSize); //imageSize = 6054400

        VmbUint32_t imageWidth = 0;
        pFrame->GetWidth(imageWidth);    //imageWidth = 2752
        VmbUint32_t imageHeight = 0;
        pFrame->GetHeight(imageHeight);  //imageHeight = 2200

        //////////////////////////////////////////////////////////////////////////
        //////////  Set Bitmap Settings   ////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////

        //file header
        BITMAPFILEHEADER* bf = new BITMAPFILEHEADER;
        bf->bfType              = 0x4d42;
        bf->bfSize              = imageSize + 40 + 14; //image size + infoheader size + fileheader size 
        bf->bfOffBits           = 54;

        //info header
        BITMAPINFOHEADER* bi = new BITMAPINFOHEADER;
        bi->biSize              = 40;
        bi->biWidth             = imageWidth;
        bi->biHeight            = imageHeight;
        bi->biPlanes            = 1;
        bi->biBitCount          = 24;
        bi->biCompression       = 0;
        bi->biSizeImage         = imageSize;
        bi->biXPelsPerMeter     = 2835;
        bi->biYPelsPerMeter     = 2835;
        bi->biClrUsed           = 0;
        bi->biClrImportant      = 0;


        //image data
        VmbUchar_t* imageData = new VmbUchar_t[imageSize];
        pFrame->GetImage(imageData);


        //////////////////////////////////////////////////////////////////////////
        //////////  Output File to .bmp   ////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////


        std::ofstream outFile;

        outFile.open("test.bmp", std::ios::binary|std::ios::out);

        outFile.write(reinterpret_cast<char *>(bf), sizeof(BITMAPFILEHEADER));
        outFile.write(reinterpret_cast<char *>(bi), sizeof(BITMAPINFOHEADER));
        outFile.write(reinterpret_cast<char *>(imageData), imageSize);

        outFile.close();
42 4D 36 62 5C 00 CD CD CD CD 36 00 00 00 28 00 00 00 C0 
0D 0A 00 00 98 08 00 00 01 00 18 00 00 00 00 00 00 62 5C 
00 13 0B 00 00 13 0B 00 00 00 00 00 00 00 00 00 00