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

DirectShow VB.NET Example

, 24 Dec 2008 Public Domain
Rate this:
Please Sign up or sign in to vote.
A example to show how to use DirectShow in VB.NET.


This application can play almost all media types. DirectShow can play a lot more file types but I haven't got around to writing the code. You can use this in your applications. DirectX is very reliable. I wrote this because there where no VB.NET source code on how to use DirectShow to Play Video (.avi, .mpg, .wmv) and Play Audio (.mp3, .wav). I converted it from a C# Example that was included with the DirectShow SDK. It still needs a lot of work. This is the first one that I know of. I may be wrong.



I converted a C# sample from the DirectShow SDK and got started. This is a example and still needs work. I included the DirectShow Library DLL. It's very easy to use.

Using the Code

Make sure DirectShow Library DLL is referenced in your application and loads a new graph. To use this application choose the handle you want the video To be displayed on. And call the Sub OpenFile(FilePath, Handle of Window, Control Name).

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object,
        ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If e.Cancel = True Then Exit Sub
        'Call The Sub to Open the File. OpenFile(FilePath, Handle of Window, Control Name)
        OpenFile(OpenFileDialog1.FileName, VidScreenBox.Handle, Me)
    End Sub

The Sub OpenFile sets the properties. It sets the owner to the Forms Handle that will play the Video if its a Video File.

Private Sub OpenFile(ByVal fName As String, ByVal VidHand As IntPtr,
    ByVal VidCtrl As System.Windows.Forms.Control)
        ' Make sure everything is closed
        filename = ""

        MaxToolStripMenuItem.Enabled = True
        MinToolStripMenuItem.Enabled = True
        CustomToolStripMenuItem.Enabled = True
        NormalPlaybackRateToolStripMenuItem.Enabled = True
        DecreasePlaybackRateToolStripMenuItem.Enabled = False
        IncreasePlaybackRateToolStripMenuItem.Enabled = True
        HalfSpeedToolStripMenuItem.Enabled = True
        DoubleSpeedToolStripMenuItem.Enabled = True
        DoubleSize200ToolStripMenuItem.Enabled = True
        NormalSize100ToolStripMenuItem.Enabled = True
        HalfSize50ToolStripMenuItem.Enabled = True
        Size75ToolStripMenuItem.Enabled = True
        FullScreenToolStripMenuItem.Enabled = True

        UseHand = VidHand 'Handle to Display Video if any
        UseCtrl = VidCtrl 'Control to Display Video if any

        filename = fName
        currentState = PlayState.Stopped 'Reset State to Stopped
        currentVolume = VolumeFull 'Reset Volume

        PlayMedia(fName) 'Call Main Sub
        Call SetFormTitle()
        Timer1.Enabled = True
    End Sub

The next Sub loads the Graph Builder and Loads the interfaces DirectShow uses to Play Audio and Render the Video to the Forms Handle. After you call these subs. You can control the Audio and Video using the DirectShow Interfaces mediaControl, mediaEventEx, basicAudio, basicVideo, videoWindow, MediaPosition...Easy.

Private Sub PlayMedia(ByVal fName As String)
        Dim hr As Integer = 0
        If fName = Nothing Then Exit Sub
            graphBuilder = DirectCast(New FilterGraph,
                IFilterGraph2) 'Load Graph Builder Device

            hr = graphBuilder.RenderFile(fName, Nothing) ' Initialize Graph Builder

            'Load all Interfaces we will use
            mediaControl = DirectCast(graphBuilder, IMediaControl)
            mediaEventEx = DirectCast(graphBuilder, IMediaEventEx)
            mediaSeeking = DirectCast(graphBuilder, IMediaSeeking)
            mediaPosition = DirectCast(graphBuilder, IMediaPosition)
            videoWindow = DirectCast(graphBuilder, IVideoWindow)
            basicAudio = DirectCast(graphBuilder, IBasicVideo)
            basicVideo = DirectCast(graphBuilder, IBasicAudio)

            Call CheckType() 'Check to See if Audio or Video Call

            If isAudioOnly = False Then
                'Notfy Window of Video
                hr = mediaEventEx.SetNotifyWindow(UseHand, WMGraphNotify, IntPtr.Zero)

                'Set Owner to Display Video
                hr = videoWindow.put_Owner(UseHand)

                'Set Owner Video Style
                hr = videoWindow.put_WindowStyle(
                   WindowStyle.Child And WindowStyle.ClipSiblings And WindowStyle.ClipChildren)
            End If

#If DEBUG Then
            rot = New DsROTEntry(graphBuilder)
#End If

            'Update Form Title
            Call SetFormTitle()

            'Start Media
            hr = mediaControl.Run

            currentState = PlayState.Running

            If isAudioOnly = False Then
                'Set Video Size
                hr = VideoWindowSize(1, 1)
            End If
        Catch ex As Exception
            MsgBox("Error " & ex.Message, MsgBoxStyle.Critical, "Error")
            RaiseEvent MedClose()
        End Try
    End Sub

Points of Interest

Feel free to edit and redistribute the source code.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Software Developer None
United States United States
No Biography provided

Comments and Discussions

QuestionAudio stream count PinmemberMember 1026358610-Sep-13 22:25 
Generalgood Pinmemberwoonhow28-Mar-13 1:09 
QuestionPlay Video with DirectShow without ActiveMovieWindow... only over picturebox Pinmembermorandeira24-Feb-13 22:17 
QuestionRotate the video Pinmemberpedrazasual29-May-12 6:21 
QuestionAutoLoop PinmemberNippo19-Jan-12 0:33 
AnswerRe: AutoLoop PinmemberMember 449312114-Mar-12 4:50 
QuestionOn win 7 mp4 is not supported Pinmemberridaria5-Aug-11 8:47 
AnswerRe: On win 7 mp4 is not supported Pinmember_Plutonix23-Dec-11 16:04 
QuestionRepeat a video continuosly Pinmemberridaria5-Aug-11 6:10 
QuestionHow to use IMpegAudioDecoder to change Audio Left/Right Channel in VCD Pinmemberbinary00113-Dec-10 18:12 
AnswerRe: How to use IMpegAudioDecoder to change Audio Left/Right Channel in VCD Pinmemberwoonhow18-Apr-13 19:22 
GeneralSelecting different sound card PinmemberMember 44443184-Dec-09 15:51 
GeneralRe: Selecting different sound card Pinmemberlilbiz87-Dec-09 6:48 
GeneralSelecting the sound card in Directshow for VB.Net PinmemberJ Lorkers30-Apr-09 8:18 
QuestionRe: Selecting the sound card in Directshow for VB.Net Pinmemberjohnnynine29-Sep-09 16:14 
QuestionHow can you prevent the window from resizing as you load a movie? PinmemberEyal K.8-Apr-09 6:51 
AnswerRe: How can you prevent the window from resizing as you load a movie? Pinmemberlilbiz86-Dec-09 13:47 
GeneralStill images from running video PinmemberMember 306612830-Dec-08 5:12 
GeneralRe: Still images from running video Pinmemberlilbiz88-Jan-09 19:26 
GeneralRe: Still images from running video Pinmemberologbozigee23-Jul-11 22:18 

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
Web03 | 2.8.150414.1 | Last Updated 24 Dec 2008
Article Copyright 2008 by lilbiz8
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid