Arr*_*rie 6 c# windows-phone-8
我正在使用Visual Studio 2012,c#,silverlight,windows phone 8 app.
我们从Web服务获取数据,通过Web服务,我们得到一个base64字符串的图片.
我将它转换为字节数组,现在我想保存它以便使用内存流来存储Windows手机?我不知道这是不是正确的做法.我不想将它保存到独立存储,只是本地文件夹,因为我想在一个人点击链接后显示图片.
这就是我到目前为止所拥有的.
byte[] ImageArray;
var image = Attachmentlist.Attachment.ToString();
imagename = Attachmentlist.FileName.ToString();
ImageArray = Convert.FromBase64String(image.ToString());
StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
await myfolder.CreateFileAsync(imagename.ToString());
StorageFile myfile = await myfolder.GetFileAsync(imagename.ToString());
MemoryStream ms = new MemoryStream();
Run Code Online (Sandbox Code Playgroud)
所以在我初始化了内存流后,如何获取字节数组并将其写入存储文件,然后再次检索它?
Raf*_*fal 14
要将文件写入光盘,请尝试以下代码:
StorageFile sampleFile = await myfolder.CreateFileAsync(imagename.ToString(),
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(sampleFile, ImageArray);
Run Code Online (Sandbox Code Playgroud)
内存流创建在内存中写入的流,因此它不适用于此问题.
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile imageFile = await folder.CreateFileAsync("Sample.png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
{
using (DataWriter dataWriter = new DataWriter(outputStream))
{
dataWriter.WriteBytes(imageBuffer);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
//await outputStream.FlushAsync();
}
//await fileStream.FlushAsync();
}
Run Code Online (Sandbox Code Playgroud)