shn_baby 2021-05-11 16:26 采纳率: 57.1%
浏览 123
已采纳

用C语言写BMP写入后,文件无法打开并且文件大小只有4096字节

初学图像处理,用VS2013写bmp文件的读写时,输出的bmp文件无法打开,并且大小只有4096kb。以下是写图片的部分代码:

  //自定义的bmp类

typedef struct
{
    int mWidth;
    int mHeight;
    int mCount;    
    unsigned char* data;
}BMP;

//文件读入 

void bmpRead(BMP &bitmap, const char *bmpPath)
{
    FILE *file = fopen(bmpPath, "rb");
    if (NULL == file)
        return;
    BITMAPFILEHEADER bmphd;
    BITMAPINFOHEADER bmpin;
    fread(&bmphd, sizeof(BITMAPFILEHEADER), 1, file);
    fread(&bmpin, sizeof(BITMAPINFOHEADER), 1, file);
    bitmap.mWidth = bmpin.biWidth;
    bitmap.mHeight = bmpin.biHeight;
    bitmap.mCount = bmpin.biBitCount;
    bitmap.data = new unsigned char[bitmap.mWidth * bitmap.mHeight];
    fread(bitmap.data, 1, bitmap.mWidth * bitmap.mHeight, file);
    fclose(file);
}

//文件写入 

void bmpWrite(BMP &bitmap,const char *bmpPath)
{
    FILE *file;
    file = fopen(bmpPath, "wb");
    if (NULL == file)
        return;
    int iByteLength = 0;
    int colorsize = 0;    
    if (8 == bitmap.mCount)
    {
        iByteLength = (bitmap.mWidth + 3 )/ 4 * 4;
        colorsize = 1024;
    }
    else if (24 == bitmap.mCount)
    {
        iByteLength = (bitmap.mWidth * 3 + 3) / 4 * 4;
    }
    else
    {
        iByteLength =( bitmap.mWidth *bitmap.mCount/3 + 3) / 4 * 4;
    }
    BITMAPFILEHEADER bmphd;    
    bmphd.bfType = 0x4D42;
    bmphd.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + colorsize;
    bmphd.bfSize = bmphd.bfOffBits + iByteLength*bitmap.mHeight;
    bmphd.bfReserved1 = 0;
    bmphd.bfReserved2 = 0;

    BITMAPINFOHEADER bmpin;
    bmpin.biBitCount = bitmap.mCount;
    bmpin.biClrImportant = 0;
    bmpin.biClrUsed = 0;
    bmpin.biCompression = 0;
    bmpin.biHeight = bitmap.mHeight;
    bmpin.biPlanes = 1;
    bmpin.biSize = sizeof(BITMAPINFOHEADER);
    bmpin.biSizeImage =0;
    bmpin.biWidth = bitmap.mWidth;
    bmpin.biXPelsPerMeter = 300;
    bmpin.biYPelsPerMeter = 300;

    fwrite(&bmphd, sizeof(BITMAPFILEHEADER), 1, file);

    fwrite(&bmpin, sizeof(BITMAPINFOHEADER), 1, file);

    if (bitmap.mCount == 8)
    {
        unsigned char uchPalette[256 * 4];
        for (int j = 0; j < 256; j++)
        {
            uchPalette[j * 4] = (unsigned char)j;
            uchPalette[j * 4 + 1] = (unsigned char)j;
            uchPalette[j * 4 + 2] = (unsigned char)j;
            uchPalette[j * 4 + 3] = 0;
        }
        fwrite(uchPalette, sizeof(RGBQUAD), 256, file);
    }
    int i = iByteLength*bitmap.mHeight;
    fwrite(bitmap.data, sizeof(unsigned char), i, file);   
    fclose(file);    

}

int main()
{  
    char* fpath = "C:\\Users\\admin\\Desktop\\picinen\\20210311162754539.bmp";
    char* fdstpath = "C:\\Users\\admin\\Desktop\\2.bmp"; 
    BMP bitmap; 
    bmpRead(bitmap, fpath);  
    bmpWrite(bitmap, fdstpath);
    return 0;    
}

目前只考虑24位图。怀疑是fwrite函数有问题,将fwrite(bitmap.data, sizeof(unsigned char), i, file); 这一行代码的i改为具体的数值发现太大的数值(100000是可以的)就无法写入。在网上找不到方法解决,求各位大佬告知!

  • 写回答

4条回答 默认 最新

  • 关注

    转换为byte数组保存。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效