Click here to Skip to main content
12,556,918 members (50,331 online)
Click here to Skip to main content
Add your own
alternative version


113 bookmarked

Extracting bitmaps from movies using DirectShow

, 4 Sep 2001
Rate this:
Please Sign up or sign in to vote.
An article showing how to extract a frame from a movie using DirectShow
<!-- Download Links -->

Sample Image - FrameGrabberDemo.gif


This article explains how to use the ISampleGrabber interface to grab a frame from a movie. We'll add the SampleGrabber filter to the graphbuilders filter list and use it to extract the bitmap. It'll show the necessary steps how to create the filter, create the graph, start it upp and grab the frame.

Setup the enviroment, we are using ATL smartpointers and DirectShow

#include "AtlBase.h"	// For atl smart pointers
#include "dShow.h"	// DirectShow header
#include "Qedit.h"	// SampleGrabber filter

The project has to be linked with Strmbase.lib

Since we're using COM we have to call CoInitialize() and CoUninitialize() in InitInstance, make sure the dialog destructor is called before CoUninitialize is called.

BOOL CFrameGrabberApp::InitInstance()
		CFrameGrabberDemoDlg dlg;
		m_pMainWnd = &dlg;
		int nResponse = dlg.DoModal();

Step 1: Create the GraphBuilder

CComPtr<IGraphBuilder> pGraphBuilder;
HRESULT hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, 

Step 2: Create the Grabber filter and add it to the graph builder

CComPtr<IBaseFilter> pGrabberBaseFilter;
CComPtr<ISampleGrabber> pSampleGrabber;
hr = ::CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
                        IID_IBaseFilter, (LPVOID *)&pGrabberBaseFilter);
if (FAILED(hr))
	return hr;
pGrabberBaseFilter->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabber);
if (pSampleGrabber == NULL)
hr = pGraphBuilder->AddFilter(pGrabberBaseFilter,L"Grabber");
if (FAILED(hr))
	return hr;

Step 3: Setup the media type we're interrested in and render the file. The graph builder will now setup all the filters it needs to render the movie including the sample grabber we added.

mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
mt.formattype = FORMAT_VideoInfo; 
hr = pSampleGrabber->SetMediaType(&mt);
if (FAILED(hr)) 
	return hr;
hr = pGraphBuilder->RenderFile(wFile,NULL); 
if (FAILED(hr)) 
	return hr;

Now when the graph is created we need to tell the sample grabber to stop the graph after receiving one sample, we also tell it to copy the sample data into it's internal buffer.

hr = pSampleGrabber->SetBufferSamples(TRUE);
if (FAILED(hr)) 
	return hr; 
hr = pSampleGrabber->SetOneShot(TRUE); 
if (FAILED(hr)) 
return hr;

Step 4: Now we run the graph and collects the data from the sample grabber.

hr = pMediaControl->Run();
if (FAILED(hr)) 
	return hr; 
long evCode;
hr = pMediaEventEx->WaitForCompletion(INFINITE, &evCode); 
if (FAILED(hr)) 
	return hr; 
hr = pSampleGrabber->GetConnectedMediaType(&MediaType); 
if (FAILED(hr)) 
	return hr; 
// Get a pointer to the video header. 
VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)MediaType.pbFormat; 
if (pVideoHeader == NULL) 
	return E_FAIL; 
