//INCLUDE ----
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
//MAIN ----
void main(void)
{
FILE *fp_bmp;
int gdriver,gmode;
int maxx,maxy;
int leng[4];
int bit;
struct hua
{
int head;
int w;
int h;
} image;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"");
maxx=getmaxx();
maxy=getmaxy();
if((fp_bmp=fopen("e:\\bc31\\fangkuai.bmp","rb"))==NULL)
{
setcolor(YELLOW);
settextstyle(2,0,8);
//setusercharsize(2,1,4,1);
outtextxy(maxx/2-80,maxy/2,"Can't open the file!");
getch();
closegraph();
exit(1);
}
fseek(fp_bmp,0x0A,SEEK_SET);
image.head=fgetc(fp_bmp);
image.w=0;
image.h=0;
//printf("%04X",image.head);
fseek(fp_bmp,0x12,SEEK_SET);
for(int i=0;i<4;i++)
{
leng[i]=fgetc(fp_bmp);
}
image.w=leng[0]+leng[1]*255+leng[2]*255*255+leng[3]*255*255*255l;
fseek(fp_bmp,0x16,SEEK_SET);
for(i=0;i<4;i++)
{
leng[i]=fgetc(fp_bmp);
}
image.h=leng[0]+leng[1]*255+leng[2]*255*255+leng[3]*255*255*255l;
//printf("%d %d\t",image.w,image.h);
int ln=(image.w+7)/8;
int line_word=(image.w+31)/32*4;
getch();
int biao=0x01;
fseek(fp_bmp,image.head,SEEK_SET);
for(i=image.h;i>0;i--)
{
for(int j=0;j<line_word;j++)
{
if(j>ln)
{bit=fgetc(fp_bmp);}
else
{
bit=fgetc(fp_bmp);
for(int k=0;k<8;k++)
{
int bit1=bit&(biao<<k);
if(bit1!=0)
putpixel(j*8+8-k,10+i,YELLOW);
else
putpixel(j*8+8-k,10+i,RED);
}
}
}
}
getch();
fclose(fp_bmp);
}
这我写的,不过有很多乱的地方,各位见笑了!!!
显示的图象根本就是乱码
高手解决一下
/*我也是从别人那里抄来的,希望对你有点用*/
/*************2色,16色非压缩BMP位图显示程序****************/
/*******功能: 可以在指定位置输出图片,
可用作该格式的图片浏览器********/
#include <stdio.h>
#include <graphics.h>
#include <alloc.h>
#include <stdlib.h>
#include <math.h>
typedef struct tagBITMAPFILEHEADER
{
unsigned int bfType;
unsigned long bfSize;
unsigned int bfReserved1;
unsigned int bfReserved2;
unsigned long bfOffBits;
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned int biPanes;
unsigned int biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
}RGBQUAD;
typedef struct tagBITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[];
}BITMAPINFO;
int xmax;
int ymax;
main()
{
char *filename = "";
printf("Input a 16BMP: ");
scanf("%s",filename);
InitGraph();
load_bmp(100,100,filename); /*在TC目录下的某个位图文件名(*.bmp)或者该文件的完整路径*/
getch();
}
/*-----------图形模式初始化------------*/
InitGraph()
{
int gd,gm;
gd=DETECT;
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
cleardevice();
xmax=getmaxx();
ymax=getmaxy();
}
void Exit(char *ErrorCode)
{
printf("%s",ErrorCode);
printf("\n Pree any key to come back ^_^");
getch();
closegraph();
exit(0);
}
long WidthBytes(long Width,int BitCount)
{
long WBytes;
WBytes=(Width*BitCount+31)/8;
WBytes=WBytes/4*4;
return WBytes;
}
unsigned char SetPalette(int Colors,unsigned char data)
{
switch(Colors)
{
case 16:
switch(data)
{
case 1:
return 4;
case 4:
return 1;
case 3:
return 6;
case 6:
return 3;
case 9:
return 12;
case 12:
return 9;
case 11:
return 14;
case 14:
return 11;
default:
return data;
}
case 2:
if(data==1)
return 15;
else
return 0;
}
}
/*********装载BMP位图*********/
load_bmp(int x_strp, int y_strp, char *filename)
{
long i,j;
long WBytes;
int Colors;
long Height,Width;
FILE *fp;
void *Temp=NULL;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
unsigned char SrcData,data;
InitGraph();
if((fp=fopen(filename,"rb"))==NULL)
{
Exit("Can Not Open The File.\n");
}
fread(&bfh,sizeof(BITMAPFILEHEADER),1,fp);
if(bfh.bfType!='M'*256+'B')
{
Exit("This Is Not A Bmp File.\n");
}
fread(&bih,sizeof(BITMAPINFOHEADER),1,fp);
Height=bih.biHeight;
Width=bih.biWidth;
WBytes=WidthBytes(Width,bih.biBitCount);
Colors=1<<bih.biBitCount;
if(!(Colors==16||Colors==2))
{
Exit("This Programme Only For 16 Colors Bitmap.\n");
}
fread(Temp,sizeof(RGBQUAD),Colors,fp);
for(i=Height-1;i>=0;i--)
{
fseek(fp,54+Colors*sizeof(RGBQUAD)+i*WBytes,SEEK_SET);
for(j=0;j<Width;j++)
{
switch(Colors)
{
case 16:
if(j%2==0)
{
fread(&SrcData,1,1,fp);
data=SetPalette(Colors,SrcData/16);
putpixel(j+x_strp,Height-1-i+y_strp,data);
}
else
{
data=SetPalette(Colors,SrcData%16);
putpixel(j+x_strp,Height-1-i+y_strp,data);
}
break;
case 2:
if(j%8==0)
{
fread(&SrcData,1,1,fp);
data=SetPalette(Colors,(SrcData>>7)%2);
putpixel(j+x_strp,Height-1-i+y_strp,data);
}
else
{
data=SetPalette(Colors,(SrcData>>(7-j%8))%2);
putpixel(j+x_strp,Height-1-i+y_strp,data);
}
}
}
}
}