You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF中如何调用设备内置摄像头拍摄高画质全像素照片

WPF调用内置摄像头采集全分辨率照片的可行方案

目前WPF下有三种成熟的方案可以实现和原生Windows相机一致的全分辨率照片采集,核心逻辑都是调用Windows Runtime提供的摄像头API,直接读取摄像头的照片流档位(和视频流是独立的输出规格,不受视频流分辨率限制),具体实现如下:

方案1:直接调用UWP CameraCaptureUI API(推荐,实现成本最低)

该方案可以直接复用UWP成熟的系统相机交互界面,不需要自定义采集UI,步骤如下:

  • 右键编辑WPF项目文件,将目标框架修改为支持WinRT API的版本,示例配置:
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    
    目标框架的Windows版本号最低可设为10.0.17763.0,适配Windows 10 1809及以上系统。
  • 直接调用CameraCaptureUI实现拍照,示例代码:
    using Windows.Media.Capture;
    using Windows.Storage;
    using System.Windows.Media.Imaging;
    
    // 拍照逻辑需在异步方法中执行
    private async void CapturePhoto()
    {
        var captureUI = new CameraCaptureUI();
        // 配置照片输出为最高可用分辨率
        captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;
        // 调起系统相机界面拍照
        var photoFile = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
        
        if (photoFile != null)
        {
            // 将照片文件转为WPF可用的BitmapImage
            var bitmap = new BitmapImage();
            using (var fileStream = await photoFile.OpenAsync(FileAccessMode.Read))
            {
                bitmap.BeginInit();
                bitmap.StreamSource = fileStream.AsStream();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.EndInit();
            }
            // 可直接赋值给WPF的Image控件展示
            // PreviewImage.Source = bitmap;
        }
    }
    
  • 未打包的WPF应用需在首次调用前请求摄像头权限,避免调用失败:
    await Windows.Devices.Enumeration.DeviceAccessInformation.CreateFromDeviceClass(Windows.Devices.Enumeration.DeviceClass.VideoCapture).RequestAccessAsync();
    

方案2:使用MediaCapture API自定义采集逻辑

如果需要自定义拍照界面,不使用系统相机UI,可以直接用MediaCapture类单独配置照片流的分辨率,和预览流规格解耦:

  • 项目配置和权限请求和方案1一致,初始化MediaCapture后单独配置照片流分辨率:
    using Windows.Media.Capture;
    using Windows.Media.MediaProperties;
    using Windows.Storage.Streams;
    
    private MediaCapture _mediaCapture;
    
    private async Task InitMediaCapture()
    {
        _mediaCapture = new MediaCapture();
        await _mediaCapture.InitializeAsync();
        
        // 枚举摄像头所有可用的照片流分辨率
        var photoProperties = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo)
                                          .Cast<ImageEncodingProperties>()
                                          .OrderByDescending(p => p.Width * p.Height)
                                          .FirstOrDefault();
        // 设置为最高分辨率
        if (photoProperties != null)
        {
            await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, photoProperties);
        }
    
        // 可额外配置预览流分辨率用于界面展示,和照片流互不影响
    }
    
    // 拍照逻辑
    private async Task<BitmapImage> CaptureCustomPhoto()
    {
        using var stream = new InMemoryRandomAccessStream();
        await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
        
        var bitmap = new BitmapImage();
        stream.Seek(0);
        bitmap.BeginInit();
        bitmap.StreamSource = stream.AsStream();
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        return bitmap;
    }
    

方案3:集成Windows App SDK混合开发

如果你的项目可以兼容Windows App SDK,也可以将WinUI 3的CameraCapture控件嵌入WPF窗口中,拍照逻辑和UWP完全一致,分辨率配置灵活度更高,适合需要深度定制相机功能的场景。

注意事项

  • 上述所有方案的运行环境要求为Windows 10 1809及以上版本,若需要支持更低版本的Windows系统,可基于DirectShow自定义滤镜读取摄像头的照片流,但实现复杂度较高。
  • 测试前请确认设备摄像头驱动为最新版本,避免出现最高分辨率枚举失败的问题。

内容的提问来源于stack exchange,提问作者Ondra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 22:39:04