我試圖將一個bmp圖像克隆到另一個bmp圖像中,但最終圖像無法打開。無法使用c保存圖像文件?
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <process.h>
void readBMP(char* filename) {
int i;
FILE* f = fopen(filename, "rb");
FILE* f1= fopen("save.bmp", "wb");
if (!f) {
printf("Could not read file!\n");
exit(0);
}
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f);
int width = *(int*)&info[18];
int height = *(int*)&info[22];
printf("%d %d\n", width, height);
fwrite(info, sizeof(unsigned char), 54, f1);
int length = width * height;
unsigned int image[10000][3];
for(i = 0; i < length; i++) {
image[i][2] = getc(f);
image[i][1] = getc(f);
image[i][0] = getc(f);
putc(image[i][2], f1);
putc(image[i][1], f1);
putc(image[i][0], f1);
printf("pixel %d : [%d,%d,%d]\n", i+1, image[i][0], image[i][1], image[i][2]);
}
fclose(f);
fclose(f1);
}
void main() {
char* fileName = "bitgray.bmp";
readBMP(fileName);
getch();
}
我作爲輸入的圖像是大小爲27918字節的114X81。 最終圖像大小相同,但大小爲27756字節。
什麼可能是錯誤?
你怎麼知道你遇到了錯誤? fopen()失敗了嗎?程序崩潰了嗎? 'save.bmp'文件是否出現在您的目錄中,但無法讀取? – chrisaycock
長度不像寬*高。 – JasonD
您應該檢查文件f1的fopen結果。 – Joze