Render image from map on background thread

4 posts, 0 answers
  1. Peter
    Peter avatar
    64 posts
    Member since:
    Oct 2016

    Posted 02 Jul Link to this post

    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

  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    2420 posts

    Posted 07 Jul Link to this post

    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.
  3. Peter
    Peter avatar
    64 posts
    Member since:
    Oct 2016

    Posted 07 Jul in reply to Martin Ivanov Link to this post

    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

     

  4. Martin Ivanov
    Admin
    Martin Ivanov avatar
    2420 posts

    Posted 09 Jul Link to this post

    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.
Back to Top