去年有个项目,要求将AVI转成BMP图片,实现有几个办法.其中一种就是利用MS的vfw32.lib来开发.
还可以用DIRECTSHOW来开发,不过对于没学过DIRECTSHOW的朋友来说,相对就复杂了.以下是部分源码.
给大家烟酒,最早我也是从清华的BBS上拿来的.不过如果真要掌握位图的相关知识,建议买本人民邮电出版的<VISUAL C++数字图像获取,处理与实践应用>一书,较全面地介绍了数字图像技术.
AfxGetApp()->DoWaitCursor(1);
GetDlgItem(IDC_BUTTONOK)->EnableWindow(false);
PAVIFILE pfile;//Pointer to a buffer that receives the new IAVIFile interface pointer.
AVIFILEINFO pfi;
PAVISTREAM pavi;
PGETFRAME pgf;
long length, start;
//initializes the AVIFile library,as AVIFileExit()。
//Call AVIFileInit() before using any other AVIFile functions
AVIFileInit();
AVIFileOpen(&pfile,m_avifile,OF_READ |OF_SHARE_DENY_NONE,NULL);
//obtains information about an AVI file
AVIFileInfo(pfile,&pfi,sizeof(AVIFILEINFO));
AVIFileGetStream(pfile,&pavi,streamtypeVIDEO,0);
length=AVIStreamLength(pavi);
start=AVIStreamStart(pavi);
AVISTREAMINFO si;
AVIStreamInfo(pavi, &si, sizeof(si));
pgf = AVIStreamGetFrameOpen(pavi, NULL);
//GET FIRST BMP
LPBITMAPINFOHEADER bmp1;
bmp1= (LPBITMAPINFOHEADER)AVIStreamGetFrame(pgf, 0);
CClientDC dc(this);
//DEFAUT HEAD
BITMAPINFOHEADER bmih;
memset(&bmih,0,sizeof(BITMAPINFOHEADER));
bmih.biBitCount=bmp1->biBitCount ; //Modif by Deng
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = pfi.dwWidth;
bmih.biHeight = pfi.dwHeight;
bmih.biPlanes =bmp1->biPlanes; //Modif By Deng Chun Ling
bmih.biCompression =BI_RGB;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
BITMAPFILEHEADER bmfh;
bmfh.bfSize = sizeof(bmfh);
//bmfh.bfSize=sizeof(bmp1); //modi by deng chun ling
bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih); //BITMAPINFOHEADER+BITMAPINFOHEADER
bmfh.bfType = (int)'M' * 256 + 'B';
// bmfh.bfType=0x424D;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
BYTE *lpbuffa=new BYTE[(((pfi.dwWidth*bmih.biBitCount)+31)/8)*pfi.dwHeight];
BYTE* lpbuff;
m_editqty=pfi.dwLength;
UpdateData(false);
//Limite to 100
// if (pfi.dwLength>100) pfi.dwLength=100;
for(LONG i=1;i<(LONG)pfi.dwLength;i++)
{
lpbuff=lpbuffa;
lpbuff=(BYTE *)AVIStreamGetFrame(pgf,i);//
lpbuff+=40;
//TranslationDIB1(lpbuff,bmih.biWidth,bmih.biHeight,10,50);//平移function
char name1[100];
strcpy(name1,m_target);//目的路徑
TCHAR name3[50];
wsprintf(name3, TEXT("Bitmap%5.5ld.bmp/0"), long(i) );
strcat(name1,name3);
////Write BMP File。
CFile ff(name1, CFile::modeWrite | CFile::modeCreate); //
ff.Write(&bmfh, sizeof(bmfh));//bmfh-->ff
ff.Write(&bmih, 40);
ff.Write(lpbuff,(((pfi.dwWidth*bmih.biBitCount)+31)/8)*pfi.dwHeight);
ff.Close();
Sleep(10);
}
delete[] lpbuffa; //delete block
m_editw=i;
UpdateData(false);
//release file
AVIStreamGetFrameClose(pgf);
AVIFileRelease(pfile);
//Use the AVIFileExit() function to release the AVIFile library
//and decrement the reference count.同AVIFileInit()??。
AVIFileExit();
ReleaseDC(&dc);
AfxGetApp()->DoWaitCursor(1);
MessageBox("CONVERT COMPLETED!","NOTE",MB_ICONINFORMATION);
GetDlgItem(IDC_BUTTONOK)->EnableWindow(true);