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

会有一股神秘感。 提交于 2019-12-24 14:15:21

问题


I want to draw Image in windows phone 8 using pixel byte. so I get some pixel byte via c++ library(c++/CLI). But pixel data not include bitmap Header. It is just pixel byte array. Is this possible to convert pixel data array to bitmap Image without bitmap header in 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;
    }

回答1:


You need to use a WriteableBitmap : http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.writeablebitmap

Then you can access it's PixelBuffer using AsStream and save that array to the stream.

//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); 
} 

EDIT

As Proglamour stated in WIndows Phone there is no PixelBuffer, but a property named Pixels which is an array exists.

It cannot be replaced but it's content can so this should work:

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



回答2:


I solved this problem. Thanks for your help 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;
        });
    }


来源:https://stackoverflow.com/questions/23073015/how-to-convert-pixel-byte-array-to-bitmap-image-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!