读书人

大大来帮帮忙。多谢

发布时间: 2012-02-08 19:52:21 作者: rapoo

大大来帮帮忙。。。谢谢
问一下
用vs怎么实现将一个bmp图像转换成二进制流啊。。。
谢谢。。。

[解决办法]
以下代码打开的是24位BMP,打开的时候以二进制方式打开就行了


int OpenAndResult1(char FileName[],double *resultbmp)
{
//unsigned char *buffer = NULL;//存储指针表的数据

int g_Height=0;//位图高
int g_Width=0;//位图宽
int g_Bitcount=0;//位图颜色位数

//////////////////////////获取图片的高宽及像素位数/////////////////////////////////////////////////////////////////////////
FILE *fp;

if((fp=fopen(FileName,"rb"))==NULL) //打开文件,并且判断文件是否存在
{
printf("open the file error!\n");
return -1;
}


fseek(fp,18,SEEK_SET);
fread(&g_Width,4,1,fp); //图象宽度 ok
fread(&g_Height,4,1,fp); //图象长度 ok
fseek(fp,14+4+4+4+2,SEEK_SET);
fread(&g_Bitcount,2,1,fp); //颜色数1,4,8,24,32


if( g_Bitcount != 24 )//是否24位
{
printf(" the file isn't 24\n");
return -2;
}

/////////////////// 图片定位 /////////////////////////////////////////////////////////////////////////////////////1


//int i,j;

int lLineBytes24 = ((g_Width * 3 + 3)>>2)<<2;//24位图片,必须是4的倍数,(原图片的每行字节数)
int lLineByte256 = (g_Width+3)/4*4;//<<2)>>2;//256色图片每行字节数

///////// 把图片数据考到内存中(以便对图片操作速度快) ////////////
unsigned char *copytobuffer = NULL;//存储图片的数据

copytobuffer = (unsigned char *)calloc(g_Height * lLineBytes24 + 1,sizeof(unsigned char));
if( NULL == copytobuffer)
{
printf(" the copytobuffer is failure of allocation Memory\n");
return -3;
}

fseek(fp, 54, SEEK_SET );
fread(copytobuffer,1,g_Height * lLineBytes24,fp);//*/

fclose(fp);

return 1;
}
[解决办法]
up

读书人网 >C++

热点推荐