【问题标题】:Convert SoftwareBitmap to WriteableBitmap将 SoftwareBitmap 转换为 WriteableBitmap
【发布时间】:2015-10-28 08:32:17
【问题描述】:

每当我想将我的 SoftwareBitmap 转换为 WriteableBitmap 时,我都会收到以下异常: System.Runtime.InteropServices.COMException.

这是我的代码 sn-p:

 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 上调试它。

【问题讨论】:

  • 抛出了什么异常?
  • @Dmitry Bychenko 看上面:System.Runtime.InteropServices.COMException
  • 旁注:吞下catch (Exception) { Debug.WriteLine("Exception "); }这样的例外是一种非常糟糕的做法
  • 我知道。只是为了澄清存在问题。但是谢谢你的笔记
  • 为了将来参考,SDK 示例在这里:github.com/Microsoft/Windows-universal-samples/tree/master/…

标签: c# windows uwp


【解决方案1】:

我可以使用您的代码重现此问题。是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");
    }

【讨论】:

  • 工作正常。谢谢
  • @Jeffrey 我不明白这有什么帮助......看来唯一真正的区别是使用 GetPreviewFrameAsync 的返回值。那没有意义!该函数不使用传递的 VideoFrame 填充吗?如果是这样,它应该以不同的方式记录。如果您在 InitializeAsync 中指定的位图像素格式与在 VideoFrame 中的位图像素格式不同,您能否告诉我是否存在性能问题?
  • @Jeffrey 说清楚......它确实有效!我只是很困惑为什么。
  • 查看文档的“重要”部分,靠近本页底部:msdn.microsoft.com/en-us/library/windows/apps/mt280228.aspx
猜你喜欢
  • 2016-09-19
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 2012-01-12
  • 2013-06-22
相关资源
最近更新 更多