2015-11-06 54 views
0

我有一個WritableBitmap是從網絡攝像機的快照創建的。我想將它加載到BitmapDecoder中,以便裁剪圖像。下面是代碼:爲什麼我的WritableBitmap無法通過IRandomAccessStream找到?

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設置。

在此先感謝。

回答

2

的WriteableBitmap.PixelBuffer已解碼爲像素BGRA的緩衝器。

BitmapDecoder期望的解碼圖像(.BMP,.PNG,.JPG,等),併產生一個像素緩衝器。 BitmapEncoder需要一個像素緩衝區併產生一個編碼圖像。

你可以通過調用的BitmapEncoder編碼爲.png格式往返(不使用有損格式,如JPG),然後BitmapDecoder解碼。如果您的目標是保存裁剪後的圖像,則只需使用BitmapEncoder。這兩個類都可以應用BitmapTransform。

如果你想要做的是作物然後通過位圖格式雙向傳遞是矯枉過正。只需將想要的像素複製出PixelArray就相當容易。如果您不想自己編寫它,開源庫WriteableBitmapEx提供了對WriteableBitmap的擴展並具有裁剪方法:

// 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); 
相關問題