如何使用解决方案中的图像制作字节数组。 (Windows Phone 8.1 运行时)
How to make a byte array with an image from solution. (Windows Phone 8.1 runtime)
我需要从图像中生成一个字节[],但首先我想我必须将它保存为图像文件,如 StorageFile 之类的,然后将其传递给这个方法,它将完成这项工作:
private async Task<byte[]> StorageFileToByteArray(StorageFile file)
{
byte[] byteArray = new byte[0];
if (null != file)
{
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
var reader = new DataReader(fileStream.GetInputStreamAt(0));
await reader.LoadAsync((uint)fileStream.Size);
byteArray = new byte[fileStream.Size];
}
return byteArray;
}
但是如何保存到StorageFile呢?
这是我在 BitmapImage 中加载它的方式:
BitmapImage bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/no_capture_receipt.png", UriKind.Absolute));
然后如何将此图像保存在存储文件中
没有你想的那么简单。
private async Task<byte[]> StorageFileToByteArray(string fileName) //filename = "no_capture_receipt.png"
{
var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var storageFile = await folder.GetFileAsync(fileName);
var buffer = await Windows.Storage.FileIO.ReadBufferAsync(storageFile);
return buffer.ToArray();
}
不要忘记将图像的复制到输出目录更改为 Copy if newer
!
我需要从图像中生成一个字节[],但首先我想我必须将它保存为图像文件,如 StorageFile 之类的,然后将其传递给这个方法,它将完成这项工作:
private async Task<byte[]> StorageFileToByteArray(StorageFile file)
{
byte[] byteArray = new byte[0];
if (null != file)
{
IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
var reader = new DataReader(fileStream.GetInputStreamAt(0));
await reader.LoadAsync((uint)fileStream.Size);
byteArray = new byte[fileStream.Size];
}
return byteArray;
}
但是如何保存到StorageFile呢?
这是我在 BitmapImage 中加载它的方式:
BitmapImage bitmapImage = new BitmapImage(new Uri("ms-appx:///Assets/no_capture_receipt.png", UriKind.Absolute));
然后如何将此图像保存在存储文件中
没有你想的那么简单。
private async Task<byte[]> StorageFileToByteArray(string fileName) //filename = "no_capture_receipt.png"
{
var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var storageFile = await folder.GetFileAsync(fileName);
var buffer = await Windows.Storage.FileIO.ReadBufferAsync(storageFile);
return buffer.ToArray();
}
不要忘记将图像的复制到输出目录更改为 Copy if newer
!