Results 1 to 9 of 9

Thread: Webcam Access

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2025
    Posts
    38

    Webcam Access

    VFW is old school and will not handle "modern" webcam resolution above 640 or access due to compression methods. Is there sample code to work with these newer Win and Webcams? Sample code for Quartz DirectShow or other available?

  2. #2
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,629

    Cool Re: Webcam Access

    You could use the CameraCaptureUI Class.

    The Windows.Media.Capture.CameraCaptureUI class provides a built-in, full-window camera user interface for Windows applications. It allows developers to quickly capture photos or videos without having to design custom UI elements or manage low-level camera hardware.
    Since it's a WinRT class, it requires the WinRT TypeLib, everything should be pretty straightforward.

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,658

    Re: Webcam Access

    There's Quartz-based samples if you search the forum but that's nearly as old as VFW. Media Foundation would be the option if you didn't want to use RT. -Franky- made a sample if you don't mind DispCallFunc hell, or just tell an LLM to make a sample and it should only need minor changes to work with oleexp.tlb (or WDL in twinBASIC).

  4. #4
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,629

    Cool Re: Webcam Access

    I've just included a Camera Capture project in the WinRT Sample Collection linked in the same thread. This project shows a live video preview from your webcam in a PictureBox. A ComboBox lists all available resolutions and video encoding properties supported by your camera and you can switch between them. The aptly named Snapshot button saves a JPG snapshot in the current folder.

    Name:  CameraCapture.jpg
Views: 50
Size:  34.6 KB

    The form is resizable and the live preview will be automatically resized accordingly. My Windows tablet has a crappy camera so that's what you see in the screenshot above. Professional webcams should support more advanced formats and video compression properties.

    This is all it takes to initialize the live camera preview in Form_Load. The WinRT TypeLib makes everything very straight forward using the friendly and familiar VB6 syntax you are already accustomed with:

    Code:
    Private Sub Form_Load()
    Dim DesktopWindowXamlSourceNative As IDesktopWindowXamlSourceNative, CaptureElement As ICaptureElement, MediaCaptureInitializationSettings As IMediaCaptureInitializationSettings, _
        MediaCaptureVideoPreview As IMediaCaptureVideoPreview
        Set m_DesktopWindowXamlSource = NewObject("DesktopWindowXamlSource"): Set DesktopWindowXamlSourceNative = m_DesktopWindowXamlSource: Set m_Await = New cAwait
        With DesktopWindowXamlSourceNative
            If .AttachToWindow(picCapture.hWnd) = S_OK Then
                m_hWndIsland = .WindowHandle
                Set CaptureElement = NewObject("CaptureElement"): Set m_DesktopWindowXamlSource.Content = CaptureElement
                Set m_MediaCapture = NewObject("MediaCapture"): Set MediaCaptureInitializationSettings = NewObject("MediaCaptureInitializationSettings")
                MediaCaptureInitializationSettings.StreamingCaptureMode = StreamingCaptureMode_Video: MediaCaptureInitializationSettings.PhotoCaptureSource = PhotoCaptureSource_VideoPreview
                With m_Await
                    If .Await(m_MediaCapture.InitializeWithSettingsAsync(MediaCaptureInitializationSettings)) = AsyncStatus_Completed Then
                        Set CaptureElement.Source = m_MediaCapture: CaptureElement.Stretch = Stretch_UniformToFill: Set MediaCaptureVideoPreview = m_MediaCapture
                        If .Await(MediaCaptureVideoPreview.StartPreviewAsync) = AsyncStatus_Completed Then
                            Set m_ImageEncodingProperties = ImageEncodingPropertiesStatics.CreateJpeg: EnumerateSupportedResolutions m_MediaCapture.VideoDeviceController
                        End If
                    Else
                        MsgBox Hex$(.ErrorCode) & vbNewLine & .ErrorDescription, vbOKOnly + vbExclamation, App.Title
                    End If
                End With
            End If
        End With
    End Sub

  5. #5
    Hyperactive Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    481

    Re: Webcam Access

    I once wrote an example using Media Foundation, and it was quite a complex undertaking. If I ever had to write something like that again, I would switch to WinRT because it is much easier there. The only downside is that WinRT is only available from Windows 10 onwards, whereas Media Foundation can be used for this purpose starting with Windows 7.

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,176

    Re: Webcam Access

    Quote Originally Posted by Claude Prompt
    Format this VB6 code with single statement/dim per line
    Code:
    Private Sub Form_Load()
        Dim DesktopWindowXamlSourceNative       As IDesktopWindowXamlSourceNative
        Dim CaptureElement                      As ICaptureElement
        Dim MediaCaptureInitializationSettings  As IMediaCaptureInitializationSettings
        Dim MediaCaptureVideoPreview            As IMediaCaptureVideoPreview
    
        Set m_DesktopWindowXamlSource = NewObject("DesktopWindowXamlSource")
        Set DesktopWindowXamlSourceNative = m_DesktopWindowXamlSource
        Set m_Await = New cAwait
        With DesktopWindowXamlSourceNative
            If .AttachToWindow(picCapture.hWnd) = S_OK Then
                m_hWndIsland = .WindowHandle
                Set CaptureElement = NewObject("CaptureElement")
                Set m_DesktopWindowXamlSource.Content = CaptureElement
                Set m_MediaCapture = NewObject("MediaCapture")
                Set MediaCaptureInitializationSettings = NewObject("MediaCaptureInitializationSettings")
                MediaCaptureInitializationSettings.StreamingCaptureMode = StreamingCaptureMode_Video
                MediaCaptureInitializationSettings.PhotoCaptureSource = PhotoCaptureSource_VideoPreview
                With m_Await
                    If .Await(m_MediaCapture.InitializeWithSettingsAsync(MediaCaptureInitializationSettings)) = AsyncStatus_Completed Then
                        Set CaptureElement.Source = m_MediaCapture
                        CaptureElement.Stretch = Stretch_UniformToFill
                        Set MediaCaptureVideoPreview = m_MediaCapture
                        If .Await(MediaCaptureVideoPreview.StartPreviewAsync) = AsyncStatus_Completed Then
                            Set m_ImageEncodingProperties = ImageEncodingPropertiesStatics.CreateJpeg
                            EnumerateSupportedResolutions m_MediaCapture.VideoDeviceController
                        End If
                    Else
                        MsgBox Hex$(.ErrorCode) & vbNewLine & .ErrorDescription, vbOKOnly + vbExclamation, App.Title
                    End If
                End With
            End If
        End With
    End Sub

  7. #7

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,176

    Re: Webcam Access

    This 32 LOC snippet is not pretending to be a 23 LOC snippet and my horizontal scrollbar remains untouched :-))

    I'm still strugging with these mouthful MediaCaptureInitializationSettings.StreamingCaptureMode = StreamingCaptureMode_Video assignments though. . .

    cheers,
    </wqw>

  9. #9
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,629

    Wink Re: Webcam Access

    By default, the StreamingCaptureMode includes both Video and Audio. If Microphone access happens to be disallowed in your Privacy settings, this will result in Access Denied when trying to initialize the MediaCapture object.

    The other mouthful, PhotoCaptureSource, specifies where you want to grab snapshots from. Fancier cameras have separate streams, VideoPreview stream and Photo stream (presumably higher quality).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width