如何将Base64转换为BitmapImage并将其保存在Project的Subforlder中

时间:2014-02-24 09:35:21

标签: winrt-xaml

我需要使用webService检索图像,然后将其转换为base64 我需要将此base64转换为BitmapImage并将其保存到我添加到项目中的文件夹

我有这些问题:

1)我是否必须将Base64String转换为Stream并使用它创建一个bitmapImage?

这个BAse64字符串是否可以用作创建bitmapImage的字节而不将其转换为Buffer?

2)从Base64转换到缓冲区后,如何在特定文件夹中保存bitmapImage?

问题: 没有错误,也没有保存图像!

我似乎无法保存它。非常感谢您的帮助。


public async Task Base64StringToBitmapTask(string Base64source,string Filenm)
 {

  var ims = new InMemoryRandomAccessStream();    

  var bytes = Convert.FromBase64String(Base64source);
  var dataWriter = new DataWriter(ims);        
  dataWriter.WriteBytes(bytes);         
  await dataWriter.StoreAsync();        
  ims.Seek(0);

  var bm = new BitmapImage();
  bm.SetSource(ims);         

  byte[] pixeBuffer = null;

  using (MemoryStream ms = new MemoryStream())
   {
      WriteableBitmap wb = new WriteableBitmap(bm.PixelWidth,bm.PixelHeight);
       await wb.SetSourceAsync(ims);

      Stream s1 = wb.PixelBuffer.AsStream();
       s1.CopyTo(ms);

       pixeBuffer = ms.ToArray();

     }
  await InsertImageInFolder(pixeBuffer, Filenm);

 }

private async Task InsertImageInFolder(byte[] buffer, string filename)
 {


   IBuffer Ibr = buffer.AsBuffer();

   StorageFolder folder = ApplicationData.Current.LocalFolder;

   //-1-- Get the folder added into the Project


   StorageFolder subfolder = await StorageFolder.GetFolderFromPathAsync("ms-appx:///ProductImages");


  //-2---- create a file in this folder 

  await subfolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

  StorageFile  sampleFile = await subfolder.GetFileAsync(filename);


  await FileIO.WriteBufferAsync(sampleFile, Ibr);         

}



0 个答案:

没有答案