从 Byte 数组创建 BitmapImage 并将其显示在 Image 对象上 UWP Raspberry Pi 3 [英] Create a BitmapImage from a Byte array and display it on an Image object UWP Raspberry Pi 3

查看:75
本文介绍了从 Byte 数组创建 BitmapImage 并将其显示在 Image 对象上 UWP Raspberry Pi 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码在文件 BMP 中写入一个字节数组:

I'm using this code to write a Byte Array inside a file BMP:

private async void ScriviBMP()
    {
        using (Stream stream = immagineBitmap.PixelBuffer.AsStream())
        {
            await stream.WriteAsync(arrayImmagine, 0, arrayImmagine.Length);
        }
        StorageFolder folder = KnownFolders.PicturesLibrary;
        if (folder != null)
        {
            StorageFile file = await folder.CreateFileAsync("area2_128x128" + ".bmp", CreationCollisionOption.ReplaceExisting);
            using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, storageStream);
                var pixelStream = immagineBitmap.PixelBuffer.AsStream();
                var pixels = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)immagineBitmap.PixelWidth, (uint)immagineBitmap.PixelHeight, 48, 48, pixels);
                await encoder.FlushAsync();
            }
        }
    }

然后我使用此代码在 Image 对象中显示 BMP 图像

Then i'm using this code to display the BMP image in a Image object

private async void VisBMP()
    {
        var file = await KnownFolders.PicturesLibrary.GetFileAsync("area2_128x128.bmp");
        using (var fileStream = (await file.OpenAsync(Windows.Storage.FileAccessMode.Read)))
        {
            var bitImg = new BitmapImage();
            //bitImg.UriSource = new Uri(file.Path);
            bitImg.SetSource(fileStream);
            image.Source = bitImg;
        }
    }

这些函数需要大约 400 毫秒才能完成这个过程,这是很多时间.有没有办法避免使用BMP文件而只使用流在图像对象上显示图像?可能是调试程序会减慢进程?我使用的是 Visual Studio 2015.

these functions take about 400 milliseconds to complete the process, that's a lot of time. Is there a way to avoid the usage of a BMP file and use only a stream to display the image on the image object? It can be that debugging the program can slow the processes? I'm using Visual Studio 2015.

推荐答案

你可以在 InMemoryRandomAccessStream 中将数据缓冲区(arrayImmagine)传输到图像.这些代码大约需要 200 毫秒.我测试了以下几条代码.另外,你可以参考这个 文章 以获取更多信息.

You can transfer the data buffer(arrayImmagine) to image in InMemoryRandomAccessStream.These codes take about 200ms.I tested with following pieces of code. In addition, you can reference this article to get more information.

        BitmapImage biSource = new BitmapImage();
        using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(bytes.AsBuffer());
            stream.Seek(0);
            await biSource.SetSourceAsync(stream);
        }

        image.Source = biSource;

这篇关于从 Byte 数组创建 BitmapImage 并将其显示在 Image 对象上 UWP Raspberry Pi 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