I am trying to convert a Telerik map to Image.
Telerik has an ExportToImage
method that works for a map created in View.
But in this case I create a map dynamically and not in View.
When I use PrintVisual
I see the map perfectly,
But when I use all kinds of methods that convert FrameworkElement
to Image,
including Telerik's ExportToImage
, I only get a picture of the Layers inside the map.
private void LocationsToImage(ICollection<LocationType> locationCollection, Size size)
{
var map = new RadMap
{
Language = XmlLanguage.GetLanguage(CultureInfo.DefaultThreadCurrentCulture.Name),
Width = size.Width,
Height = size.Height
};
var layer = new InformationLayer();
map.Items.Add(layer);
MapHelper.DrawLocations(map, layer, locationCollection);
//print
map.Arrange(new Rect(size));
map.Measure(size);
map.Provider = MapProviderFactory.Create();
var path = @"C:\Test.png";
SaveToPng(map, path);
}
void SaveToPng(FrameworkElement visual, string fileName)
{
var encoder = new PngBitmapEncoder();
SaveUsingEncoder(visual, fileName, encoder);
}
void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder)
{
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(visual);
BitmapFrame frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);
using (var stream = File.Create(fileName))
{
encoder.Save(stream);
}
}
And the result I get:
Instead of a normal map, like this:
Would appreciate help
CodePudding user response:
You probably have to wait for the map to load, try doing this:
map.Provider = MapProviderFactory.Create();
await Task.Run(async () => await Task.Delay(TimeSpan.FromSeconds(1)));
And then save to image