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!
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.
<SPAN class="keyword token">public</SPAN> <SPAN class="keyword token">static</SPAN> <SPAN class="keyword token">void</SPAN> <SPAN class="token function">ExportToPng</SPAN><SPAN class="punctuation token">(</SPAN>Uri path<SPAN class="punctuation token">,</SPAN> MapView surface<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">int</SPAN> scale<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>path <SPAN class="operator token">==</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Save current canvas transform</SPAN> Transform transform <SPAN class="operator token">=</SPAN> surface<SPAN class="punctuation token">.</SPAN>LayoutTransform<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// reset current transform (in case it is scaled or rotated)</SPAN> surface<SPAN class="punctuation token">.</SPAN>LayoutTransform <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Get the size of canvas</SPAN> Size size <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Size</SPAN><SPAN class="punctuation token">(</SPAN>surface<SPAN class="punctuation token">.</SPAN>ActualWidth<SPAN class="punctuation token">,</SPAN> surface<SPAN class="punctuation token">.</SPAN>ActualHeight<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Measure and arrange the surface</SPAN> <SPAN class="comment token">// VERY IMPORTANT</SPAN> surface<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Measure</SPAN><SPAN class="punctuation token">(</SPAN>size<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> surface<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Arrange</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Rect</SPAN><SPAN class="punctuation token">(</SPAN>size<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Create a render bitmap and push the surface to it</SPAN> RenderTargetBitmap renderBitmap <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">RenderTargetBitmap</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">int</SPAN><SPAN class="punctuation token">)</SPAN>size<SPAN class="punctuation token">.</SPAN>Width <SPAN class="operator token">*</SPAN> scale<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">int</SPAN><SPAN class="punctuation token">)</SPAN>size<SPAN class="punctuation token">.</SPAN>Height <SPAN class="operator token">*</SPAN> scale<SPAN class="punctuation token">,</SPAN> 96d <SPAN class="operator token">*</SPAN> scale<SPAN class="punctuation token">,</SPAN> 96d <SPAN class="operator token">*</SPAN> scale<SPAN class="punctuation token">,</SPAN> PixelFormats<SPAN class="punctuation token">.</SPAN>Pbgra32<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> renderBitmap<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Render</SPAN><SPAN class="punctuation token">(</SPAN>surface<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// Create a file stream for saving image</SPAN> <SPAN class="keyword token">using</SPAN> <SPAN class="punctuation token">(</SPAN>FileStream outStream <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">FileStream</SPAN><SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">.</SPAN>LocalPath<SPAN class="punctuation token">,</SPAN> FileMode<SPAN class="punctuation token">.</SPAN>Create<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// Use png encoder for our data</SPAN> PngBitmapEncoder encoder <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">PngBitmapEncoder</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// push the rendered bitmap to it</SPAN> encoder<SPAN class="punctuation token">.</SPAN>Frames<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Add</SPAN><SPAN class="punctuation token">(</SPAN>BitmapFrame<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Create</SPAN><SPAN class="punctuation token">(</SPAN>renderBitmap<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// save the data to the stream</SPAN> encoder<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Save</SPAN><SPAN class="punctuation token">(</SPAN>outStream<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">// Restore previously saved layout</SPAN> surface<SPAN class="punctuation token">.</SPAN>LayoutTransform <SPAN class="operator token">=</SPAN> transform<SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thanks. It works great.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.