如何从其他应用程序打开的网络摄像头获取帧或使用一个网络摄像头共享的多应用程序共享

Hui Liu-MSFT 48,711 信誉分 Microsoft 外部员工
2024-05-31T09:11:46.5166667+00:00

我的APP想让网络摄像头帧在使用其他应用程序时保存为位图

Note:此问题总结整理于:How to get the frame from webcam that other application opened or multi app share use the one webcam

开发人员技术 | Windows Presentation Foundation
0 个注释 无注释
{count} 票

问题作者接受的答案
  1. 匿名
    2024-05-31T09:28:02.5133333+00:00

    如何使用 MediaCapture 类从网络摄像头捕获图像?若要使用有关 MediaCapture 的相关 API,需要添加到 WPf 项目中。使用 Canvas 中包含的 Image 控件来显示捕获。然后,您可以将画布另存为 png 文件。以下是我实现此目的的步骤:Microsoft.Windows.SDK.Contracts

    **步骤1:**将 和 添加到项目的 Nuget 包管理器Microsoft.Windows.SDK.ContractsMMaitre.MediaCaptureWPF

    **步骤2:**xaml 代码为:

     <StackPanel>  
            <Canvas Name="MyCanvas" Width="200" Height="200">  
                <Image Name="webCamImage" Width="200" Height="200" ></Image>  
            </Canvas>  
            <Button Width="120" Height="38" Content="Capture" Click="CapturePhoto_Click" ></Button>  
            <Button Width="120" Height="38" Content="Save" Click="Save_Click" ></Button>  
        </StackPanel>  
    

    **步骤3:**Cs 代码为:

      async private void CapturePhoto_Click(object sender, RoutedEventArgs e)  
            {  
                captureManager = new MediaCapture();  
                await captureManager.InitializeAsync();  
      
                var preview = new CapturePreview(captureManager);  
                this.webCamImage.Source = preview;  
                await preview.StartAsync();  
            }  
      
            private void Save_Click(object sender, RoutedEventArgs e)  
            {  
                var d = new DataObject(DataFormats.Bitmap, webCamImage.Source, true);  
                var bmp = d.GetData("System.Drawing.Bitmap") as System.Drawing.Bitmap;  
                bmp.Save("e://res.bmp");  
            }  
    

    MediaCapture 可能无法满足您的要求。如果您不想使用第三方工具,也许您可以尝试DirectShow,在此答案中声明可能会给您一些帮助。


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

提问者可以将答案标记为“已接受”,审查方可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。