123456789101112131415161718192021222324252627282930313233 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Windows.Storage.Streams;
- using Windows.Storage;
- namespace KdanCommon.Helpers
- {
- public static class Base64Helper
- {
- // https://stackoverflow.com/questions/18553691/metro-getting-the-base64-string-of-a-storagefile/18555063
- public static async Task<string> StorageFileToBase64(StorageFile file)
- {
- string base64String = "";
- if (file != null)
- {
- using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
- {
- using(var reader = new DataReader(fileStream.GetInputStreamAt(0)))
- {
- await reader.LoadAsync((uint)fileStream.Size);
- byte[] byteArray = new byte[fileStream.Size];
- reader.ReadBytes(byteArray);
- base64String = Convert.ToBase64String(byteArray);
- }
- }
- }
- return base64String;
- }
- }
- }
|