在地图上添加带有图像作为流(由 xaml 生成)的 mapicon
我不知道您如何通过 xaml 生成图像流,我猜您有一个控件或其他东西,并且您使用RenderTargetBitmap生成一个图像源,该图像源填充了 XAML 可视化树的组合内容。然后您可以使用BitmapEncoder创建一个InMemoryRandomAccessStream
.
为了演示如何使用BitmapEncoder
,我以官方MapControl 示例的场景 1为例:
SymbolIcon
为 the创建一个MapIcon
并创建一个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);
}
}
此演示的渲染图像:
