Link to home
Start Free TrialLog in
Avatar of jaytechnology
jaytechnologyFlag for Afghanistan

asked on

Passing combobox value to another form on open

I am running windows 7 32 bit, i have a form that has a dropdownlist that has a value.  I want to click on open form button and pass all the combo box variables to the other form. VS2010
Avatar of kaufmed
kaufmed
Flag of United States of America image

Avatar of jaytechnology

ASKER

There has to be an easier way that actually spells it out.  I am an asp.net programmer and all the stuff from that link doesnt spell it out.

Hmmm....  I thought I spelled it out in great detail when I wrote that...  *sigh*  It seems you want (need) a specific example. So be it:

Public Class Form1
    Inherits Form

    Public Sub btnOpenForm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOpenForm.Click
        Dim f2 As New Form2()
        Dim items As New List(Of String)

        For Each item In ComboBox1.Items
            items.Add(item.ToString())
        Next

        f2.DisplayPassedItems(items)

    End Sub
End Class

Public Class Form2
    Inherits Form

    Private passedItems As List(Of String)

    Public Sub DisplayPassedItems(ByVal items As List(Of String))
        For Each item In items
            MessageBox.Show(item)
        Next
    End Sub
End Class

Open in new window


This is an extremely crude example, and as I pointed out in my article, there are multiple ways to pass data between forms. Just make sure you have valid references within the same scope and you can pass the data in the manner you see fit.
You can ignore line 20...  I was going to use that, but decided to take a different approach. I simply forgot to remove it.
OK.  I appreciate all the help so far but maybe I am going about this the wrong way.  Let me explain what I got and what I am trying to do.  I will post the code.
I have Form1 that does a capture of every usb camera that I have connected, and the dropdown list lets me select beteween them.  I want to move the controls of the swithcing on to another form rather than form1, form2.  So have the dropdown list on form2 with the list of cameras and when changed they change on form 1.  Here is the code and a screenshot.

Thanks for sticking with me so far.
Form1
Imports System.Runtime.InteropServices, System.IO, DShowNET, DShowNET.Device, System.Drawing, System.Drawing.Imaging, System.Collections, System.ComponentModel, System.Diagnostics
Imports System.Data, DirectShowLib
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Menu

