This is a migrated thread and some comments may be shown as answers.

Render image from map on background thread

3 Answers 450 Views
Map
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Veteran
Peter asked on 02 Jul 2020, 04:47 PM

I want to be able to render an image from a RadMap control without displaying the map control. I can see the map control rendered in my png but the map background (in this case OSM) does not get rendered with the image. I assume this happens because the map data hasn't yet been retrieved. 

public class MapRender
{
    private Thread _thread;
 
    public void StartRender()
    {
        ThreadStart start = ScreenshotMap; // Render;
        _thread = new Thread(start);
        _thread.SetApartmentState(ApartmentState.STA);
 
        _thread.Start();
    }
 
    private void ScreenshotMap()
    {
        var imageSize = 800;
 
        var osmBasedMapProvider = new OsmBasedMapProvider();
 
        var radMap = new RadMap()
        {
            Provider = osmBasedMapProvider,
            RenderSize = new Size(imageSize, imageSize), Height = imageSize, Width = imageSize
        };
 
        radMap.Loaded += RadMap_Loaded;
        radMap.InitializeCompleted += RadMap_InitializeCompleted;
        radMap.SizeChanged += RadMap_SizeChanged;
 
        var c = new Canvas { Width = imageSize, Height = imageSize };
        c.Children.Add(new Rectangle { Height = imageSize, Width = imageSize, Fill = new SolidColorBrush(Colors.Red) });
        c.Children.Add(radMap);
 
        var dpi = 80;
 
        var bitmap = new RenderTargetBitmap((int)c.Width, (int)c.Height, dpi, dpi, PixelFormats.Default);
        c.Measure(new Size((int)c.Width, (int)c.Height));
        c.Arrange(new Rect(new Size((int)c.ActualWidth, (int)c.ActualHeight)));
        bitmap.Render(c);
 
        var png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(bitmap));
 
        using (Stream stm = File.Create("c:\\temp\\map.png"))
        {
            png.Save(stm);
        }
 
        //radMap.ExportToImage("c:\\temp\\map2.png", true);
    }
}

Is there some way for me to wait for the map data to be retrieved before saving the image file?

Thanks
Pete

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 07 Jul 2020, 02:35 PM

Hello Peter,

I am afraid that RadMap doesn't support image export without rendering the map first. The map needs to be fully rendered in order to take a snapshot of it. However, because of the async nature of the map design, there is no convenient way to check when all map elements are fully loaded and rendered. To achieve your requirement you will need to wait until all tiles and shapes are loaded which is very unclear, because this may depend on the internet connection (if the tiles provider gets images from a web server).    

Regards,
Martin Ivanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Peter
Top achievements
Rank 1
Veteran
answered on 07 Jul 2020, 02:48 PM

Hi Martin,

That's disappointing. I noticed a couple of other threads where people wanted to know when rendering has finished:

https://www.telerik.com/forums/howto-create-image-from-radmap-without-showing-the-radmap

https://www.telerik.com/forums/how-to-know-when-the-radmap-is-finished-rendering

Is there no way to get this raised as a feature request?

Pete

 

0
Martin Ivanov
Telerik team
answered on 09 Jul 2020, 12:10 PM

Hello Peter,

We have decided to log this as a feature request in the feedback portal. You can track its status there.

Regards,
Martin Ivanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Map
Asked by
Peter
Top achievements
Rank 1
Veteran
Answers by
Martin Ivanov
Telerik team
Peter
Top achievements
Rank 1
Veteran
Share this question
or