How to Save WriteableBitmap Image to StorageFile

How to save WriteableBitmap Image to Storage File in Windows Phone 8.1 RT/Windows 10?

 
To convert a WriteableBitmap to Storage File first you need to Encode the Image in the right way so that it won't turn out to be a blank image.
 
Where wb is your WriteableBitmap Image, the second parameter is which type of format you want to save as. you can choose different available formats Tiff, Jpeg, PNG, BMP, etc.., depending on your selection you can select the format which you want to encode and save.
 
WriteableBitmap
  1. StorageFile filePath = await WriteableBitmapToStorageFile(wb, FileFormat.Jpeg);  
StorageFilefilePath
  1. private async Task < StorageFile > WriteableBitmapToStorageFile(WriteableBitmap WB, FileFormat fileFormat)   
  2. {  
  3.     string FileName = "YourFile.";  
  4.     Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;  
  5.     switch (fileFormat)  
  6.     {  
  7.         case FileFormat.Jpeg:  
  8.             FileName += "jpeg";  
  9.             BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;  
  10.             break;  
  11.         case FileFormat.Png:  
  12.             FileName += "png";  
  13.             BitmapEncoderGuid = BitmapEncoder.PngEncoderId;  
  14.             break;  
  15.         case FileFormat.Bmp:  
  16.             FileName += "bmp";  
  17.             BitmapEncoderGuid = BitmapEncoder.BmpEncoderId;  
  18.             break;  
  19.         case FileFormat.Tiff:  
  20.             FileName += "tiff";  
  21.             BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;  
  22.             break;  
  23.         case FileFormat.Gif:  
  24.             FileName += "gif";  
  25.             BitmapEncoderGuid = BitmapEncoder.GifEncoderId;  
  26.             break;  
  27.     }  
  28.     var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);  
  29.     using(IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))   
  30.     {  
  31.         BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);  
  32.         Stream pixelStream = WB.PixelBuffer.AsStream();  
  33.         byte[] pixels = new byte[pixelStream.Length];  
  34.         await pixelStream.ReadAsync(pixels, 0, pixels.Length);  
  35.         encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint) WB.PixelWidth, (uint) WB.PixelHeight,  
  36.             96.0,  
  37.             96.0,  
  38.             pixels);  
  39.         await encoder.FlushAsync();  
  40.     }  
  41.     return file;  
  42. }  
  43. private enum FileFormat  
  44. {  
  45.     Jpeg,  
  46.     Png,  
  47.     Bmp,  
  48.     Tiff,  
  49.     Gif  
  50. }  
And you can set this saved file path as Image source like the following:
  1. // Get a photo as a Bitmap Image using storage file path.  
  2. BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));  
  3. Myimage.Source=bmpImage ;  
  4. <Image x:Name="MyImage" strech="Uniform"/>