本文介绍了无法保存使用C图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想克隆一个BMP图像到另一个BMP图像,但最终的图像不会打开。

I tried to clone a bmp image into another bmp image but the final image would not open.

#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();
}

这是我花了作为输入的图像是114X81与27918字节大小。
最终图像具有相同的大小,但大小为27756字节。

The image that I took as an input was 114X81 with size of 27918 bytes.The final image had same size but the size was 27756 bytes.

这可能是什么错误??

What could be the error ??

推荐答案

BMP商店中的每一行的倍数。在你的情况,这意味着每行需要116字节(2字节填充)。这给了116x78x3 + 54 = 27198
 所以,你就错了。

BMP stores each row in a multiple of 4 bytes. In your case, that means that each rows takes 116 bytes, (2 bytes padding). That gives 116x78x3+54=27198 So you are doing it wrong.

顺便说一句标题长度不总是为54个字节。

BTW the header length not always is 54 bytes.

这篇关于无法保存使用C图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:52