繁体   English   中英

将SoftwareBitmap转换为WriteableBitmap

[英]Convert SoftwareBitmap to WriteableBitmap

每当我想将我的SoftwareBitmap转换为WriteableBitmap我都会遇到以下异常: 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 RemoteDevice上调试它。

我可以使用您的代码重现此问题。 它是由frame.SoftwareBitmap始终返回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");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM