Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

Questionselect output device Pin Pinmemberkinani11-Aug-11 7:32 
GeneralThank you PinmemberXiaohu Shao26-Jul-10 18:08 
Generalerror C2504: 'IDXEffect' and error C2787: 'IVideoWindow' : PinmemberVirex_A30-Oct-08 15:54 
Generalcan't grub from the "*.mp4" Pinmemberlaguna_leo27-Sep-08 6:23 
Generalto all - if doesn't run DX9 Pinmembergrandmasta126-Aug-07 1:20 
QuestionUrgent Problem... PinmemberTushar Jadhav22-Jun-07 20:22 
Generalimage won't fit to picturebox(CStatic) PinmemberEd_lon17-May-07 21:49 
Generalhi Pinmembervikram panwar27-Mar-07 0:54 
GeneralRe: hi Pinmemberrajendra@yahoo.com27-Mar-07 22:07 
GeneralIt Does not run in Win2k Pinmemberrajendra@yahoo.com22-Mar-07 7:45 
QuestionThe other way around Pinmembershfnet13-Mar-07 5:29 
AnswerRe: The other way around Pinmembertanvon malik21-Nov-07 18:57 
Questionhow vedio file(mpeg4) is streamed PinmemberMember #248359415-Feb-07 19:38 
GeneralProblem opening file with bad indexes PinmemberSbarBaz29-Sep-06 7:07 
GeneralConflict between directshow and wmplayer. Help! Pinmemberaritosteles10-Sep-06 13:13 
GeneralError when compiling Pinmemberarindam_stcet3-May-06 5:19 
GeneralError: Could not grab the frame. PinmemberVenu Gopal Lolla13-Feb-06 20:09 
GeneralRe: Error: Could not grab the frame. Pinmemberbeatbox24-Mar-06 3:50 
QuestionTheoretical misunderstanding of a process Pinmemberwhitesail6-Dec-05 14:25 
Generalcouldnt compile PinmemberUsman mani16-Mar-05 23:50 
GeneralError saving the grabbed frame to bmp file PinmemberA. Asif Raza15-Mar-05 11:38 
Questionhow to grab images from .mov files Pinmembersrkrishna25-Jan-05 5:23 
GeneralNo Video and Audio Pinmemberbozitaai11-Jan-05 20:32 
Generali got this error while building this project Pinmemberslucky18-Dec-04 16:19 
GeneralRe: i got this error while building this project PinmemberJohn4420-Dec-04 21:59 
GeneralRe: i got this error while building this project Pinmemberpig head xiaoma8-Jun-05 18:38 
GeneralRe: i got this error while building this project PinmemberJohn4410-Jun-05 0:36 
GeneralRe: i got this error while building this project PinmemberSurindra5-Jun-07 20:54 
QuestionRe: i got this error while building this project Pinmemberside218-Jun-07 7:18 
QuestionRe: i got this error while building this project PinmemberbrianA493-Oct-06 10:55 
GeneralRe: i got this error while building this project Pinmembernicostar20-Dec-04 22:39 
GeneralRe: i got this error while building this project PinmemberAndrey_Mark19-Aug-09 6:06 
GeneralCompressed Files, HOW GRABBER PinsussAnonymous21-Sep-04 19:53 
Generalfor audio data Pinmemberslampiggy27-May-04 9:53 
GeneralOnly Sound no Pics Pinmemberwijesijp19-May-04 21:06 
GeneralGuidance PinmemberChivalrous13-Apr-04 20:20 
Generali can't run FrameGrabberDemo Pinmemberangel4119-Mar-04 17:26 
GeneralRe: i can't run FrameGrabberDemo PinmemberMarkus Axelsson10-Mar-04 11:40 
QuestionCan not run properly in DirectX 9 Pinmemberyouken30-Dec-03 18:10 
GeneralInserting a Frame.! Pinmembernewnick16-Dec-03 18:45 
GeneralEasy way to do same... Pinmemberyooyo3d9-Dec-03 13:18 
GeneralHe's right IMediaDet is the way to go Pinmembersaltynuts200231-Dec-04 16:15 
GeneralSolution for most of Error Pinmembersriniss8-Dec-03 21:32 
GeneralRe: Solution for most of Error PinsussAnonymous2-Jan-04 6:52 
Questionhow to extract clip of specified duration out of an avi or anyother file Pinmemberhina_askh2-Dec-03 16:05 
Generalvideo doesn't play Pinmembertoms2k10-Nov-03 3:40 
GeneralRe: video doesn't play PinmemberDeeKe15-Nov-03 23:00 
GeneralRe: video doesn't play Pinmembernobird2-Dec-03 22:14 
GeneralRe: video doesn't play Pinmemberkurtas2-Nov-05 10:06 
GeneralRe: video doesn't play PinsussJabran11-Dec-03 11:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web02 | 2.8.141223.1 | Last Updated 5 Sep 2001
Article Copyright 2001 by Markus Axelsson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid