24位RGB数据保存为BMP图片
2014-05-21 10:48
381 查看
实现过程:
A、写入文件头
B、写入信息头
C、写入图像RGB数据
(无调色板)
//////////////////////////////////////////////////////////////////////////////////////////
程序在VC6.0下实现:
[cpp] view
plaincopy
//保存buffer到bmp文件
//iWidth:图像宽; iHeight:图像高;pBuffer:图像RGB数据;filePath:存储路径;fileName:保存文件名;fileNum:保存文件编号
//SaveDIB2Bmp(100, "frame", "D://screenshot")
bool MyDC::SaveDIB2Bmp(int fileNum, const char * fileName, const char * filePath, int iWidth, int iHeight, BYTE *pBuffer)
{
BITMAPINFOHEADER bih;
ConstructBih(iWidth,iHeight,bih);
BITMAPFILEHEADER bhh;
ContructBhh(iWidth,iHeight,bhh);
TCHAR BMPFileName[1024];
int widthStep = (((iWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
int DIBSize = widthStep * iHeight ; //buffer的大小 (字节为单位)
//save
char path[1024];
char str[1024];
sprintf(str,"%d",fileNum);
strcat(str, fileName);
strcat(str,".bmp"); //frame100.bmp
sprintf(path,"%s", filePath);
strcat(path,str); //Path//frame100.bmp
strcpy(BMPFileName,path);
CFile file;
try
{
if(file.Open(BMPFileName,CFile::modeWrite | CFile::modeCreate))
{//写入文件
file.Write((LPSTR)&bhh,sizeof(BITMAPFILEHEADER));
file.Write((LPSTR)&bih,sizeof(BITMAPINFOHEADER));
file.Write(pBuffer,DIBSize);
file.Close();
return true;
}
}
catch (...)
{
AfxMessageBox("MyDC::SaveDIB2Bmp");
}
return false;
}
//构建BMP位图文件头
void MyDC::ContructBhh(int nWidth,int nHeight,BITMAPFILEHEADER& bhh) //add 2010-9-04
{
int widthStep = (((nWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
bhh.bfType = ((WORD) ('M' << 8) | 'B'); //'BM'
bhh.bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + widthStep * nHeight;
bhh.bfReserved1 = 0;
bhh.bfReserved2 = 0;
bhh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
}
//构建BMP文件信息头
void MyDC::ConstructBih(int nWidth,int nHeight,BITMAPINFOHEADER& bih)
{
int widthStep = (((nWidth * 24) + 31) & (~31)) / 8 ;
bih.biSize=40; // header size
bih.biWidth=nWidth;
bih.biHeight=nHeight;
bih.biPlanes=1;
bih.biBitCount=24; // RGB encoded, 24 bit
bih.biCompression=BI_RGB; // no compression 非压缩
bih.biSizeImage=widthStep*nHeight*3;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
bih.biClrUsed=0;
bih.biClrImportant=0;
}
A、写入文件头
B、写入信息头
C、写入图像RGB数据
(无调色板)
//////////////////////////////////////////////////////////////////////////////////////////
程序在VC6.0下实现:
[cpp] view
plaincopy
//保存buffer到bmp文件
//iWidth:图像宽; iHeight:图像高;pBuffer:图像RGB数据;filePath:存储路径;fileName:保存文件名;fileNum:保存文件编号
//SaveDIB2Bmp(100, "frame", "D://screenshot")
bool MyDC::SaveDIB2Bmp(int fileNum, const char * fileName, const char * filePath, int iWidth, int iHeight, BYTE *pBuffer)
{
BITMAPINFOHEADER bih;
ConstructBih(iWidth,iHeight,bih);
BITMAPFILEHEADER bhh;
ContructBhh(iWidth,iHeight,bhh);
TCHAR BMPFileName[1024];
int widthStep = (((iWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
int DIBSize = widthStep * iHeight ; //buffer的大小 (字节为单位)
//save
char path[1024];
char str[1024];
sprintf(str,"%d",fileNum);
strcat(str, fileName);
strcat(str,".bmp"); //frame100.bmp
sprintf(path,"%s", filePath);
strcat(path,str); //Path//frame100.bmp
strcpy(BMPFileName,path);
CFile file;
try
{
if(file.Open(BMPFileName,CFile::modeWrite | CFile::modeCreate))
{//写入文件
file.Write((LPSTR)&bhh,sizeof(BITMAPFILEHEADER));
file.Write((LPSTR)&bih,sizeof(BITMAPINFOHEADER));
file.Write(pBuffer,DIBSize);
file.Close();
return true;
}
}
catch (...)
{
AfxMessageBox("MyDC::SaveDIB2Bmp");
}
return false;
}
//构建BMP位图文件头
void MyDC::ContructBhh(int nWidth,int nHeight,BITMAPFILEHEADER& bhh) //add 2010-9-04
{
int widthStep = (((nWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
bhh.bfType = ((WORD) ('M' << 8) | 'B'); //'BM'
bhh.bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + widthStep * nHeight;
bhh.bfReserved1 = 0;
bhh.bfReserved2 = 0;
bhh.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
}
//构建BMP文件信息头
void MyDC::ConstructBih(int nWidth,int nHeight,BITMAPINFOHEADER& bih)
{
int widthStep = (((nWidth * 24) + 31) & (~31)) / 8 ;
bih.biSize=40; // header size
bih.biWidth=nWidth;
bih.biHeight=nHeight;
bih.biPlanes=1;
bih.biBitCount=24; // RGB encoded, 24 bit
bih.biCompression=BI_RGB; // no compression 非压缩
bih.biSizeImage=widthStep*nHeight*3;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
bih.biClrUsed=0;
bih.biClrImportant=0;
}
相关文章推荐
- 24位RGB数据保存为BMP图片
- 24位RGB数据保存为BMP图片
- RGB数据保存为BMP图片
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
- [转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
- Linux下获取显存一帧数据并保存为bmp图片
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
- RGB数据写bmp图片文件遇到的这些坑
- windows 保存rgb数据为bmp文件
- 保存客户区数据成BMP图片
- OpenGL保存bmp图片(字节对齐与RGB分量转换)
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
- 24位bmp图片文件头、信息头的二进制数据
- 将rgb图像数据保存为BMP格式图片的方法
- 24位和8位BMP图片保存纯C代码
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
- MFC获取rgb图像数据后动态显示及保存图片的方法
- 将rgb图像数据保存为BMP格式图片的方法
- ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片