C++读取与保存bmp图片文件编程实现

转自http://blog.sina.com.cn/s/blog_71fa0df50100v2gv.html

 

要用C++读取bmp图片文件,首先要弄清楚bmp图片文件的结构。可以参考这篇文章:http://blog.csdn.net/xiajun07061225/article/details/5813726

我采用的编译环境是VS2008.但是先前本程序有一点问题是,保存bmp的功能没有实现,始终出现绘图失败的错误。现在终于调试好了。

上代码:分两个文件:ReadBmp.h和ReadBmp.cpp

ReadBmp.h:

 

  1. typedef unsigned char BYTE;  
  2. typedef unsigned short WORD;  
  3. typedef unsigned int DWORD;  
  4. typedef long LONG;  
  5.   
  6. //位图文件头定义;   
  7. //其中不包含文件类型信息(由于结构体的内存结构决定,   
  8. //要是加了的话将不能正确读取文件信息)   
  9. typedef struct  tagBITMAPFILEHEADER{  
  10.     //WORD bfType;//文件类型,必须是0x424D,即字符“BM”   
  11.     DWORD bfSize;//文件大小   
  12.     WORD bfReserved1;//保留字   
  13.     WORD bfReserved2;//保留字   
  14.     DWORD bfOffBits;//从文件头到实际位图数据的偏移字节数   
  15. }BITMAPFILEHEADER;  
  16.   
  17. typedef struct tagBITMAPINFOHEADER{  
  18.     DWORD biSize;//信息头大小   
  19.     LONG biWidth;//图像宽度   
  20.     LONG biHeight;//图像高度   
  21.     WORD biPlanes;//位平面数,必须为1   
  22.     WORD biBitCount;//每像素位数   
  23.     DWORD  biCompression; //压缩类型   
  24.     DWORD  biSizeImage; //压缩图像大小字节数   
  25.     LONG  biXPelsPerMeter; //水平分辨率   
  26.     LONG  biYPelsPerMeter; //垂直分辨率   
  27.     DWORD  biClrUsed; //位图实际用到的色彩数   
  28.     DWORD  biClrImportant; //本位图中重要的色彩数   
  29. }BITMAPINFOHEADER; //位图信息头定义   
  30.   
  31. typedef struct tagRGBQUAD{  
  32.     BYTE rgbBlue; //该颜色的蓝色分量   
  33.     BYTE rgbGreen; //该颜色的绿色分量   
  34.     BYTE rgbRed; //该颜色的红色分量   
  35.     BYTE rgbReserved; //保留值   
  36. }RGBQUAD;//调色板定义   
  37.   
  38. //像素信息   
  39. typedef struct tagIMAGEDATA  
  40.  
  41.     BYTE red;  
  42.     BYTE green;  
  43.     BYTE blue;  
  44. }IMAGEDATA;  

