private async Task<string> WriteTempThumbnailImageAsync(ImageSource mapImageSource)
{
string outputFilename = string.Empty;
try
{
// Export the current map view display to a bitmap
var mapImage = mapImageSource as WriteableBitmap;
// Create a new file in the device's Pictures folder
var outStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("MapImage_Temp.jpg", CreationCollisionOption.GenerateUniqueName);
outputFilename = outStorageFile.Name;
// Open the new file for read/write
using (var stream = await outStorageFile.OpenAsync(FileAccessMode.ReadWrite))
{
// Create a bitmap encoder to encode the image to Jpeg
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
// Read the pixels from the map image into a byte array
var pixelStream = mapImage.PixelBuffer.AsStream();
var pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
// Use the encoder to write the map image pixels to the output file
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)mapImage.PixelWidth, (uint)mapImage.PixelHeight, 96.0, 96.0, pixels);
await encoder.FlushAsync();
}
}
catch
{
// Exception message will be shown in the calling function
throw;
}
return outputFilename;
}