Everytime when i want to convert my SoftwareBitmap to a WriteableBitmap I get the following Exception:
System.Runtime.InteropServices.COMException.
Here is my code snippet for that:
 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 ");
            }
        }
The line
frameBitmap.CopyToBuffer(bitmap.PixelBuffer); 
is throwing the Exception.
I am debugging this on a x64 RemoteDevice.
I can reproduce this issue by using your code. It is caused by frame.SoftwareBitmap always returns null.
You can fix this issue by using the code as following:
    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");
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With