簡體   English   中英

如何在C#中從bing映射創建縮略圖/快照以實現通用解決方案

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

我有一個支持Windows Phone平台的通用解決方案。 用戶在Bing Maps上點擊后,會創建一個圖釘,還會創建該地圖的快照,但是此圖像不包含圖釘。

我正在使用Windows.UI.Xaml.Controls.Maps.MapControl與此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));
}

由於將數據添加到地圖是異步操作,因此在將任何數據添加到地圖(例如MapIcon)或使用任何視圖設置API更改地圖視圖之后,您都應等待,直到地圖控件的LoadingStatus變為MapLoadingStatus已加載。 這表明地圖控件已經完成了asynch工作並已完全呈現。 您可以在地圖控件上注冊LoadingStatusChanged事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM