ThinkGeo.com    |     Documentation    |     Premium Support

Take snapshot of the map

Hi,

I want to capture snapshot (in image/bitmap type) of the WPF map with its features.
Is it possible to do this? If so how can it be done?

Thanks

Thanks Duy,
If you want to snapshot very thing. You could snapshot the mapview controller.

       RenderTargetBitmap renderTargetBitmap =
        new RenderTargetBitmap((int)this.Width, (int)this.Height, 96, 96, PixelFormats.Pbgra32);
                renderTargetBitmap.Render(mapView);
                PngBitmapEncoder pngImage = new PngBitmapEncoder();
                pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
                using (Stream fileStream = File.Create("D:\\test.png"))
               {
                    pngImage.Save(fileStream);
                }

If you want to more control for the layers. You can draw each layer one by one.

        GeoImage bitmap = new GeoImage((int)this.Width, (int)this.Height);
        GeoCanvas canvas = GeoCanvas.CreateDefaultGeoCanvas();
        foreach (var layer in (mapView.Overlays["WorldOverlay"] as LayerOverlay).Layers)
        {
            RectangleShape extent = layer.GetBoundingBox();
            canvas.BeginDrawing(bitmap, extent, GeographyUnit.DecimalDegree);
            layer.Draw(canvas, new Collection<SimpleCandidate>());
        }
        canvas.EndDrawing();
        bitmap.Save("D:\\test.png");

You may need adjust the above code base on your project.

Thanks

Frank

Thank you Frank,

I’ve tried your first method to snapshot everything but I just got a blank image (see attached).
Do you have any idea how to fix this?

Duy,
The first method is come from

I created a demo for you.

WpfAppCore.zip (3.9 KB)

You may need wait the layers on the map show up. Then click the snapshot button. If this still not work. Could you send me a demo project to re-produce the issue.

Thanks

Frank

Hi Frank,

I couldn’t open the project you provided. I got error “Project file is incomplete. Expected imports are missing.”
However, I looked at the code and noticed that you used uc1:MapView control. What I’m using currently is ThinkGeo.MapSuite.Wpf.WpfMap. Are they different?

Thanks,
Duy

Hi Frank,

I was able to capture snapshot of the map through clicking the button manually once the map was loaded. Is there a way to detect the map was loaded programmatically and capture snapshot automatically at that moment? In my application, that needs to happen automatically in the background. I’ve tried to hook onto both Loaded and OverlaysDrawn events of the map canvas but can’t get a correct map snapshots. The captured snapshots look like the map still haven’t completely finished rendering yet.

Thanks,
Duy

Thanks Duy,
Because the muti-threading thing. It may difficult to know all tiles drawn on the map. Could you try the second way to do the snapshot. This will create a new canvas to draw the items.

Thanks

Frank