.BMP 文件无法打开
.BMP file cannot be opened
我正在尝试创建一个 bmp 文件。文件已创建,我可以打开它,但它说文件已损坏,无法正常打开。这是我保存文件的方式:
void createBMP(char* pixelData, long xRes, long yRes){
BITMAPFILEHEADER fheader;
BITMAPINFOHEADER iheader;
COLORREF_RGB rgb;
//file header werte setzen
fheader.bfType = 0x4D42;
fheader.bfSize = sizeof(BITMAPFILEHEADER);
fheader.bfReserved1 =0;
fheader.bfReserved2=0;
fheader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
//info header werte setzen
iheader.biSize = sizeof(BITMAPINFOHEADER);
iheader.biWidth = xRes;
iheader.biHeight = yRes;
iheader.biPlanes = 1;
//farbtiefe
iheader.biBitCount = 24;
iheader.biCompression = 0;
iheader.biSizeImage = 0;
iheader.biXPelsPerMeter = 0;
iheader.biYPelsPerMeter = 0;
//anzahl der eintraege der farbtabelle
iheader.biClrUsed = 0;
iheader.biClrImportant = 0;
//rgbfarbwerte setzen
rgb.cRed = 0;
rgb.cGreen = 0;
rgb.cBlue = 0;
printf("\nwriting Image-File");
FILE *f;
f = fopen("testASP.bmp", "wb");
if(f == NULL){
freopen("testASP.bmp", "wb", f);
// printf("Fehler beim Laden des Files");
// return;
}
printf("\nPath: %S\n");
fwrite(&fheader, sizeof(BITMAPFILEHEADER),1,f);
fwrite(&iheader, sizeof(BITMAPINFOHEADER),1,f);
int i,j;
for(i=0;i<xRes; i++){
for(j=0;j<yRes;j++){
fwrite(&rgb, sizeof(COLORREF_RGB),1,f);
}
}
fclose(f);
printf("Bild wurde gespeichert\n");
我不确定我做错了什么,仔细检查了这些值,但似乎找不到我的错误。帮助将不胜感激。谢谢
根据请求添加结构:
typedef unsigned int UINT;
typedef unsigned long DWORD;
typedef long int LONG;
typedef unsigned short WORD;
typedef unsigned char BYTE;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct COLORREF_RGB
{
BYTE cRed;
BYTE cGreen;
BYTE cBlue;
}COLORREF_RGB;
以下过程创建一个黑色 BMP 文件:
// WIDTHBYTES takes # of bits in a scanline and rounds up to nearest word.
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
int makeBMPfile(int xRes, int yRes)
{
BITMAPFILEHEADER fheader = {0};
BITMAPINFOHEADER iheader = {0};
COLORREF_RGB rgb = {0,0,0};
FILE *fp;
int i,j, padbytes;
iheader.biSize = sizeof (BITMAPINFOHEADER);
iheader.biWidth = xRes;
iheader.biHeight = yRes;
iheader.biPlanes = 1;
iheader.biBitCount = 24;
iheader.biSizeImage= WIDTHBYTES(xRes*24) * yRes;
padbytes= WIDTHBYTES(xRes*24) - (xRes*24/8);
fheader.bfType = 0x4D42;
fheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 0/*no palette for 24 bits color*/;
fheader.bfSize = fheader.bfOffBits + iheader.biSizeImage;
if ((fp = fopen("testASP.bmp", "wb"))==NULL) return(0);
fwrite(&fheader, sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&iheader, sizeof(BITMAPINFOHEADER),1,fp);
for(i=0;i<yRes; i++){
for(j=0;j<xRes;j++){
fwrite(&rgb, sizeof(COLORREF_RGB),1,fp);
}
if (padbytes) fwrite(&rgb, padbytes,1,fp);
}
fclose(fp);
return(1);
}
我正在尝试创建一个 bmp 文件。文件已创建,我可以打开它,但它说文件已损坏,无法正常打开。这是我保存文件的方式:
void createBMP(char* pixelData, long xRes, long yRes){
BITMAPFILEHEADER fheader;
BITMAPINFOHEADER iheader;
COLORREF_RGB rgb;
//file header werte setzen
fheader.bfType = 0x4D42;
fheader.bfSize = sizeof(BITMAPFILEHEADER);
fheader.bfReserved1 =0;
fheader.bfReserved2=0;
fheader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
//info header werte setzen
iheader.biSize = sizeof(BITMAPINFOHEADER);
iheader.biWidth = xRes;
iheader.biHeight = yRes;
iheader.biPlanes = 1;
//farbtiefe
iheader.biBitCount = 24;
iheader.biCompression = 0;
iheader.biSizeImage = 0;
iheader.biXPelsPerMeter = 0;
iheader.biYPelsPerMeter = 0;
//anzahl der eintraege der farbtabelle
iheader.biClrUsed = 0;
iheader.biClrImportant = 0;
//rgbfarbwerte setzen
rgb.cRed = 0;
rgb.cGreen = 0;
rgb.cBlue = 0;
printf("\nwriting Image-File");
FILE *f;
f = fopen("testASP.bmp", "wb");
if(f == NULL){
freopen("testASP.bmp", "wb", f);
// printf("Fehler beim Laden des Files");
// return;
}
printf("\nPath: %S\n");
fwrite(&fheader, sizeof(BITMAPFILEHEADER),1,f);
fwrite(&iheader, sizeof(BITMAPINFOHEADER),1,f);
int i,j;
for(i=0;i<xRes; i++){
for(j=0;j<yRes;j++){
fwrite(&rgb, sizeof(COLORREF_RGB),1,f);
}
}
fclose(f);
printf("Bild wurde gespeichert\n");
我不确定我做错了什么,仔细检查了这些值,但似乎找不到我的错误。帮助将不胜感激。谢谢
根据请求添加结构:
typedef unsigned int UINT;
typedef unsigned long DWORD;
typedef long int LONG;
typedef unsigned short WORD;
typedef unsigned char BYTE;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct COLORREF_RGB
{
BYTE cRed;
BYTE cGreen;
BYTE cBlue;
}COLORREF_RGB;
以下过程创建一个黑色 BMP 文件:
// WIDTHBYTES takes # of bits in a scanline and rounds up to nearest word.
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
int makeBMPfile(int xRes, int yRes)
{
BITMAPFILEHEADER fheader = {0};
BITMAPINFOHEADER iheader = {0};
COLORREF_RGB rgb = {0,0,0};
FILE *fp;
int i,j, padbytes;
iheader.biSize = sizeof (BITMAPINFOHEADER);
iheader.biWidth = xRes;
iheader.biHeight = yRes;
iheader.biPlanes = 1;
iheader.biBitCount = 24;
iheader.biSizeImage= WIDTHBYTES(xRes*24) * yRes;
padbytes= WIDTHBYTES(xRes*24) - (xRes*24/8);
fheader.bfType = 0x4D42;
fheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 0/*no palette for 24 bits color*/;
fheader.bfSize = fheader.bfOffBits + iheader.biSizeImage;
if ((fp = fopen("testASP.bmp", "wb"))==NULL) return(0);
fwrite(&fheader, sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&iheader, sizeof(BITMAPINFOHEADER),1,fp);
for(i=0;i<yRes; i++){
for(j=0;j<xRes;j++){
fwrite(&rgb, sizeof(COLORREF_RGB),1,fp);
}
if (padbytes) fwrite(&rgb, padbytes,1,fp);
}
fclose(fp);
return(1);
}