簡體   English   中英

如何在Windows Phone 8中將像素字節數組轉換為位圖圖像

[英]How to convert pixel byte array to bitmap image in windows phone 8

我想在Windows Phone 8中使用像素字節繪制圖像。 所以我通過c ++庫(c ++ / CLI)得到了一些像素字節。 但是像素數據不包括位圖標題。 它只是像素字節數組。 這可能將像素數據數組轉換為Windows Phone中沒有位圖標題的位圖圖像嗎?

    public void updateImage(byte[] byteArray, UInt32 bufferSize, int cvtWidth, int cvtHeight)
    {
        // I saw this source a lot of search. But It's not work. It makes some exeption.
        BitmapImage bi = new BitmapImage();
        MemoryStream memoryStream = new MemoryStream(byteArray);
        bi.SetSource(memoryStream);

        ImageScreen.Source = bi;
    }

您需要使用WriteableBitmap: http : //msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.media.imaging.writeablebitmap

然后,您可以使用AsStream訪問它的PixelBuffer並將該數組保存到流中。

//srcWidth and srcHeight are original image size, needed
//srcData is pixel data array
WriteableBitmap wb = new WriteableBitmap(srcWidth, srcHeight); 

using (Stream stream = wb.PixelBuffer.AsStream()) 
{ 
    await stream.WriteAsync(srcData, 0, srcData.Length); 
} 

編輯

如Proglamour在Windows Phone中所述,沒有PixelBuffer,但是存在一個名為Pixels的屬性,它是一個數組。

它不能被替換,但是它的內容可以使它工作:

WriteableBitmap wb = new WriteableBitmap(srcWidth, srcHeight); 
Array.Copy(srcData, wb.Pixels, srcData.Length);

我解決了這個問題。 感謝您的幫助Filip。

    public void updateImage(byte[] byteArray, UInt32 bufferSize, int cvtWidth, int cvtHeight)
    {
        Dispatcher.BeginInvoke(() =>
        {
            WriteableBitmap wb = new WriteableBitmap(cvtWidth, cvtHeight);
            System.Buffer.BlockCopy(byteArray, 0, wb.Pixels, 0, byteArray.Length);
            //wb.Invalidate();
            ImageScreen.Source = wb;
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM