Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you assign a storage file to a ByteArray?

How can I assign a storage file to a ByteArray?

var file = await openPicker.PickSingleFileAsync();
like image 555
Terry Bennett Avatar asked Oct 28 '25 16:10

Terry Bennett


1 Answers

here a sample using a file picker, getting a storage file and convert it to a byte array.

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);
    }
}
like image 146
danvy Avatar answered Oct 30 '25 06:10

danvy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!