-1

下面提到的代码;在 32 位机器上运行良好,但在 64 位机器上无法运行..

任何想法/建议 - 以及如何解决这个问题???伙计们,你们怎么看 - 在 64 位机器中保存图像 8 位 BMP

void BMPFile::SaveBMP(char* fileName,BYTE * buf,UINT width,UINT height)
{
    short res1=0;
    short res2=0;
    long pixoff=54;
    long compression=0;
    long cmpsize=0;
    long colors=0;
    long impcol=0;
    char m1='B';
    char m2='M';

    DWORD widthDW = WIDTHBYTES(width *24);

    long bmfsize=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
                            widthDW * height;   
    long byteswritten=0;

    BITMAPINFOHEADER header;
    header.biSize=40;                       // header size
    header.biWidth=width;
    header.biHeight=height;
    header.biPlanes=1;
    header.biBitCount=24;                   // RGB encoded, 24 bit
    header.biCompression=BI_RGB;            // no compression
    header.biSizeImage=0;
    header.biXPelsPerMeter=0;
    header.biYPelsPerMeter=0;
    header.biClrUsed=0;
    header.biClrImportant=0;

    FILE *fp;   
    fp=fopen(fileName,"wb");
    if (fp==NULL)
    {
        return;
    }

    // should probably check for write errors here...

    fwrite((BYTE  *)&(m1),1,1,fp); byteswritten+=1;
    fwrite((BYTE  *)&(m2),1,1,fp); byteswritten+=1;
    fwrite((long  *)&(bmfsize),4,1,fp); byteswritten+=4;
    fwrite((int  *)&(res1),2,1,fp); byteswritten+=2;
    fwrite((int  *)&(res2),2,1,fp); byteswritten+=2;
    fwrite((long  *)&(pixoff),4,1,fp); byteswritten+=4;

    fwrite((BITMAPINFOHEADER *)&header,sizeof(BITMAPINFOHEADER),1,fp);
    byteswritten+=sizeof(BITMAPINFOHEADER);

    long row=0;
    long rowidx;
    long row_size;
    row_size=header.biWidth*3;
    long rc;
    for (row=0;row<header.biHeight;row++) 
    {
        rowidx=(long unsigned)row*row_size;                           

        // write a row
        rc=fwrite((void  *)(buf+rowidx),row_size,1,fp);
        if (rc!=1) 
        {
            break;
        }
        byteswritten+=row_size; 

        // pad to DWORD
        for (DWORD count=row_size;count<widthDW;count++) {
            char dummy=0;
            fwrite(&dummy,1,1,fp);
            byteswritten++;                           
        }
    }             
    fclose(fp);
}
4

1 回答 1

1

编辑:我再次查看了 Windows 标题,我错过了#pragma 包。所以对齐不是问题。

通常,如果您需要将固定大小的值写入磁盘,请使用显式大小的数据类型,因为 int/long 等的大小可能因编译器和体系结构而异。在 ANSI 平台上,您可以包含“stdint.h”,在视觉 c 上,您可以 typedef 这些:

typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef signed __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
于 2012-05-07T04:38:40.663 回答