简体   繁体   English

如何在Windows Phone 8中将像素字节数组转换为位图图像

[英]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. 我想在Windows Phone 8中使用像素字节绘制图像。 so I get some pixel byte via c++ library(c++/CLI). 所以我通过c ++库(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? 这可能将像素数据数组转换为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 您需要使用WriteableBitmap: http : //msdn.microsoft.com/zh-cn/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. 然后,您可以使用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); 
} 

EDIT 编辑

As Proglamour stated in WIndows Phone there is no PixelBuffer, but a property named Pixels which is an array exists. 如Proglamour在Windows Phone中所述,没有PixelBuffer,但是存在一个名为Pixels的属性,它是一个数组。

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. 感谢您的帮助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