c语言下读bmp位图

本文介绍如何使用C语言读取2位黑白BMP位图并将其显示,针对50*50像素的位图进行操作,虽然未涉及迷宫搜索功能,但提供了源代码实现。
这个程序是原来帮别人写个位图迷宫的一部分

主要是对2位的黑白图片读取到数组中,再显示出来,最后再搜索迷宫(未做迷宫搜索);


bmp图片格式就不多介绍了,百度下就有了。

图片为50*50的位图


效果图如下:

-----------------------------------------------------------------------------




----------------------------------------------------------------------------------

源代码:



#include "stdio.h" 
 #include "Windows.h" 
 #include "stdlib.h" 
 #include "math.h" 
int Ma[200][200];  
typedefstruct bmpMaze  
 {  
char *bmp;//图像指针 
char *bmpName;//图像路径 
int width;//宽 
int height;//图像的高 
int biBitCount;//图像类型,每像素位数 
int lineByte;  
int datasize;  
     BITMAPINFOHEADER head;  
     BITMAPFILEHEADER file;  
     RGBQUAD color[2];  
 }bmpMaze;  
int readBmp(bmpMaze *maze)  
 {  
int i=0;  
FILE *fp=fopen(maze->bmpName,"rb");  
if(fp==NULL)  
     {  
         printf("File is not found !!\n");  
return 0;  
     }  
     fread(&(maze->file), sizeof(BITMAPFILEHEADER), 1,fp);  
     fread(&(maze->head), sizeof(BITMAPINFOHEADER), 1,fp);  
     fread(&(maze->color), 8, 1,fp);  
     maze->width = maze->head.biWidth;  
     maze->height = maze->head.biHeight;  
     maze->biBitCount = maze->head.biBitCount;  
     maze->lineByte=(maze->width+8-maze->width%8)/8;  
if(maze->width%8==0)  
         maze->lineByte--;  
     maze->datasize=maze->file.bfSize-maze->file.bfOffBits-2;  
     maze->bmp=(char *)malloc(maze->datasize*sizeof(char));  
     fread(maze->bmp,maze->datasize,1,fp);  
     fclose(fp);  
return 1;  
 }  
void MazeToArray(bmpMaze maze)  
 {  
//将图片像素信息读取到Ma数组中 
int status,i=0,x,y,n=8,j=0,line;  
     line=maze.datasize/maze.height;  
for(x=maze.height-1;x>=0;x--)  
     {  
for(y=0;y<maze.width;y++)  
         {  
//获取这个点是黑的还是白的 
             status=maze.bmp[i]&(int)pow(2,n-1);  
if(!status)  
                 Ma[x][y]=0;  
else
                 Ma[x][y]=1;  
             n--;  
if(n==0)  
             {  
                 n=8;  
                 i++;  
             }  
         }  
         i=i+(line-maze.lineByte+1);  
         n=8;  
     }  
 }  
void printMaze(bmpMaze maze)  
 {  
int x=0,y=0;  
for(x=0;x<maze.height;x++)  
     {  
for(y=0;y<maze.width;y++)  
             printf("%d",Ma[x][y]);  
         printf("\n");  
     }  
 }  
void main()  
 {  
     bmpMaze maze;  
     maze.bmpName="test.bmp";  
     readBmp(&maze);  
     MazeToArray(maze);  
     printMaze(maze);  
 } 



评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值