c# – 使用MediaCapture的CaptureElement自定义分辨率拍摄照片

我正在使用CaptureElement显示我的 Windows Store App的相机Feed.现在,当用户点击显示时,我想将照片作为流捕获,我使用下面的代码进行工作.不幸的是,返回的图像的分辨率为640 x 360,但相机(Surface RT)可以拍摄1280×800的图像,我想做.

我试过设置

encodingProperties.Height = 800;
        encodingProperties.Width = 1280;

但那没有办法.那么如何更改分辨率?

private async void captureElement_Tapped(object sender,TappedRoutedEventArgs e)
    {
        var encodingProperties = ImageEncodingProperties.CreateJpeg();
        //encodingProperties.Height = 800;
        //encodingProperties.Width = 1280;
        WriteableBitmap wbmp;

        using (var imagestream = new InMemoryRandomAccessstream())
        {
            await captureMgr.CapturePhotoToStreamAsync(encodingProperties,imagestream);
            await imagestream.FlushAsync();
            imagestream.Seek(0);
            wbmp = await new WriteableBitmap(1,1).FromStream(imagestream);
        }

        capturedImage.source = wbmp;
    }

解决方法

所以我终于弄清楚如何来这个,也摆脱了可怕的“HRESULT:0xC00D36B4”错误,部分是由于这里找到的代码
http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/751b8d83-e646-4ce9-b019-f3c8599e18e0

我做了一些调整,这就是为什么我在这里重新发布我的代码

MediaCapture mediaCapture;
    DeviceinformationCollection devices;

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        devices = await Deviceinformation.FindAllAsync(DeviceClass.VideoCapture);
        this.mediaCapture = new MediaCapture();
        if (devices.Count() > 0)
        {
            await this.mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { Videodeviceid = devices.ElementAt(1).Id,PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview });
            SetResolution();
        }  
    }


    //This is how you can set your resolution
    public async void SetResolution()
    {
        System.Collections.Generic.IReadOnlyList<IMediaEncodingProperties> res;
        res = this.mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
        uint maxResolution = 0;
        int indexMaxResolution = 0;

        if (res.Count >= 1)
        {
            for (int i = 0; i < res.Count; i++)
            {
                VideoEncodingProperties vp = (VideoEncodingProperties)res[i];

                if (vp.Width > maxResolution)
                {
                    indexMaxResolution = i;
                    maxResolution = vp.Width;
                    Debug.WriteLine("Resolution: " + vp.Width);
                }
            }
            await this.mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview,res[indexMaxResolution]);
        }
    }

虽然拍照,确保你一直使用.VideoPreview,而不是.Photo!

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...