I have this code and would like to know how to capture a singe frame and put it in a picture box? The source of the video is a WebCam.
Any help would be appreciated.Code:Imports System Imports System.Diagnostics Imports System.Drawing Imports System.Runtime.InteropServices Imports System.Windows.Forms Imports DirectShowLib Imports System.Runtime.InteropServices.ComTypes Namespace Capture_The_Webcam Public Class Form1 Inherits System.Windows.Forms.Form Enum PlayState Stopped Paused Running Init End Enum Dim CurrentState As PlayState = PlayState.Stopped Dim D As Integer = Convert.ToInt32("0X8000", 16) Public WM_GRAPHNOTIFY As Integer = D + 1 Dim VideoWindow As IVideoWindow = Nothing Dim MediaControl As IMediaControl = Nothing Dim MediaEventEx As IMediaEventEx = Nothing Dim GraphBuilder As IGraphBuilder = Nothing Dim CaptureGraphBuilder As ICaptureGraphBuilder2 = Nothing Dim rot As DsROTEntry = Nothing <STAThread()> Shared Sub Main() Application.Run(New Form1) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Form2.Show() InitializeComponent() CaptureVideo() End Sub Private Sub InitializeComponent() Me.SuspendLayout() ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(304, 282) Me.Name = "Form1" Me.Text = "Video Capture Previewer (PlayCap)" Me.ResumeLayout(False) End Sub Private Sub CaptureVideo() Dim hr As Integer = 0 Dim sourceFilter As IBaseFilter = Nothing Try GetInterfaces() hr = Me.CaptureGraphBuilder.SetFiltergraph(Me.GraphBuilder) 'Specifies filter graph "graphbuilder" for the capture graph builder "captureGraphBuilder" to use. Debug.WriteLine("Attach the filter graph to the capture graph : " & DsError.GetErrorText(hr)) DsError.ThrowExceptionForHR(hr) sourceFilter = FindCaptureDevice() hr = Me.GraphBuilder.AddFilter(sourceFilter, "Video Capture") Debug.WriteLine("Add capture filter to our graph : " & DsError.GetErrorText(hr)) DsError.ThrowExceptionForHR(hr) hr = Me.CaptureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, Nothing, Nothing) Debug.WriteLine("Render the preview pin on the video capture filter : " & DsError.GetErrorText(hr)) DsError.ThrowExceptionForHR(hr) Marshal.ReleaseComObject(sourceFilter) SetupVideoWindow() rot = New DsROTEntry(Me.GraphBuilder) Dim hrr As Integer hr = Me.MediaControl.Run() Debug.WriteLine("Start previewing video data : " & DsError.GetErrorText(hr)) DsError.ThrowExceptionForHR(hr) Me.CurrentState = PlayState.Running Debug.WriteLine("The currentstate : " & Me.CurrentState.ToString) Catch ex As Exception MessageBox.Show("An unrecoverable error has occurred.With error : " & ex.ToString) End Try End Sub Private Sub GetInterfaces() Dim hr As Integer = 0 Me.GraphBuilder = CType(New FilterGraph, IGraphBuilder) Me.CaptureGraphBuilder = CType(New CaptureGraphBuilder2, ICaptureGraphBuilder2) Me.MediaControl = CType(Me.GraphBuilder, IMediaControl) Me.VideoWindow = CType(Me.GraphBuilder, IVideoWindow) Me.MediaEventEx = CType(Me.GraphBuilder, IMediaEventEx) hr = Me.MediaEventEx.SetNotifyWindow(Me.Handle, WM_GRAPHNOTIFY, IntPtr.Zero) 'This method designates a window as the recipient of messages generated by or sent to the current DirectShow object DsError.ThrowExceptionForHR(hr) 'ThrowExceptionForHR is a wrapper for Marshal.ThrowExceptionForHR, but additionally provides descriptions for any DirectShow specific error messages.If the hr value is not a fatal error, no exception will be thrown: Debug.WriteLine("I started Sub Get interfaces , the result is : " & DsError.GetErrorText(hr)) End Sub Public Function FindCaptureDevice() As IBaseFilter Debug.WriteLine("Start the Sub FindCaptureDevice") Dim hr As Integer = 0 Dim classEnum As IEnumMoniker = Nothing Dim moniker As IMoniker() = New IMoniker(0) {} Dim source As Object = Nothing Dim devEnum As ICreateDevEnum = CType(New CreateDevEnum, ICreateDevEnum) hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, classEnum, 0) Debug.WriteLine("Create an enumerator for the video capture devices : " & DsError.GetErrorText(hr)) DsError.ThrowExceptionForHR(hr) Marshal.ReleaseComObject(devEnum) If classEnum Is Nothing Then Throw New ApplicationException("No video capture device was detected.\r\n\r\n" & _ "This sample requires a video capture device, such as a USB WebCam,\r\n" & _ "to be installed and working properly. The sample will now close.") End If If classEnum.Next(moniker.Length, moniker, IntPtr.Zero) = 0 Then Dim iid As Guid = GetType(IBaseFilter).GUID moniker(0).BindToObject(Nothing, Nothing, iid, source) Else Throw New ApplicationException("Unable to access video capture device!") End If Marshal.ReleaseComObject(moniker(0)) Marshal.ReleaseComObject(classEnum) Return CType(source, IBaseFilter) End Function Public Sub SetupVideoWindow() Dim hr As Integer = 0 hr = Me.VideoWindow.put_Owner(Me.Handle) DsError.ThrowExceptionForHR(hr) hr = Me.VideoWindow.put_WindowStyle(WindowStyle.Child Or WindowStyle.ClipChildren) DsError.ThrowExceptionForHR(hr) ResizeVideoWindow() hr = Me.VideoWindow.put_Visible(OABool.True) DsError.ThrowExceptionForHR(hr) End Sub Protected Overloads Sub WndProc(ByRef m As Message) Select Case m.Msg Case WM_GRAPHNOTIFY HandleGraphEvent() End Select If Not (Me.VideoWindow Is Nothing) Then Me.VideoWindow.NotifyOwnerMessage(m.HWnd, m.Msg, m.WParam.ToInt32, m.LParam.ToInt32) End If MyBase.WndProc(m) End Sub Public Sub HandleGraphEvent() Dim hr As Integer = 0 Dim evCode As EventCode Dim evParam1 As Integer Dim evParam2 As Integer If Me.MediaEventEx Is Nothing Then Return End If While Me.MediaEventEx.GetEvent(evCode, evParam1, evParam2, 0) = 0 hr = Me.MediaEventEx.FreeEventParams(evCode, evParam1, evParam2) DsError.ThrowExceptionForHR(hr) End While End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then closeinterfaces() End If MyBase.Dispose(disposing) End Sub Public Sub closeinterfaces() If Not (Me.MediaControl Is Nothing) Then Me.MediaControl.StopWhenReady() End If Me.CurrentState = PlayState.Stopped If Not (Me.MediaEventEx Is Nothing) Then Me.MediaEventEx.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero) End If If Not (Me.VideoWindow Is Nothing) Then Me.VideoWindow.put_Visible(OABool.False) Me.VideoWindow.put_Owner(IntPtr.Zero) End If If Not (rot Is Nothing) Then rot.Dispose() rot = Nothing End If Marshal.ReleaseComObject(Me.MediaControl) : Me.MediaControl = Nothing Marshal.ReleaseComObject(Me.MediaEventEx) : Me.MediaEventEx = Nothing Marshal.ReleaseComObject(Me.VideoWindow) : Me.VideoWindow = Nothing Marshal.ReleaseComObject(Me.GraphBuilder) : Me.GraphBuilder = Nothing Marshal.ReleaseComObject(Me.CaptureGraphBuilder) : Me.CaptureGraphBuilder = Nothing End Sub Private Sub Form1_Resize1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize If Me.WindowState = FormWindowState.Minimized Then ChangePreviewState(False) End If If Me.WindowState = FormWindowState.Normal Then ChangePreviewState(True) End If ResizeVideoWindow() End Sub Public Sub ChangePreviewState(ByVal showVideo As Boolean) Dim hr As Integer = 0 If Me.MediaControl Is Nothing Then Debug.WriteLine("MediaControl is nothing") Return End If If showVideo = True Then If Not (Me.CurrentState = PlayState.Running) Then Debug.WriteLine("Start previewing video data") hr = Me.MediaControl.Run Me.CurrentState = PlayState.Running End If Else Debug.WriteLine("Stop previewing video data") hr = Me.MediaControl.StopWhenReady Me.CurrentState = PlayState.Stopped End If End Sub Public Sub ResizeVideoWindow() If Not (Me.VideoWindow Is Nothing) Then Me.VideoWindow.SetWindowPosition(0, 0, Me.Width, Me.ClientSize.Height) End If End Sub End Class End Namespace
Last edited by duvix; 03-13-2010 at 12:56 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks