다음을 통해 공유


Saving File to Byte Array in Windows 8 WinRT

I’ve been hitting my head to the ground for a couple of days trying send a byte array that represents my Image to my WCF Service in Windows 8, things has changed a lot when it comes to IO and Streams in Windows 8 so I included my code here for others to use

 

 

 

 

 

 

 

Covert File to Byte Array

  1. private async Task<byte[]> ConvertImagetoByte(StorageFileimage)
  2.       {
  3.           IRandomAccessStream fileStream = await image.OpenAsync(FileAccessMode.Read);
  4.           var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
  5.           await reader.LoadAsync((uint)fileStream.Size);
  6.  
  7.           byte[] pixels = new byte[fileStream.Size];
  8.  
  9.           reader.ReadBytes(pixels);
  10.  
  11.           return pixels;
  12.  
  13.       }

Comments

  • Anonymous
    April 17, 2013
    this is old but for the next guy who finds this off a search engine like i did: add: (IBuffer.ToArray() is defined in WindowsRuntimeBufferExtensions) using System.Runtime.InteropServices.WindowsRuntime; then just do: var buffer = await FileIO.ReadBufferAsync(image); var bytes = buffer.ToArray();