简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM