如何将存储文件分配给ByteArray

本文关键字:ByteArray 文件分配 存储 | 更新日期: 2023-09-27 18:26:31

如何将存储文件分配给ByteArray?

var file = await openPicker.PickSingleFileAsync();

如何将存储文件分配给ByteArray

这里是一个使用文件选择器的示例,获取存储文件并将其转换为字节数组。

private async void Button_Click_2(object sender, RoutedEventArgs e)
{
    var picker = new FileOpenPicker();
    picker.ViewMode = PickerViewMode.Thumbnail;
    picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".png");
    var file = await picker.PickSingleFileAsync();
    if (file != null)
    {
        var stream = await file.OpenStreamForReadAsync();
        var bytes = new byte[(int)stream.Length];
        stream.Read(bytes, 0, (int)stream.Length);
    }
}