win api 函数 :getwindowpixel()别忘加分呢。
#include
int main()
{
int width,height,x,y;
unsigned short bitCount;
int offbits;
int bitPerLine;
unsigned char data;
FILE* bmpfp = fopen("E:\\风景\\风景1.bmp","rb");
fseek(bmpfp,18,SEEK_SET);
fread(&width,sizeof(int),1,bmpfp);
fread(&height,sizeof(int),1,bmpfp);
printf("width : %d , height : %d\n",width,height);
fseek(bmpfp,2,SEEK_CUR);
fread(&bitCount,sizeof(bitCount),1,bmpfp);
fseek(bmpfp,10,SEEK_SET);
fread(&offbits,sizeof(int),1,bmpfp);
if(bitCount==24){
bitPerLine = ( (width*3)%4==0 ) ? width*3 : ( (width*3)/4 )*4 + 4;
while(1){
printf("请输出坐标:");
scanf("%d%d",&x,&y);
if(x>width||y>height) return 0;
fseek(bmpfp, 18 + offbits + bitPerLine * y + 3*x , SEEK_SET);
fread(&data,sizeof(data),1,bmpfp);
printf("该点蓝色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("该点绿色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("该点红色分量:%d\n",data);
}
}else{
printf("不是真彩位图!");
}
}
给你找了份示例代码
#include
int main()
{
int width,height,x,y;
unsigned short bitCount;
int offbits;
int bitPerLine;
unsigned char data;
FILE* bmpfp = fopen("E:\\风景\\风景1.bmp","rb");
fseek(bmpfp,18,SEEK_SET);
fread(&width,sizeof(int),1,bmpfp);
fread(&height,sizeof(int),1,bmpfp);
printf("width : %d , height : %d\n",width,height);
fseek(bmpfp,2,SEEK_CUR);
fread(&bitCount,sizeof(bitCount),1,bmpfp);
fseek(bmpfp,10,SEEK_SET);
fread(&offbits,sizeof(int),1,bmpfp);
if(bitCount==24){
bitPerLine = ( (width*3)%4==0 ) ? width*3 : ( (width*3)/4 )*4 + 4;
while(1){
printf("请输出坐标:");
scanf("%d%d",&x,&y);
if(x>width||y>height) return 0;
fseek(bmpfp, 18 + offbits + bitPerLine * y + 3*x , SEEK_SET);
fread(&data,sizeof(data),1,bmpfp);
printf("该点蓝色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("该点绿色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("该点红色分量:%d\n",data);
}
}else{
printf("不是真彩位图!");
}
}
/*
运行结果:
width : 700 , height : 382
请输出坐标:0 0
该点蓝色分量:68该点绿色分量:82该点红色分量:80
*/