ReadBmp.cpp:
  1. #include <stdio.h>   
  2. #include "ReadBmp.h"   
  3. #include "stdlib.h"   
  4. #include <iostream>   
  5. using namespace std;  
  6.   
  7. //变量定义   
  8. BITMAPFILEHEADER strHead;  
  9. RGBQUAD strPla[256];//256色调色板   
  10. BITMAPINFOHEADER strInfo;  
  11. IMAGEDATA imagedata[256][256];//存储像素信息   
  12.   
  13. //显示位图文件头信息   
  14. void showBmpHead(BITMAPFILEHEADER pBmpHead){  
  15.     cout<<"位图文件头:"<<endl;  
  16.     //cout<<"bfType value is "<<hex<<pBmpHead.bfType<<endl;   
  17.     cout<<"文件大小:"<<pBmpHead.bfSize<<endl;  
  18.     //printf("文件大小:%d\n",pBmpHead.bfSize);   
  19.     cout<<"保留字_1:"<<pBmpHead.bfReserved1<<endl;  
  20.     cout<<"保留字_2:"<<pBmpHead.bfReserved2<<endl;  
  21.     cout<<"实际位图数据的偏移字节数:"<<pBmpHead.bfOffBits<<endl<<endl;  
  22.  
  23.   
  24. void showBmpInforHead(tagBITMAPINFOHEADER pBmpInforHead){  
  25.     cout<<"位图信息头:"<<endl;  
  26.     cout<<"结构体的长度:"<<pBmpInforHead.biSize<<endl;  
  27.     cout<<"位图宽:"<<pBmpInforHead.biWidth<<endl;  
  28.     cout<<"位图高:"<<pBmpInforHead.biHeight<<endl;  
  29.     cout<<"biPlanes平面数:"<<pBmpInforHead.biPlanes<<endl;  
  30.     cout<<"biBitCount采用颜色位数:"<<pBmpInforHead.biBitCount<<endl;  
  31.     cout<<"压缩方式:"<<pBmpInforHead.biCompression<<endl;  
  32.     cout<<"biSizeImage实际位图数据占用的字节数:"<<pBmpInforHead.biSizeImage<<endl;  
  33.     cout<<"X方向分辨率:"<<pBmpInforHead.biXPelsPerMeter<<endl;  
  34.     cout<<"Y方向分辨率:"<<pBmpInforHead.biYPelsPerMeter<<endl;  
  35.     cout<<"使用的颜色数:"<<pBmpInforHead.biClrUsed<<endl;  
  36.     cout<<"重要颜色数:"<<pBmpInforHead.biClrImportant<<endl;  
  37.  
  38.   
  39. tagRGBQUAD* ReadFile(char *strFile){  
  40.     FILE *fpi,*fpw;  
  41.     fpi=fopen(strFile,"rb");  
  42.     if(fpi!=NULL){  
  43.         //先读取文件类型   
  44.         WORD bfType;  
  45.         fread(&bfType,1,sizeof(WORD),fpi);  
  46.         if(0x4d42!=bfType){  
  47.             cout<<"the file is not bmp file!"<<endl;  
  48.             return NULL;  
  49.          
  50.         //读取bmp文件的文件头和信息头   
  51.         fread(&strHead,1,sizeof(tagBITMAPFILEHEADER),fpi);  
  52.         showBmpHead(strHead);//显示文件头   
  53.         fread(&strInfo,1,sizeof(tagBITMAPINFOHEADER),fpi);  
  54.         showBmpInforHead(strInfo);//显示文件信息头   
  55.       
  56.         //读取调色板   
  57.         for(int nCounti=0;nCounti<strInfo.biClrUsed;nCounti++){  
  58.             //存储的时候,一般去掉保留字rgbReserved   
  59.             fread((char *)&strPla[nCounti].rgbBlue,1,sizeof(BYTE),fpi);  
  60.             fread((char *)&strPla[nCounti].rgbGreen,1,sizeof(BYTE),fpi);  
  61.             fread((char *)&strPla[nCounti].rgbRed,1,sizeof(BYTE),fpi);  
  62.             cout<<"strPla[nCounti].rgbBlue"<<strPla[nCounti].rgbBlue<<endl;  
  63.             cout<<"strPla[nCounti].rgbGreen"<<strPla[nCounti].rgbGreen<<endl;  
  64.             cout<<"strPla[nCounti].rgbRed"<<strPla[nCounti].rgbRed<<endl;  
  65.          
  66.   
  67.         //读出图片的像素数据   
  68.         memset(imagedata,0,sizeof(IMAGEDATA) 256 256);  
  69.         //fseek(fpi,54,SEEK_SET);   
  70.         fread(imagedata,sizeof(struct tagIMAGEDATA) strInfo.biWidth,strInfo.biHeight,fpi);  
  71.         //for(int 0;i strInfo.biWidth;++i)   
  72.         for(int 0;i 1;++i)//只输出第一行数据   
  73.          
  74.             for(int 0;j strInfo.biHeight; ++j){  
  75.                 printf("%d  ", imagedata[i][j].green);  
  76.                 // cout<<imagedata[0][j].green+ ";   
  77.                 if((i strInfo.biHeight+j+1) == 0)  
  78.                     cout<<endl;  
  79.              
  80.          
  81.           
  82.         fclose(fpi);  
  83.      
  84.     else{  
  85.         cout<<"file open error!"<<endl;  
  86.         return NULL;  
  87.      
  88.       
  89.     //保存bmp图片   
  90.     if((fpw=fopen("b.bmp","wb"))==NULL){  
  91.         cout<<"create the bmp file error!"<<endl;  
  92.         return NULL;  
  93.      
  94.     WORD bfType=0x4d42;  
  95.     fwrite(&bfType,1,sizeof(WORD),fpw);  
  96.     //fpw +=2;   
  97.     fwrite(&strHead,1,sizeof(tagBITMAPFILEHEADER),fpw);  
  98.     fwrite(&strInfo,1,sizeof(tagBITMAPINFOHEADER),fpw);  
  99.     //保存调色板数据   
  100.     for(int nCounti=0;nCounti<strInfo.biClrUsed;nCounti++){  
  101.         fwrite(&strPla[nCounti].rgbBlue,1,sizeof(BYTE),fpw);  
  102.         fwrite(&strPla[nCounti].rgbGreen,1,sizeof(BYTE),fpw);  
  103.         fwrite(&strPla[nCounti].rgbRed,1,sizeof(BYTE),fpw);  
  104.      
  105.     //保存像素数据   
  106.     for(int =0;i strInfo.biWidth;++i){  
  107.         for(int 0;j strInfo.biHeight;++j){  
  108.             fwrite( &imagedata[i][j].blue,1,sizeof(BYTE),fpw);  
  109.             fwrite( &imagedata[i][j].green,1,sizeof(BYTE),fpw);  
  110.             fwrite( &imagedata[i][j].red,1,sizeof(BYTE),fpw);  
  111.          
  112.      
  113.   
  114.     fclose(fpw);  
  115.  
  116.   
  117. int main(){  
  118.     char strFile[30];//bmp文件名   
  119.     cout<<"请输入所要读取的文件名:"<<endl;  
  120.     cin>>strFile;  
  121.     ReadFile(strFile);  
  122.     system("pause");  
  123.  

 

读取的图片lena.bmp:


运行结果:即输出的图片信息:

保存得到的图片b.bmp:

b.bmp:


  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值