Public Class FrmCam
    Implements DShowNET.ISampleGrabberCB
    <DllImport("oleaut32.dll")> _
    Public Shared Function OleCreatePropertyFrame(ByVal hwndOwner As IntPtr, ByVal x As Integer, ByVal y As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszCaption As String, ByVal cObjects As Integer, <MarshalAs(UnmanagedType.[Interface], ArraySubType:=UnmanagedType.IUnknown)> ByRef ppUnk As Object, _
      ByVal cPages As Integer, ByVal lpPageClsID As IntPtr, ByVal lcid As Integer, ByVal dwReserved As Integer, ByVal lpvReserved As IntPtr) As Integer
    End Function
    Private firstActive As Boolean
    ''' <summary> base filter of the actually used video devices. </summary>
    Private capFilter As DShowNET.IBaseFilter
    ''' <summary> graph builder interface. </summary>
    Private graphBuilder As DShowNET.IGraphBuilder
    ''' <summary> capture graph builder interface. </summary>
    Private capGraph As DShowNET.ICaptureGraphBuilder2
    Private sampGrabber As DShowNET.ISampleGrabber
    ''' <summary> control interface. </summary>
    Private mediaCtrl As DShowNET.IMediaControl
    ''' <summary> event interface. </summary>
    Private mediaEvt As DShowNET.IMediaEventEx
    ''' <summary> video window interface. </summary>
    Private videoWin As DShowNET.IVideoWindow
    ''' <summary> grabber filter interface. </summary>
    Private baseGrabFlt As DShowNET.IBaseFilter
    ''' <summary> structure describing the bitmap to grab. </summary>
    Private videoInfoHeader As DShowNET.VideoInfoHeader
    Private captured As Boolean = True
    Private bufferedSize As Integer
    ''' <summary> buffer for bitmap data. </summary>
    Private savedArray As Byte()
    ''' <summary> list of installed video devices. </summary>
    Private capDevices As ArrayList
    Private Const WM_GRAPHNOTIFY As Integer = &H8001
    ' message from graph
    Private Const WS_CHILD As Integer = &H40000000
    ' attributes for video window
    Private Const WS_CLIPCHILDREN As Integer = &H2000000
    Private Const WS_CLIPSIBLINGS As Integer = &H4000000
    ''' <summary> event when callback has finished (ISampleGrabberCB.BufferCB). </summary>
    Private Delegate Sub CaptureDone()
    Private rotCookie As Integer = 0
    Private mediaControl As DirectShowLib.IMediaControl = Nothing
    Private theDevice As DirectShowLib.IBaseFilter = Nothing
    Private m_rot As DsROTEntry = Nothing
    Dim CurrPnl As String
    Friend Enum PlayState
        Init
        Stopped
        Paused
        Running
    End Enum
    Private Sub FrmCam_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        CloseInterfaces(True)
    End Sub

    Private Sub FrmCam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'for the panel movment only
        'Me.Panel3.Location = New System.Drawing.Point(902, 583)
        'check if directX installed or not and is there any capture devices
        Check()
        'filling the combobox with devices names
        If capDevices.Count <> 0 Then
            For Each d As DShowNET.Device.DsDevice In capDevices
                ComboBox1.Items.Add(d.Name)
                ComboBox1.SelectedIndex = 0
            Next
        End If

    End Sub
    Private Sub Check()
        If firstActive Then
            Exit Sub
        End If
        firstActive = True

        If Not DShowNET.DsUtils.IsCorrectDirectXVersion() Then
            MessageBox.Show(Me, "DirectX 8.1 NOT installed!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Me.Close()
            Exit Sub
        End If

        If Not DsDev.GetDevicesOfCat(DShowNET.FilterCategory.VideoInputDevice, capDevices) Then
            MessageBox.Show(Me, "No video capture devices found!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Me.Close()
            Exit Sub
        End If
    End Sub

    Private Sub Panel5_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel5.Resize
        ResizeVideoWindow()
    End Sub

    Private Sub OnCaptureDone()
        Trace.WriteLine("!!DLG: OnCaptureDone")
        Try
            Button1.Enabled = True
            Dim hr As Integer
            If sampGrabber Is Nothing Then
                Exit Sub
            End If
            hr = sampGrabber.SetCallback(Nothing, 0)

            Dim w As Integer = videoInfoHeader.BmiHeader.Width
            Dim h As Integer = videoInfoHeader.BmiHeader.Height
            If ((w And &H3) <> 0) OrElse (w < 32) OrElse (w > 4096) OrElse (h < 32) OrElse (h > 4096) Then
                Exit Sub
            End If
            Dim stride As Integer = w * 3

            Dim handle As GCHandle = GCHandle.Alloc(savedArray, GCHandleType.Pinned)
            Dim scan0 As Integer = CInt(handle.AddrOfPinnedObject())
            scan0 += (h - 1) * stride
            Dim b As New Bitmap(w, h, -stride, PixelFormat.Format24bppRgb, DirectCast(CType(scan0, IntPtr), IntPtr))
            handle.Free()
            savedArray = Nothing
            Dim old As Image = PictureBox.Image
            PictureBox.Image = b
            If old IsNot Nothing Then
                old.Dispose()
            End If
            Button1.Enabled = True
        Catch ee As Exception
            MessageBox.Show(Me, "Could not grab picture" & vbCr & vbLf & ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
        End Try
    End Sub



    Private Function StartupVideo(ByVal mon As UCOMIMoniker) As Boolean
        Dim hr As Integer
        Try
            If Not CreateCaptureDevice(mon) Then
                Return False
            End If

            If Not GetInterfaces() Then
                Return False
            End If

            If Not SetupGraph() Then
                Return False
            End If

            If Not SetupVideoWindow() Then
                Return False
            End If

            DsROT.AddGraphToRot(graphBuilder, rotCookie)
            ' graphBuilder capGraph
            hr = mediaCtrl.Run()
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            Return True
        Catch ee As Exception
            MessageBox.Show(Me, "Could not start video stream" & vbCr & vbLf & ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End Try
    End Function


    Private Function SetupVideoWindow() As Boolean
        Dim hr As Integer
        Try
            ' Set the video window to be a child of the main window
            hr = videoWin.put_Owner(Panel5.Handle)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            ' Set video window style
            hr = videoWin.put_WindowStyle(WS_CHILD Or WS_CLIPCHILDREN)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            ' Use helper function to position video window in client rect of owner window
            ResizeVideoWindow()

            ' Make the video window visible, now that it is properly positioned
            hr = videoWin.put_Visible(DsHlp.OATRUE)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            hr = mediaEvt.SetNotifyWindow(Me.Handle, WM_GRAPHNOTIFY, IntPtr.Zero)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If
            Return True
        Catch ee As Exception
            MessageBox.Show(Me, "Could not setup video window" & vbCr & vbLf & ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End Try
    End Function


    Private Function SetupGraph() As Boolean
        Dim hr As Integer
        Try
            hr = capGraph.SetFiltergraph(graphBuilder)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device")
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            'DShowNET.DsUtils.ShowCapPinDialog(capGraph, capFilter, Me.Handle)

            Dim media As New DShowNET.AMMediaType()
            media.majorType = DShowNET.MediaType.Video
            media.subType = DShowNET.MediaSubType.RGB24
            media.formatType = DShowNET.FormatType.VideoInfo
            ' ???
            hr = sampGrabber.SetMediaType(media)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber")
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            Dim cat As Guid = DShowNET.PinCategory.Preview
            Dim med As Guid = DShowNET.MediaType.Video
            hr = capGraph.RenderStream(cat, med, capFilter, Nothing, Nothing)
            ' baseGrabFlt
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            cat = DShowNET.PinCategory.Capture
            med = DShowNET.MediaType.Video
            hr = capGraph.RenderStream(cat, med, capFilter, Nothing, baseGrabFlt)
            ' baseGrabFlt
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            media = New DShowNET.AMMediaType()
            hr = sampGrabber.GetConnectedMediaType(media)
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If
            If (media.formatType <> DShowNET.FormatType.VideoInfo) OrElse (media.formatPtr = IntPtr.Zero) Then
                Throw New NotSupportedException("Unknown Grabber Media Format")
            End If

            videoInfoHeader = DirectCast(Marshal.PtrToStructure(media.formatPtr, GetType(DShowNET.VideoInfoHeader)), DShowNET.VideoInfoHeader)
            Marshal.FreeCoTaskMem(media.formatPtr)
            media.formatPtr = IntPtr.Zero

            hr = sampGrabber.SetBufferSamples(False)
            If hr = 0 Then
                hr = sampGrabber.SetOneShot(False)
            End If
            If hr = 0 Then
                hr = sampGrabber.SetCallback(Nothing, 0)
            End If
            If hr < 0 Then
                Marshal.ThrowExceptionForHR(hr)
            End If

            Return True
        Catch ee As Exception
            MessageBox.Show(Me, "Could not setup graph" & vbCr & vbLf & ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End Try
    End Function

    Private Function GetInterfaces() As Boolean
        Dim comType As Type = Nothing
        Dim comObj As Object = Nothing
        Try
            comType = Type.GetTypeFromCLSID(Clsid.FilterGraph)
            If comType Is Nothing Then
                Throw New NotImplementedException("DirectShow FilterGraph not installed/registered!")
            End If
            comObj = Activator.CreateInstance(comType)
            graphBuilder = DirectCast(comObj, DShowNET.IGraphBuilder)
            comObj = Nothing

            Dim clsid__1 As Guid = Clsid.CaptureGraphBuilder2
            Dim riid As Guid = GetType(DShowNET.ICaptureGraphBuilder2).GUID
            comObj = DsBugWO.CreateDsInstance(clsid__1, riid)
            capGraph = DirectCast(comObj, DShowNET.ICaptureGraphBuilder2)
            comObj = Nothing

            comType = Type.GetTypeFromCLSID(Clsid.SampleGrabber)
            If comType Is Nothing Then
                Throw New NotImplementedException("DirectShow SampleGrabber not installed/registered!")
            End If
            comObj = Activator.CreateInstance(comType)
            sampGrabber = DirectCast(comObj, DShowNET.ISampleGrabber)
            comObj = Nothing

            mediaCtrl = DirectCast(graphBuilder, DShowNET.IMediaControl)
            videoWin = DirectCast(graphBuilder, DShowNET.IVideoWindow)
            mediaEvt = DirectCast(graphBuilder, DShowNET.IMediaEventEx)
            baseGrabFlt = DirectCast(sampGrabber, DShowNET.IBaseFilter)
            Return True
        Catch ee As Exception
            MessageBox.Show(Me, "Could not get interfaces" & vbCr & vbLf & ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        Finally
            If comObj IsNot Nothing Then
                Marshal.ReleaseComObject(comObj)
            End If
            comObj = Nothing
        End Try
    End Function

    Private Function CreateCaptureDevice(ByVal mon As UCOMIMoniker) As Boolean
        Dim capObj As Object = Nothing
        Try
            Dim gbf As Guid = GetType(DShowNET.IBaseFilter).GUID
            mon.BindToObject(Nothing, Nothing, gbf, capObj)
            capFilter = DirectCast(capObj, DShowNET.IBaseFilter)
            capObj = Nothing
            Return True
        Catch ee As Exception
            MessageBox.Show(Me, "Could not create capture device" & vbCr & vbLf & ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        Finally
            If capObj IsNot Nothing Then
                Marshal.ReleaseComObject(capObj)
            End If
            capObj = Nothing

        End Try
    End Function

    Private Sub CloseInterfaces(ByVal endnow As Boolean)
        Dim hr As Integer
        Try

            If rotCookie <> 0 Then
                DsROT.RemoveGraphFromRot(rotCookie)
            End If


            If mediaCtrl IsNot Nothing Then
                hr = mediaCtrl.[Stop]()
                mediaCtrl = Nothing
            End If

            If mediaEvt IsNot Nothing Then
                hr = mediaEvt.SetNotifyWindow(IntPtr.Zero, WM_GRAPHNOTIFY, IntPtr.Zero)
                mediaEvt = Nothing
            End If

            If videoWin IsNot Nothing Then
                hr = videoWin.put_Visible(DsHlp.OAFALSE)
                hr = videoWin.put_Owner(IntPtr.Zero)
                videoWin = Nothing
            End If

            baseGrabFlt = Nothing
            If sampGrabber IsNot Nothing Then
                Marshal.ReleaseComObject(sampGrabber)
            End If
            sampGrabber = Nothing

            If capGraph IsNot Nothing Then
                Marshal.ReleaseComObject(capGraph)
            End If
            capGraph = Nothing

            If graphBuilder IsNot Nothing Then
                Marshal.ReleaseComObject(graphBuilder)
            End If
            graphBuilder = Nothing

            If capFilter IsNot Nothing Then
                Marshal.ReleaseComObject(capFilter)
            End If
            capFilter = Nothing
            If endnow = True Then
                If capDevices IsNot Nothing Then
                    For Each d As DShowNET.Device.DsDevice In capDevices
                        d.Dispose()
                    Next
                    capDevices = Nothing
                End If
            End If

        Catch generatedExceptionName As Exception
        End Try
    End Sub

    Private Sub ResizeVideoWindow()
        If videoWin IsNot Nothing Then
            Dim rc As Rectangle = VideoPanel.ClientRectangle
            videoWin.SetWindowPosition(0, 0, rc.Right, rc.Bottom)
        End If
    End Sub

    Protected Overloads Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_GRAPHNOTIFY Then
            If mediaEvt IsNot Nothing Then
                OnGraphNotify()
            End If
            Exit Sub
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub OnGraphNotify()
        Dim code As DsEvCode
        Dim p1 As Integer, p2 As Integer, hr As Integer = 0
        Do
            hr = mediaEvt.GetEvent(code, p1, p2, 0)
            If hr < 0 Then
                Exit Do
            End If
            hr = mediaEvt.FreeEventParams(code, p1, p2)
        Loop While hr = 0
    End Sub

    ''' <summary> sample callback, NOT USED. </summary>
    Private Function SampleCB(ByVal SampleTime As Double, ByVal pSample As DShowNET.IMediaSample) As Integer Implements DShowNET.ISampleGrabberCB.SampleCB
        Return 0
    End Function

    Private Function BufferCB(ByVal SampleTime As Double, ByVal pBuffer As IntPtr, ByVal BufferLen As Integer) As Integer Implements DShowNET.ISampleGrabberCB.BufferCB
        If captured OrElse (savedArray Is Nothing) Then
            Trace.WriteLine("!!CB: ISampleGrabberCB.BufferCB")
            Return 0
        End If

        captured = True
        bufferedSize = BufferLen
        Trace.WriteLine("!!CB: ISampleGrabberCB.BufferCB !GRAB! size = " & BufferLen.ToString())
        If (pBuffer <> IntPtr.Zero) AndAlso (BufferLen > 1000) AndAlso (BufferLen <= savedArray.Length) Then
            Marshal.Copy(pBuffer, savedArray, 0, BufferLen)
        Else
            Trace.WriteLine(" !!!GRAB! failed ")
        End If
        Me.BeginInvoke(New CaptureDone(AddressOf OnCaptureDone))
        Return 0
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox.Visible = True
        Trace.WriteLine("!!BTN: toolBar_ButtonClick")
        Dim hr As Integer
        If sampGrabber Is Nothing Then
            Exit Sub
        End If

        If savedArray Is Nothing Then
            Dim size As Integer = videoInfoHeader.BmiHeader.ImageSize
            If (size < 1000) OrElse (size > 16000000) Then
                Exit Sub
            End If
            savedArray = New Byte(size + 63999) {}
        End If

        Dim old As Image = PictureBox.Image

        PictureBox.Image = Nothing
        If old IsNot Nothing Then
            old.Dispose()
        End If

        'toolBarBtnGrab.Enabled = False
        captured = False
        hr = sampGrabber.SetCallback(Me, 1)
        'ElseIf e.Button = toolBarBtnSave Then
        'Dim sd As New SaveFileDialog()
        'sd.FileName = "DsNET.bmp"
        'sd.Title = "Save Image as..."
        'sd.Filter = "Bitmap file (*.bmp)|*.bmp"
        'sd.FilterIndex = 1
        'If sd.ShowDialog() <> DialogResult.OK Then
        '    Exit Sub
        'End If

        ' save to new bmp file
        'PictureBox.Image.Save(sd.FileName, ImageFormat.Bmp)
        'ElseIf e.Button = toolBarBtnTune Then
        'Me.Panel3.Location = New System.Drawing.Point(902, 583)
        Button1.Enabled = False
        TmrPnlVw.Enabled = False
        CurrPnl = "1"
        TmrPnlVw.Enabled = True

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Me.CloseInterfaces(False)
        Dim dev As DShowNET.Device.DsDevice = Nothing
        Check()
        If capDevices.Count <> 0 Then
            dev = TryCast(capDevices(ComboBox1.SelectedIndex), DShowNET.Device.DsDevice)
        End If

        If dev Is Nothing Then
            Me.Close()
            Exit Sub
        End If

        If Not StartupVideo(dev.Mon) Then
            Me.Close()
        End If
    End Sub

    Private Sub DisplayPropertyPage(ByVal dev As DirectShowLib.IBaseFilter)
        'Get the ISpecifyPropertyPages for the filter
        Dim pProp As DirectShowLib.ISpecifyPropertyPages = TryCast(dev, DirectShowLib.ISpecifyPropertyPages)
        Dim hr As Integer = 0

        If pProp Is Nothing Then
            'If the filter doesn't implement ISpecifyPropertyPages, try displaying IAMVfwCompressDialogs instead!
            Dim compressDialog As DirectShowLib.IAMVfwCompressDialogs = TryCast(dev, DirectShowLib.IAMVfwCompressDialogs)
            If compressDialog IsNot Nothing Then

                hr = compressDialog.ShowDialog(VfwCompressDialogs.Config, IntPtr.Zero)
                DsError.ThrowExceptionForHR(hr)
            End If
            Exit Sub
        End If

        'Get the name of the filter from the FilterInfo struct
        Dim filterInfo As DirectShowLib.FilterInfo
        hr = dev.QueryFilterInfo(filterInfo)
        DsError.ThrowExceptionForHR(hr)

        ' Get the propertypages from the property bag
        Dim caGUID As DirectShowLib.DsCAUUID
        hr = pProp.GetPages(caGUID)
        DsError.ThrowExceptionForHR(hr)

        ' Check for property pages on the output pin
        Dim pPin As DirectShowLib.IPin = DsFindPin.ByDirection(dev, DirectShowLib.PinDirection.Output, 0)
        Dim pProp2 As DirectShowLib.ISpecifyPropertyPages = TryCast(pPin, DirectShowLib.ISpecifyPropertyPages)
        If pProp2 IsNot Nothing Then
            Dim caGUID2 As DirectShowLib.DsCAUUID
            hr = pProp2.GetPages(caGUID2)
            DsError.ThrowExceptionForHR(hr)

            If caGUID2.cElems > 0 Then
                Dim soGuid As Integer = Marshal.SizeOf(GetType(Guid))

                ' Create a new buffer to hold all the GUIDs
                Dim p1 As IntPtr = Marshal.AllocCoTaskMem((caGUID.cElems + caGUID2.cElems) * soGuid)

                ' Copy over the pages from the Filter
                For x As Integer = 0 To caGUID.cElems * soGuid - 1
                    Marshal.WriteByte(p1, x, Marshal.ReadByte(caGUID.pElems, x))
                Next

                ' Add the pages from the pin
                For x As Integer = 0 To caGUID2.cElems * soGuid - 1
                    Marshal.WriteByte(p1, x + (caGUID.cElems * soGuid), Marshal.ReadByte(caGUID2.pElems, x))
                Next

                ' Release the old memory
                Marshal.FreeCoTaskMem(caGUID.pElems)
                Marshal.FreeCoTaskMem(caGUID2.pElems)

                ' Reset caGUID to include both
                caGUID.pElems = p1
                caGUID.cElems += caGUID2.cElems
            End If
        End If

        ' Create and display the OlePropertyFrame
        Dim oDevice As Object = DirectCast(dev, Object)
        hr = OleCreatePropertyFrame(Me.Handle, 0, 0, filterInfo.achName, 1, oDevice, _
        caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero)
        DsError.ThrowExceptionForHR(hr)

        ' Release COM objects
        Marshal.FreeCoTaskMem(caGUID.pElems)
        Marshal.ReleaseComObject(pProp)
        If filterInfo.pGraph IsNot Nothing Then
            Marshal.ReleaseComObject(filterInfo.pGraph)
        End If
    End Sub

    Private Function CreateFilter(ByVal category As Guid, ByVal friendlyname As String) As DirectShowLib.IBaseFilter
        Dim source As Object = Nothing
        Dim iid As Guid = GetType(DirectShowLib.IBaseFilter).GUID
        For Each device As DirectShowLib.DsDevice In DirectShowLib.DsDevice.GetDevicesOfCat(category)
            If device.Name.CompareTo(friendlyname) = 0 Then
                device.Mon.BindToObject(Nothing, Nothing, iid, source)
                Exit For
            End If
        Next

        Return DirectCast(source, DirectShowLib.IBaseFilter)
    End Function

    Private Sub Button12_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim devicepath As String = ComboBox1.SelectedItem.ToString()
        theDevice = CreateFilter(DirectShowLib.FilterCategory.VideoInputDevice, devicepath)
        DisplayPropertyPage(theDevice)
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        If Not Directory.Exists(Path.GetTempPath & "Capture") Then
            Directory.CreateDirectory(Path.GetTempPath & "Capture\")
        End If
        If Not PictureBox.Image Is Nothing Then
            Dim X As String = Path.GetRandomFileName
            'specify the save path
            PictureBox.Image.Save(Path.GetTempPath & "Capture\" & X & ".jpg", Imaging.ImageFormat.Jpeg)
            MessageBox.Show("Saved to: " & Path.GetTempPath & "Capture\" & X & ".jpg")
            Me.Hide()
        End If

    End Sub

Open in new window

Camera-1.PNG
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial