【问题标题】:Why can't my WritableBitmap be found through the IRandomAccessStream?为什么通过 IRandomAccessStream 找不到我的 WritableBitmap?
【发布时间】:2015-11-07 09:31:44
【问题描述】:

我有一个从网络摄像头的快照创建的 WritableBitmap。我想将它加载到 BitmapDecoder 中,以便裁剪图像。代码如下:

IRandomAccessStream ras = displaySource.PixelBuffer.AsStream().AsRandomAccessStream();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras); //Fails Here

displaySource 是来自网络摄像头的 WritableBitmap,我得到的异常是

{“找不到组件。(HRESULT 异常:0x88982F50)”}

这对我来说很奇怪,因为我可以将 displaySource 加载到我的 GUI 上,但我无法使用此代码。我也尝试将其切换到 InMemoryRandomAccessStream。从我看到的情况来看,这个异常没有任何 stackoverflow 解决方案。

displaySource 是从这段代码生成的:

                // Create a WritableBitmap for our visualization display; copy the original bitmap pixels to wb's buffer.
                // Note that WriteableBitmap doesn't support NV12 and we have to convert it to 32-bit BGRA.
                using (SoftwareBitmap convertedSource = SoftwareBitmap.Convert(previewFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8))
                {
                    displaySource = new WriteableBitmap(convertedSource.PixelWidth, convertedSource.PixelHeight);
                    convertedSource.CopyToBuffer(displaySource.PixelBuffer);
                }

其中预览帧是来自网络摄像头的 VideoFrame 设置。

提前致谢。

【问题讨论】:

    标签: c# bitmap stream win-universal-app


    【解决方案1】:

    WriteableBitmap.PixelBuffer 已解码为 BGRA 像素缓冲区。

    BitmapDecoder 需要一个解码图像(.bmp、.png、.jpg 等)并生成一个像素缓冲区。 BitmapEncoder 采用像素缓冲区并生成编码图像。

    您可以通过调用 BitmapEncoder 编码为 .png(不要使用 jpg 等有损格式)然后调用 BitmapDecoder 进行解码来往返。如果您的目标是保存裁剪后的图像,那么只需使用 BitmapEncoder。两个类都可以应用 BitmapTransform。

    如果您只想裁剪,那么通过位图格式进行往返是多余的。只需从 PixelArray 中复制所需的像素就很容易了。如果你不想自己写,开源库WriteableBitmapEx 提供了对 WriteableBitmap 的扩展,并有一个 Crop 方法:

    // Crops the WriteableBitmap to a region starting at P1(5, 8) and 10px wide and 10px high
    var cropped = writeableBmp.Crop(5, 8, 10, 10);
    

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2021-01-09
      • 1970-01-01
      相关资源
      最近更新 更多