要在地圖上
與圖像流(通過XAML生成的)添加mapicon我不知道你是怎麼生成的XAML一個圖像流,我想你有一個控制或東西,您使用RenderTargetBitmap生成一個圖像源,該圖像源用XAML可視化樹的組合內容填充。然後你可以使用BitmapEncoder創建一個InMemoryRandomAccessStream
。
爲了演示如何使用BitmapEncoder
,我採取的官方MapControl sample場景1,例如這裏:
創建爲MapIcon
一個SymbolIcon
並創建一個Button
把MapIcon
上圖:
<Grid x:Name="imgContainer" Margin="0,5" Width="20" Height="25">
<SymbolIcon Symbol="AddFriend" Foreground="Red" />
</Grid>
<Button Content="Place MapIcon" Click="Button_Click" />
後面的代碼:
private async void Button_Click(object sender, RoutedEventArgs e)
{
//render symbolicon to bmp
RenderTargetBitmap renderbmp = new RenderTargetBitmap();
await renderbmp.RenderAsync(imgContainer);
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
//create a bitmap encoder
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
//write pixels into this encoder
var pixels = await renderbmp.GetPixelsAsync();
var reader = DataReader.FromBuffer(pixels);
byte[] bytes = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(bytes);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight,
(uint)renderbmp.PixelWidth, (uint)renderbmp.PixelHeight, 0, 0, bytes);
await encoder.FlushAsync();
mapIconStreamReference = RandomAccessStreamReference.CreateFromStream(stream);
//create mapIcon
var mapIcon = new MapIcon();
mapIcon.Image = mapIconStreamReference;
mapIcon.Location = new Geopoint(myMap.Center.Position);
mapIcon.Title = "Some label".ToString();
myMap.MapElements.Add(mapIcon);
}
}
渲染圖像是演示:
你的代碼沒有問題,你想要什麼?在xaml代碼中添加mapicon並且不在代碼後面生成它? –
在地圖上添加帶有圖像的mapicon作爲流(由xaml生成) – user3045261