01/10/2018, 10:36
Hỏi về đọc ghi File Bitmap trong C
Em chào mọi người trong diễn đàn ạ.
Em đang thực hiện một bài tập về đọc ghi file nhị phân, cụ thể là file Bitmap.
Theo em tìm hiểu thì cấu trúc file Bitmap gồm 3 phần chính:
BitmapHeader 14byte
BitmapInfo 40byte
Bitmapdata
Em thử việc đọc vào một file ảnh bitmap. Và thử in ra định dạng file nhưng không ra chứ BM (bitmap).
Em muốn hỏi là có phải code em có vấn đề không ạ.
code:
#include <stdio.h>
#include <conio.h>
struct BitMapHeader{
char bfType[2];
unsigned char bfSize[4];
unsigned char bfReserved1[2];
unsigned char bfReserved2[2];
unsigned char bfOffBits[4];
};
struct BitMapInFo
{
unsigned char biSize[4];
unsigned char biWidth[4];
unsigned char biHeight[4];
unsigned char biPlanes[2];
unsigned char biBitCount[2];
unsigned char stuff1[16];
unsigned char biClrUsed[4];
unsigned char biClrImportant[4];
};
void readFromFile(FILE* fp){
BitMapHeader bmhd;
BitMapInFo bmif;
fread(&bmhd,sizeof(bmhd),1,fp);
fread(&bmif,sizeof(bmif),1,fp);
printf("
%s",bmhd.bfType);
printf("
%d",bmhd.bfSize);
printf("
%d",bmif.biSize);
printf("
%d",bmif.biWidth);
printf("
%d",bmif.biHeight);
}
int main(){
FILE* fp = NULL;
fp = fopen("E:\anh.bmp","w+b");
if(!fp){
printf("Open file fail ...");
return 0;
}
readFromFile(fp);
//fseek(fp,0,SEEK_SET);
fclose(fp);
getchar();
return 0;
}
Mong mọi người xem giúp em với ạ.
Chúc mọi người một ngày làm việc hiệu quả ạ.
Bài liên quan
cái này để xử lý ảnh phải không
Đúng rồi bạn. Nhưng t chỉ đang làm bài tập nhỏ thôi. Tạo bản copy được đảo ngược.