How do I get a camera frame and show a preview in WPF of.NET 6.0

华 张 156 Reputation points
2023-11-02T02:02:17.75+00:00

I have a WPF project that uses.NET 6.0. Now I want to get a preview of the camera, using the MediaCapture class, but there is no CaptureElement in WPF, so I can't show the preview. So I want to get the frame and draw it on the image myself, is there any way to do that

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,099 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 47,341 Reputation points Microsoft Vendor
    2023-11-02T07:00:56.3633333+00:00

    Hi, 华 张. Welcome to Microsoft Q&A Forum.

    You could use MediaCapture API in a WPF application. To do so you need to setup your project to target the right windows versions:

    • For .NET 6, you can set the Target OS in properties to 10.0.17763.0 or above (or set the TargetFramrwork in project file to net6.0-windows10.0.17763.0)
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
    	  <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
    	  <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
    
    

    To learn more, you can take a look at Call Windows Runtime APIs in desktop apps.

    For a WPF project sample, you could try checking out MediaCaptureApiWPF .


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.