Ok, Here is my process... this will probably help others who are like me trying to do this.

I wanted to build an application that allowed users to connect to thier webcam show a preview of what they are doing in a PictureBox and then allow the user to save a capture of that video to file.

Only problem was.... the file was too big.

I needed to access this information and pass it to the internet so I needed the files to be smaller, instead of AVI i needed WMV. So i researched codecs and Encoding and came up with a solution using a code i found for a screengrab program. The filesize was WMV and that was perfect.

Only problem was.... it captured and encoded the screengrab, and not the webcam

So i did some more research and basically tried to combine the two codes, I did, i managed to clean up the two codes so they were reading properly one was pointing to the picturebox with a preview and the other was making a file that was a WMV, YES!!!... only... not...

Since i set the code up as two different controls for one device... .now i can call the Webcam to the picture window to preview it.... but i cannot capture with the saveas function, because the preview control is still using the webcam...... SOOOOO.... after all of that i ask for your help. Can someone look at this code and see where i went wrong. Im thinking that i need to call the same "Source" to the PictureBox as the capture, but im not sure.

Here is my code, any help is more than appreciated.

VBCode Code:
  1. Imports WMEncoderLib
  2. Imports WMPREVIEWLib
  3. Imports System.IO
  4. Imports System.Runtime.InteropServices
  5.  
  6.  
  7.  
  8. Public Class Form1
  9.  
  10.     Inherits System.Windows.Forms.Form
  11.  
  12.     Dim Encoder As WMEncoder
  13.  
  14.     Const WM_CAP As Short = &H400S
  15.  
  16.     Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
  17.     Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
  18.     Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
  19.  
  20.     Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
  21.     Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
  22.     Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
  23.     Const WS_CHILD As Integer = &H40000000
  24.     Const WS_VISIBLE As Integer = &H10000000
  25.     Const SWP_NOMOVE As Short = &H2S
  26.     Const SWP_NOSIZE As Short = 1
  27.     Const SWP_NOZORDER As Short = &H4S
  28.     Const HWND_BOTTOM As Short = 1
  29.  
  30.     Dim iDevice As Integer = 0 ' Current device ID
  31.     Dim hHwnd As Integer ' Handle to preview window
  32.  
  33.     Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  34.         (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
  35.         <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
  36.  
  37.     Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
  38.         ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
  39.         ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
  40.  
  41.     Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
  42.  
  43.     Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
  44.         (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
  45.         ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
  46.         ByVal nHeight As Short, ByVal hWndParent As Integer, _
  47.         ByVal nID As Integer) As Integer
  48.  
  49.     Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, _
  50.         ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, _
  51.         ByVal cbVer As Integer) As Boolean
  52.    
  53.    
  54.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  55.         'load listbox
  56.  
  57.         LoadDeviceList()
  58.         If lstDevices.Items.Count > 0 Then
  59.             btnStart.Enabled = True
  60.             lstDevices.SelectedIndex = 0
  61.             btnStart.Enabled = True
  62.         Else
  63.             lstDevices.Items.Add("No Capture Device")
  64.             btnStart.Enabled = False
  65.         End If
  66.  
  67.         btnStart.Enabled = True
  68.         btnSave.Enabled = False
  69.         picCapture.SizeMode = PictureBoxSizeMode.StretchImage
  70.  
  71.  
  72.         ' Create a WMEncoder object.
  73.  
  74.         Encoder = New WMEncoder
  75.         ' Retrieve the source group collection and add a source group.
  76.  
  77.         Dim SrcGrp As IWMEncSourceGroup2
  78.         Dim SrcGrpColl As IWMEncSourceGroupCollection
  79.         SrcGrpColl = Encoder.SourceGroupCollection
  80.         SrcGrp = SrcGrpColl.Add("SG_1")
  81.  
  82.         ' Add a video and audio source to the source group.
  83.         Dim SrcVid As IWMEncVideoSource2
  84.         Dim SrcAud As IWMEncAudioSource
  85.         SrcVid = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO)
  86.         SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO)
  87.  
  88.         ' Identify the source files to encode.
  89.         SrcVid.SetInput("Default_Video_Device", "Device", "")
  90.         SrcAud.SetInput("Device://Default_Audio_Device")
  91.  
  92.         ' Choose a profile from the collection.
  93.         Dim ProColl As IWMEncProfileCollection
  94.         Dim Pro As IWMEncProfile
  95.         Dim i As Integer
  96.         Dim lLength As Long
  97.  
  98.         ProColl = Encoder.ProfileCollection
  99.         lLength = ProColl.Count
  100.         'For i = 0 To lLength - 1
  101.         '    Console.WriteLine(ProColl.Item(i).Name)
  102.         'Next
  103.         For i = 0 To lLength - 1
  104.             Pro = ProColl.Item(i)
  105.             If Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)" Then
  106.                 SrcGrp.Profile = Pro
  107.                 Exit For
  108.             End If
  109.         Next
  110.  
  111.         ' Fill in the description object members.
  112.         Dim Descr As IWMEncDisplayInfo
  113.         Descr = Encoder.DisplayInfo
  114.         Descr.Author = "Armoghan Asif"
  115.         Descr.Copyright = "Copyright information"
  116.         Descr.Description = "Text description of encoded content"
  117.         Descr.Rating = "Rating information"
  118.         Descr.Title = "Title of encoded content"
  119.  
  120.         ' Add an attribute to the collection.
  121.         Dim Attr As IWMEncAttributes
  122.         Attr = Encoder.Attributes
  123.         Attr.Add("URL", "www.EpicRI.com")
  124.  
  125.         ' Specify a file object in which to save encoded content.
  126.         Dim File As IWMEncFile
  127.         File = Encoder.File
  128.         File.LocalFileName = "C:\OutputFile.wmv"
  129.  
  130.         ' Crop 2 pixels from each edge of the video image.
  131.         SrcVid.CroppingBottomMargin = 2
  132.         SrcVid.CroppingTopMargin = 2
  133.         SrcVid.CroppingLeftMargin = 2
  134.         SrcVid.CroppingRightMargin = 2
  135.  
  136.     End Sub
  137.     Private Sub LoadDeviceList()
  138.         Dim strName As String = Space(100)
  139.         Dim strVer As String = Space(100)
  140.         Dim bReturn As Boolean
  141.         Dim x As Integer = 0
  142.  
  143.         '
  144.         ' Load name of all avialable devices into the lstDevices
  145.         '
  146.  
  147.         Do
  148.             '
  149.             '   Get Driver name and version
  150.             '
  151.             bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
  152.  
  153.             '
  154.             ' If there was a device add device name to the list
  155.             '
  156.             If bReturn Then lstDevices.Items.Add(strName.Trim)
  157.             x += 1
  158.         Loop Until bReturn = False
  159.     End Sub
  160.     Private Sub OpenPreviewWindow()
  161.         Dim iHeight As Integer = picCapture.Height
  162.         Dim iWidth As Integer = picCapture.Width
  163.  
  164.         '
  165.         ' Open Preview window in picturebox
  166.         '
  167.         hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
  168.             480, picCapture.Handle.ToInt32, 0)
  169.  
  170.         '
  171.         ' Connect to device
  172.         '
  173.         If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
  174.             '
  175.             'Set the preview scale
  176.             '
  177.             SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
  178.  
  179.             '
  180.             'Set the preview rate in milliseconds
  181.             '
  182.             SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
  183.  
  184.             '
  185.             'Start previewing the image from the camera
  186.             '
  187.             SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
  188.  
  189.             '
  190.             ' Resize window to fit in picturebox
  191.             '
  192.             SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
  193.                     SWP_NOMOVE Or SWP_NOZORDER)
  194.  
  195.             btnSave.Enabled = True
  196.             btnStop.Enabled = True
  197.             btnStart.Enabled = False
  198.         Else
  199.             '
  200.             ' Error connecting to device close window
  201.             '
  202.             DestroyWindow(hHwnd)
  203.  
  204.             btnSave.Enabled = False
  205.         End If
  206.     End Sub
  207.  
  208.  
  209.  
  210.  
  211.  
  212.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopRecord.Click
  213.         If Encoder.RunState Then
  214.             Encoder.Stop()
  215.         End If
  216.     End Sub
  217.  
  218.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartRecord.Click
  219.  
  220.         ' Start the encoding process.
  221.         Encoder.Start()
  222.  
  223.     End Sub
  224.  
  225.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
  226.         Close()
  227.     End Sub
  228.  
  229.     Private Sub btnStopCamera_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
  230.         iDevice = lstDevices.SelectedIndex
  231.         OpenPreviewWindow()
  232.     End Sub
  233.     Private Sub ClosePreviewWindow()
  234.         '
  235.         ' Disconnect from device
  236.         '
  237.         SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
  238.  
  239.         '
  240.         ' close window
  241.         '
  242.  
  243.         DestroyWindow(hHwnd)
  244.     End Sub
  245.  
  246.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
  247.         ClosePreviewWindow()
  248.         btnSave.Enabled = False
  249.         btnStart.Enabled = True
  250.         btnStop.Enabled = False
  251.     End Sub