2013-08-31 63 views
5

我需要File Picker選擇的文件的base64字符串。Metro獲取StorageFile的base64字符串

//file is a StorageFile 

FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.ReadWrite); 

using (MemoryStream ms = new MemoryStream()) 
{ 
    Stream ss1 = stream.AsStream(); ; 
    s1.CopyTo(ms); 

} 
byteArray = ms.ToArray(); 
string imageStringSixtyfour = Convert.ToBase64String(byteArray); 
if (imageStringSixtyfour != null) 
return imageStringSixtyfour; 

返回的字符串總是空的,有什麼想法?大多數在線的例子是與不是的W8平臺一部分的類

回答

9

試試這個

private async Task<string> StorageFileToBase64(StorageFile file) 
{ 
    string Base64String = ""; 

    if (file != null) 
    { 
     IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read); 
     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; 
} 
+0

謝謝你......一段時間後發現一個DataReader的類,它工作得很好,大加讚賞 –

相關問題