private void btnRenderMap2_Click(object sender, EventArgs e) { //Note we're casting to mg-desktop specific subclasses as this is what mg-desktop rendering APIs expect MgdMap map = (MgdMap)_viewer.GetMap(); MgdSelection selection = (MgdSelection)_viewer.GetSelection(); MgMapViewerProvider provider = _viewer.GetProvider(); MgdRenderingService renderSvc = (MgdRenderingService)provider.CreateService(MgServiceType.RenderingService); MgPoint centerPt = map.ViewCenter; MgCoordinate centerCoord = centerPt.Coordinate; //MgdTransientMapState is a helper class which lets us apply transient state to a map, which is automatically //undone on disposal. This is how we can render custom views of a map with specific display parameters without //permanently changing the display parameters used by the map viewer using (MgTransientMapState tempState = provider.CreateTransientState(map)) { MgMapDisplayParameters state = new MgMapDisplayParameters(centerCoord.X, centerCoord.Y, 5000, 1024, 768, 96); tempState.PushState(state); MgByteReader br = renderSvc.RenderMap(map, selection, "PNG"); using (SaveFileDialog save = new SaveFileDialog()) { save.Filter = "Portable Network Graphics (*.png)|*.png"; if (save.ShowDialog() == DialogResult.OK) { //MgReadOnlyStream is a stream adapter class that provides a .net stream //interface to the MgByteReader, allowing MgByteReader objects to be used //anywhere a System.IO.Stream is expected. using (MgReadOnlyStream stream = new MgReadOnlyStream(br)) { Image img = Image.FromStream(stream); img.Save(save.FileName); MessageBox.Show("Image saved to: " + save.FileName); } } br.Dispose(); } } }
private static MgdSelection Convert(MgdMap map, Mapping.MapSelection sel) { if (sel == null) return null; MgdSelection impl = new MgdSelection(map); var xml = sel.ToXml(); if (!string.IsNullOrEmpty(xml)) impl.FromXml(xml); return impl; }
private void button1_Click(object sender, EventArgs e) { try { var fact = new MgdServiceFactory(); MgdRenderingService renSvc = (MgdRenderingService)fact.CreateService(MgServiceType.RenderingService); MgResourceIdentifier resId = new MgResourceIdentifier(textBox1.Text); var sw = new Stopwatch(); sw.Start(); MgdMap map = new MgdMap(resId); sw.Stop(); Trace.TraceInformation("Runtime map created in {0}ms", sw.ElapsedMilliseconds); MgdSelection selection = null; if (!string.IsNullOrEmpty(txtSelectLayer.Text) && !string.IsNullOrEmpty(txtSelectFilter.Text)) { selection = new MgdSelection(map); var layers = map.GetLayers(); if (layers.IndexOf(txtSelectLayer.Text) >= 0) { var layer = layers.GetItem(txtSelectLayer.Text); var query = new MgFeatureQueryOptions(); query.SetFilter(txtSelectFilter.Text); var reader = layer.SelectFeatures(query); selection.AddFeatures(layer, reader, 0); Trace.TraceInformation("{0} features in selection set", selection.GetSelectedFeaturesCount(layer, layer.GetFeatureClassName())); } } double x; double y; if (double.TryParse(textBox2.Text, out x) && double.TryParse(textBox3.Text, out y)) { map.SetViewCenterXY(x, y); } int w; int h; if (int.TryParse(txtWidth.Text, out w) && int.TryParse(txtHeight.Text, out h)) { map.SetDisplaySize(w, h); } double scale; if (double.TryParse(textBox4.Text, out scale)) { map.SetViewScale(scale); } int mode; if (int.TryParse(textBox6.Text, out mode)) { } MgColor selColor = new MgColor("0x0000FFFF"); MgdRenderingOptions renderOpts = new MgdRenderingOptions(textBox5.Text, mode, selColor); sw.Start(); MgByteReader response = renSvc.RenderDynamicOverlay(map, selection, renderOpts); sw.Stop(); Trace.TraceInformation("RenderDynamicOverlay executed in {0}ms", sw.ElapsedMilliseconds); new ImageResponseDialog(response).ShowDialog(); } catch (MgException ex) { MessageBox.Show(ex.ToString(), "Error from MapGuide"); } }