failed to take a snapshot on the video.
BOOL CCaptureClass::Picture(CString fileName)
{
if ( m_pBV )
{
long bitmapSize = 0;
if ( SUCCEEDED(m_pBV->GetCurrentImage(&bitmapSize,0)))
{
bool pass =false;
unsigned char * buffer=new unsigned char[bitmapSize];
HRESULT hr = m_pBV->GetCurrentImage(&bitmapSize,(long*)buffer); // hr's value is E_UNEXPECTED
if ( SUCCEEDED(hr))
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
lpbi = (LPBITMAPINFOHEADER)buffer;
int nColors = 0;
if (lpbi->biBitCount < 8)
{
nColors = 1 << lpbi->biBitCount;
}
hdr.bfType=((WORD)('M'<<8) | 'B');
hdr.bfSize=bitmapSize + sizeof(hdr);
hdr.bfReserved1=0;
hdr.bfReserved2=0;
hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER)+lpbi->biSize+nColors*sizeof(RGBQUAD));
CFile bitmapFile(fileName,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary);
bitmapFile.Write(&hdr,sizeof(BITMAPFILEHEADER));
bitmapFile.Write(buffer,bitmapSize);
bitmapFile.Close();
pass = true;
}
delete [] buffer;
return pass;
}
}
return false;}
HRESULT CCaptureClass::InitCaptureGraphBuilder()
{
HRESULT hr;
hr = CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,IID_IGraphBuilder,(void**)&m_pGB);
if ( FAILED(hr) ) return hr;hr = CoCreateInstance(CLSID_CaptureGraphBuilder2,NULL,CLSCTX_INPROC,IID_ICaptureGraphBuilder2,(void**)&m_pCapture);
if ( FAILED(hr) ) return hr;
m_pCapture->SetFiltergraph(m_pGB);hr = m_pGB->QueryInterface(IID_IMediaControl,(void**)&m_pMC);
if ( FAILED(hr) ) return hr;hr = m_pGB->QueryInterface(IID_IVideoWindow,(LPVOID*)&m_pVW);
if ( FAILED(hr) ) return hr;hr = m_pGB->QueryInterface(IID_IBasicVideo, (void**)&m_pBV);
return hr;
}
You can download the compressed file of the project: http://www.yanglihao.com/filesUploaded/videoCapture.rar
Thank you.- Edited bycutedevil Tuesday, November 03, 2009 11:56 AMwrong
- Moved byThe March HareMVP, ModeratorTuesday, November 03, 2009 3:15 PMdshow related (From:Media Foundation Development)
All Replies
- If you have a failure there, then perhaps you have an error code. What is it?
http://alax.info/blog/tag/directshow - I don't find the error code yet. I know that the firs code of "m_pBV->GetCurrentImage(&bitmapSize,0)" returns "S_OK".
Is there a function like GetLastError() that can let me get the Error Code. - "hr" is already an error code, you don't need a function for that.
http://alax.info/blog/tag/directshow


