How to create thumbnail/snapshot from bing maps in c# for universal solution

179 Views Asked by At

I have a universal solution with platform supporting Windows Phone. After the user has tapped on Bing Maps, a pin is created an snapshot of the map is also created, but this image doesn't contain the pin.

I am using Windows.UI.Xaml.Controls.Maps.MapControl with this xaml:-

XAML:

<mp:MapControl x:Name="mpWinPh"
                           Grid.Row="1"
                           Width="{Binding ElementName=ControlRoot,
                                           Path=Width}"
                           Height="{StaticResource ActivityLogGfxSize}"
                           MapServiceToken="{Binding Credentials}"
                           MapTapped="mpWinPh_MapTapped" />

C#:

private async void mpWinPh_MapTapped(MapControl sender, MapInputEventArgs args)
{
    var location = args.Location;
    BasicGeoposition snPosition = new BasicGeoposition
    {
        Latitude = location.Position.Latitude,
        Longitude = location.Position.Longitude,
        Altitude = location.Position.Altitude
    };
    Geopoint snPoint = new Geopoint(snPosition);
    MapIcon icon = new MapIcon { Location = snPoint };
    mpWinPh.Center = snPoint;
    mpWinPh.ZoomLevel = 15;
    mpWinPh.MapElements.Clear();
    mpWinPh.MapElements.Add(icon);

    Field.GpsCoordinate coordinate = new Field.GpsCoordinate { Latitude = location.Position.Latitude, Longitude = location.Position.Longitude };
    (viewModel as LocationControlViewModel).UpdateGps(coordinate);

    await SaveImage();
}

private async Task SaveImage()
{
    var renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(mpWinPh);
    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

    var pixels = pixelBuffer.ToArray();

    // Useful for rendering in the correct DPI
    var displayInformation = DisplayInformation.GetForCurrentView();

    var stream = new InMemoryRandomAccessStream();
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                         BitmapAlphaMode.Premultiplied,
                         (uint)renderTargetBitmap.PixelWidth,
                         (uint)renderTargetBitmap.PixelHeight,
                         displayInformation.RawDpiX,
                         displayInformation.RawDpiY,
                         pixels);

    await encoder.FlushAsync();
    stream.Seek(0);
    byte[] resizedData = new byte[stream.Size];
    await stream.ReadAsync(resizedData.AsBuffer(), (uint)stream.Size, InputStreamOptions.None);
    await SaveStreamToTempLocation(() => Task.FromResult(new MemoryStream(resizedData) as Stream));
}
1

There are 1 best solutions below

1
Duncan Lawler On

Since adding data to the map is an asynchronous operation, after adding any data to the map (such as a MapIcon), or changing the map view with any of the view setting API's, you should wait until the LoadingStatus of the map control becomes MapLoadingStatus.Loaded. This indicates the map control has finished the asynch work and is fully rendered. You can register for the LoadingStatusChanged event on the map control.