I would like to know if there is a way to do the equivalent of MapView.ExportImageAsync using sdk.net 10.2.7 ? We have to display wmts layers in our application and hence cannot switch to the new release.
Thanks!
Solved! Go to Solution.
I did this once by exporting the canvas, not perfect but it might get the job done for you. I adapted this from here: WPF Diagramming. Saving you canvas to image, XPS document or raw Xaml. | Denis Vuyka
I used a "scale" of 2 for what I was doing, note that it is not the "Map Scale" the method just exports exactly what is visible in the canvas. Hope this helps.
public static void ExportToPng(Uri path, MapView surface, int scale)
{
if (path == null) return;
// Save current canvas transform
Transform transform = surface.LayoutTransform;
// reset current transform (in case it is scaled or rotated)
surface.LayoutTransform = null;
// Get the size of canvas
Size size = new Size(surface.ActualWidth, surface.ActualHeight);
// Measure and arrange the surface
// VERY IMPORTANT
surface.Measure(size);
surface.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
RenderTargetBitmap renderBitmap =
new RenderTargetBitmap(
(int)size.Width * scale,
(int)size.Height * scale,
96d * scale,
96d * scale,
PixelFormats.Pbgra32);
renderBitmap.Render(surface);
// Create a file stream for saving image
using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
{
// Use png encoder for our data
PngBitmapEncoder encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);
}
// Restore previously saved layout
surface.LayoutTransform = transform;
}
I did this once by exporting the canvas, not perfect but it might get the job done for you. I adapted this from here: WPF Diagramming. Saving you canvas to image, XPS document or raw Xaml. | Denis Vuyka
I used a "scale" of 2 for what I was doing, note that it is not the "Map Scale" the method just exports exactly what is visible in the canvas. Hope this helps.
public static void ExportToPng(Uri path, MapView surface, int scale)
{
if (path == null) return;
// Save current canvas transform
Transform transform = surface.LayoutTransform;
// reset current transform (in case it is scaled or rotated)
surface.LayoutTransform = null;
// Get the size of canvas
Size size = new Size(surface.ActualWidth, surface.ActualHeight);
// Measure and arrange the surface
// VERY IMPORTANT
surface.Measure(size);
surface.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
RenderTargetBitmap renderBitmap =
new RenderTargetBitmap(
(int)size.Width * scale,
(int)size.Height * scale,
96d * scale,
96d * scale,
PixelFormats.Pbgra32);
renderBitmap.Render(surface);
// Create a file stream for saving image
using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
{
// Use png encoder for our data
PngBitmapEncoder encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);
}
// Restore previously saved layout
surface.LayoutTransform = transform;
}
Thanks. It works great.