为什么可以';我的可写位图不能通过IRandomAccessStream找到
本文关键字:不能 IRandomAccessStream 找到 位图 我的 为什么 | 更新日期: 2023-09-27 18:21:05
我有一个WritableBitmap,它是从网络摄像头的快照创建的。我想把它加载到位图解码器中,这样我就可以裁剪图像。这是代码:
IRandomAccessStream ras = displaySource.PixelBuffer.AsStream().AsRandomAccessStream();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras); //Fails Here
displaySource是来自网络摄像头的可写位图,我得到的例外是
{"找不到组件。(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设置。
提前谢谢。
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);