MapView.ExportImageAsync using older version of sdk.net (10.2.7)

1321
2
Jump to solution
12-21-2016 08:23 AM
MarcBaillargeon
New Contributor

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!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
ChristopherMoravec2
New Contributor III

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;
}

View solution in original post

2 Replies
ChristopherMoravec2
New Contributor III

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;
}

View solution in original post

MarcBaillargeon
New Contributor

Thanks. It works great.

0 Kudos