在.h头文件
#include "vfw.h"
#include "mmsystem.h"
#pragma comment (lib,"vfw32.lib")
#pragma comment (lib,"winmm.lib")
全局变量
int nFrames = 0;
PAVIFILE pfile;
AVISTREAMINFO strhdr;
PAVISTREAM ps;
HRESULT hr;
AVICOMPRESSOPTIONS pCompressOption;
AVICOMPRESSOPTIONS FAR * opts[1] = {&pCompressOption};
PAVISTREAM pComStream;
这里就不说如何用dib截屏了
BITMAPINFOHEADER *pBInfo = new BITMAPINFOHEADER;
pBInfo->biBitCount = 24;
pBInfo->biClrImportant = 0;
pBInfo->biCompression = 0;
pBInfo->biHeight = DIBitmap.bmHeight;
pBInfo->biPlanes = DIBitmap.bmPlanes;
pBInfo->biSize = sizeof(BITMAPINFOHEADER);
pBInfo->biSizeImage = DIBitmap.bmWidthBytes * DIBitmap.bmHeight;
pBInfo->biWidth = DIBitmap.bmWidth;
pBInfo->biXPelsPerMeter = 0;
pBInfo->biYPelsPerMeter = 0;
if(nFrames == 0)
{
AVIFileInit();
AVIFileOpen(&pfile,"录像.avi",OF_WRITE | OF_CREATE,NULL);
memset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = 5;
strhdr.dwSuggestedBufferSize = pBInfo->biSizeImage;
SetRect(&strhdr.rcFrame,0,0,pBInfo->biWidth,pBInfo->biHeight);
hr = AVIFileCreateStream(pfile,&ps,&strhdr);
opts[0]->fccType = streamtypeVIDEO;
opts[0]->fccHandler = mmioStringToFOURCC("MSVC", 0);
opts[0]->dwQuality = 7500;
opts[0]->dwBytesPerSecond = 0;
opts[0]->dwFlags = AVICOMPRESSF_VALID || AVICOMPRESSF_KEYFRAMES;
opts[0]->lpFormat = 0;
opts[0]->cbFormat = 0;
opts[0]->dwInterleaveEvery = 0;
AVIMakeCompressedStream(&pComStream,ps,&pCompressOption,NULL);
AVIStreamSetFormat(pComStream,0,pBInfo,sizeof(BITMAPINFOHEADER));
}
hr = AVIStreamWrite(pComStream,nFrames ,1,(LPBYTE)p+54,
pBInfo->biSizeImage,AVIIF_KEYFRAME,NULL,NULL);
nFrames++;
delete pBInfo ;
结束的函数中加入
if (pComStream != 0)
AVIStreamClose(pComStream);
if (ps != 0)
AVIStreamClose(ps);
if(pfile != NULL)
AVIFileRelease(pfile);
AVIFileExit();