// The video header contains the bitmap information. 
// Copy it into a BITMAPINFO structure. 
ZeroMemory(&BitmapInfo, sizeof(BitmapInfo)); 
CopyMemory(&BitmapInfo.bmiHeader, &(pVideoHeader->bmiHeader), 

// Create a DIB from the bitmap header, and get a pointer to the buffer. 
void *buffer = NULL; 
HBITMAP hBitmap = ::CreateDIBSection(0, &BitmapInfo, DIB_RGB_COLORS, &buffer, 
                                     NULL, 0); 
// Copy the image into the buffer. 
long size = 0; 
hr = pSampleGrabber->GetCurrentBuffer(&size,(long *)buffer);   
if (FAILED(hr)) 
	return  hr;

Now we have the bitmap handle, the demo program takes the sample one second in the movie and displays it to the user using an picture box.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

Questionselect output device Pin Pin
kinani11-Aug-11 6:32
memberkinani11-Aug-11 6:32 
GeneralThank you Pin
Xiaohu Shao26-Jul-10 17:08
memberXiaohu Shao26-Jul-10 17:08 
Generalerror C2504: 'IDXEffect' and error C2787: 'IVideoWindow' : Pin
Virex_A30-Oct-08 14:54
memberVirex_A30-Oct-08 14:54 
Generalcan't grub from the "*.mp4" Pin
laguna_leo27-Sep-08 5:23
memberlaguna_leo27-Sep-08 5:23 
Generalto all - if doesn't run DX9 Pin
grandmasta126-Aug-07 0:20
membergrandmasta126-Aug-07 0:20 
QuestionUrgent Problem... Pin
Tushar Jadhav22-Jun-07 19:22
memberTushar Jadhav22-Jun-07 19:22 
Generalimage won't fit to picturebox(CStatic) Pin
Ed_lon17-May-07 20:49
memberEd_lon17-May-07 20:49 
Generalhi Pin
vikram panwar26-Mar-07 23:54
membervikram panwar26-Mar-07 23:54 
GeneralRe: hi Pin
rajendra@yahoo.com27-Mar-07 21:07
memberrajendra@yahoo.com27-Mar-07 21:07 
GeneralIt Does not run in Win2k Pin
rajendra@yahoo.com22-Mar-07 6:45
memberrajendra@yahoo.com22-Mar-07 6:45 
QuestionThe other way around Pin
shfnet13-Mar-07 4:29
membershfnet13-Mar-07 4:29 
AnswerRe: The other way around Pin
tanvon malik21-Nov-07 17:57
membertanvon malik21-Nov-07 17:57 
Questionhow vedio file(mpeg4) is streamed Pin
Member #248359415-Feb-07 18:38
memberMember #248359415-Feb-07 18:38 
GeneralProblem opening file with bad indexes Pin
SbarBaz29-Sep-06 6:07
memberSbarBaz29-Sep-06 6:07 
GeneralConflict between directshow and wmplayer. Help! Pin
aritosteles10-Sep-06 12:13
memberaritosteles10-Sep-06 12:13 
GeneralError when compiling Pin
arindam_stcet3-May-06 4:19
memberarindam_stcet3-May-06 4:19 
GeneralError: Could not grab the frame. Pin
Venu Gopal Lolla13-Feb-06 19:09
memberVenu Gopal Lolla13-Feb-06 19:09 
GeneralRe: Error: Could not grab the frame. Pin
beatbox24-Mar-06 2:50
memberbeatbox24-Mar-06 2:50 
QuestionTheoretical misunderstanding of a process Pin
whitesail6-Dec-05 13:25
memberwhitesail6-Dec-05 13:25 
Generalcouldnt compile Pin
Usman mani16-Mar-05 22:50
memberUsman mani16-Mar-05 22:50 
GeneralError saving the grabbed frame to bmp file Pin
A. Asif Raza15-Mar-05 10:38
memberA. Asif Raza15-Mar-05 10:38 
Questionhow to grab images from .mov files Pin
srkrishna25-Jan-05 4:23
membersrkrishna25-Jan-05 4:23 
GeneralNo Video and Audio Pin
bozitaai11-Jan-05 19:32
memberbozitaai11-Jan-05 19:32 
Generali got this error while building this project Pin
slucky18-Dec-04 15:19
memberslucky18-Dec-04 15:19 
GeneralRe: i got this error while building this project Pin
John4420-Dec-04 20:59
memberJohn4420-Dec-04 20:59 
GeneralRe: i got this error while building this project Pin
pig head xiaoma8-Jun-05 17:38
memberpig head xiaoma8-Jun-05 17:38 
GeneralRe: i got this error while building this project Pin
John449-Jun-05 23:36
memberJohn449-Jun-05 23:36 
GeneralRe: i got this error while building this project Pin
Surindra5-Jun-07 19:54
memberSurindra5-Jun-07 19:54 
QuestionRe: i got this error while building this project Pin
side218-Jun-07 6:18
memberside218-Jun-07 6:18 
QuestionRe: i got this error while building this project Pin
brianA493-Oct-06 9:55
memberbrianA493-Oct-06 9:55 
GeneralRe: i got this error while building this project Pin
nicostar20-Dec-04 21:39
membernicostar20-Dec-04 21:39 
GeneralRe: i got this error while building this project Pin
Andrey_Mark19-Aug-09 5:06
memberAndrey_Mark19-Aug-09 5:06 
GeneralCompressed Files, HOW GRABBER Pin
Anonymous21-Sep-04 18:53
sussAnonymous21-Sep-04 18:53 
Generalfor audio data Pin
slampiggy27-May-04 8:53
memberslampiggy27-May-04 8:53 
GeneralOnly Sound no Pics Pin
wijesijp19-May-04 20:06
memberwijesijp19-May-04 20:06 
GeneralGuidance Pin
Chivalrous13-Apr-04 19:20
memberChivalrous13-Apr-04 19:20 
Generali can't run FrameGrabberDemo Pin
angel4119-Mar-04 16:26
memberangel4119-Mar-04 16:26 
GeneralRe: i can't run FrameGrabberDemo Pin
Markus Axelsson10-Mar-04 10:40
memberMarkus Axelsson10-Mar-04 10:40 
QuestionCan not run properly in DirectX 9 Pin
youken30-Dec-03 17:10
memberyouken30-Dec-03 17:10 
GeneralInserting a Frame.! Pin
newnick16-Dec-03 17:45
membernewnick16-Dec-03 17:45 
GeneralEasy way to do same... Pin
yooyo3d9-Dec-03 12:18
memberyooyo3d9-Dec-03 12:18 
GeneralHe's right IMediaDet is the way to go Pin
saltynuts200231-Dec-04 15:15
membersaltynuts200231-Dec-04 15:15 
GeneralSolution for most of Error Pin
sriniss8-Dec-03 20:32
membersriniss8-Dec-03 20:32 
GeneralRe: Solution for most of Error Pin
Anonymous2-Jan-04 5:52
sussAnonymous2-Jan-04 5:52 
Questionhow to extract clip of specified duration out of an avi or anyother file Pin
hina_askh2-Dec-03 15:05
memberhina_askh2-Dec-03 15:05 
Generalvideo doesn't play Pin
toms2k10-Nov-03 2:40
membertoms2k10-Nov-03 2:40 
GeneralRe: video doesn't play Pin
DeeKe15-Nov-03 22:00
memberDeeKe15-Nov-03 22:00 
GeneralRe: video doesn't play Pin
nobird2-Dec-03 21:14
membernobird2-Dec-03 21:14 
GeneralRe: video doesn't play Pin
kurtas2-Nov-05 9:06
memberkurtas2-Nov-05 9:06 
GeneralRe: video doesn't play Pin
Jabran11-Dec-03 10:47
sussJabran11-Dec-03 10:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161026.1 | Last Updated 5 Sep 2001
Article Copyright 2001 by Markus Axelsson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid