0

我使用这个代码写一个文件BMP内的字节数组:从字节数组创建的BitmapImage和图像对象上显示它UWP树莓裨3

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

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

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年

+1

'immagineBitmap'是一个WriteableBitmap吗?然后,您可以直接将其分配给图像控件的Source属性,如'image.Source = immagineBitmap;' – Clemens

回答

0

可以在InMemoryRandomAccessStream传输数据缓冲器(arrayImmagine)图像约200ms.I与下面的代码段测试。这些代码进行。另外,您可以参考此article以获取更多信息。

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

     image.Source = biSource; 
+0

虽然这是真的,但它仍然看起来完全是多余的。 OP肯定可以直接将它们的WriteableBitmap分配给Image的Source。 – Clemens

+0

我试过直接将WriteableBitmap分配给图像的源,但无法正确显示图像。 –