Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将软件位图转换为可写位图_C#_Windows_Uwp - Fatal编程技术网

C# 将软件位图转换为可写位图

C# 将软件位图转换为可写位图,c#,windows,uwp,C#,Windows,Uwp,每次我想将我的软件位图转换为可写位图时,我都会遇到以下异常: System.Runtime.InteropServices.COMException 以下是我的代码片段: private async void Start(object sender, RoutedEventArgs e) { _MediaCapture = new MediaCapture(); await _MediaCapture.InitializeAsy

每次我想将我的
软件位图
转换为
可写位图
时,我都会遇到以下异常:
System.Runtime.InteropServices.COMException

以下是我的代码片段:

 private async void Start(object sender, RoutedEventArgs e)
        {

            _MediaCapture = new MediaCapture();
            await _MediaCapture.InitializeAsync();

            mediaElement.Source = _MediaCapture;
            await _MediaCapture.StartPreviewAsync();
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 1);
            timer.Tick += HandleTimerTick;
            timer.Start();
        }

        private async void HandleTimerTick(object Sender, object E)
        {


            var frame = await _MediaCapture.GetPreviewFrameAsync();
            SoftwareBitmap frameBitmap = frame.SoftwareBitmap;
            WriteableBitmap bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight);
            try
            {
                frameBitmap.CopyToBuffer(bitmap.PixelBuffer);
            }
            catch (Exception)
            {
                Debug.WriteLine("Exception ");
            }
        }
线路

frameBitmap.CopyToBuffer(bitmap.PixelBuffer); 
正在抛出异常


我正在x64远程设备上调试此问题。

我可以使用您的代码重现此问题。它是由frame.softwarebramp导致的,始终返回null

您可以使用以下代码修复此问题:

    private async void button_Click(object sender, RoutedEventArgs e)
    {
        _mediaCapture = new MediaCapture();

        await _mediaCapture.InitializeAsync();

        mediaElement.Source = _mediaCapture;

        await _mediaCapture.StartPreviewAsync();

        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0, 0, 0, 1);
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private async void Timer_Tick(object sender, object e)
    {
        var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

        var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

        var frame = await _mediaCapture.GetPreviewFrameAsync(videoFrame);

        SoftwareBitmap frameBitmap = frame.SoftwareBitmap;

        WriteableBitmap bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight);

        frameBitmap.CopyToBuffer(bitmap.PixelBuffer);

        Debug.WriteLine("done");
    }

抛出的异常是什么?@Dmitry Bychenko查看上面的:System.Runtime.InteropServices.ComeExceptionSide注意:吞咽异常,如
catch(exception){Debug.WriteLine(“exception”);}
,是一种非常糟糕的做法。我知道这一点。这只是为了澄清问题的存在。但是为了便于将来参考,SDK示例如下:工作正常。感谢you@Jeffrey我不明白这有什么帮助。。。看起来唯一真正的区别是使用GetPreviewFrameAsync的返回值。那没有道理!该函数不使用传递的视频帧来填充吗?如果是,则应以不同的方式记录。如果您在InitializeAsync中指定的位图像素格式与在视频帧中指定的位图像素格式不同,您是否也能告诉我性能是否会受到影响?@Jeffrey澄清一下。。。它确实有用!我只是不明白为什么。请看一下文档中靠近本页底部的“重要”部分: