/// <summary> /// Exports the specified plot model to a xml writer. /// </summary> /// <param name="model">The model.</param> /// <param name="writer">The xml writer.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> private static void Export(IPlotModel model, XmlWriter writer, double width, double height) { var c = new Canvas(); if (model.Background.IsVisible()) { c.Background = model.Background.ToBrush(); } c.Measure(new Size(width, height)); c.Arrange(new Rect(0, 0, width, height)); var rc = new CanvasRenderContext(c) { UseStreamGeometry = false }; rc.TextFormattingMode = TextFormattingMode.Ideal; model.Update(true); model.Render(rc, new OxyRect(0, 0, width, height)); c.UpdateLayout(); XamlWriter.Save(c, writer); }
/// <summary> /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) /// call <see cref="M:System.Windows.Controls.Control.ApplyTemplate" /> . In simplest terms, this means the method is called /// just before a UI element displays in an application. For more information, see Remarks. /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); this.grid = this.GetTemplateChild(PartGrid) as Grid; if (this.grid == null) { return; } this.canvas = new Canvas(); this.grid.Children.Add(this.canvas); this.canvas.UpdateLayout(); this.renderContext = new CanvasRenderContext(this.canvas); this.overlays = new Canvas(); this.grid.Children.Add(this.overlays); this.zoomControl = new ContentControl(); this.overlays.Children.Add(this.zoomControl); // add additional grid on top of everthing else to fix issue of mouse events getting lost // it must be added last so it covers all other controls var mouseGrid = new Grid(); mouseGrid.Background = Brushes.Transparent; // background must be set for hit test to work this.grid.Children.Add(mouseGrid); }
/// <summary> /// Write the specified <see cref="IPlotModel" /> to the specified <see cref="XpsDocumentWriter" />. /// </summary> /// <param name="model">The model.</param> /// <param name="writer">The document writer.</param> private void Write(IPlotModel model, XpsDocumentWriter writer) { var canvas = new Canvas { Width = this.Width, Height = this.Height, Background = this.Background.ToBrush() }; canvas.Measure(new Size(this.Width, this.Height)); canvas.Arrange(new Rect(0, 0, this.Width, this.Height)); var rc = new CanvasRenderContext(canvas); rc.TextFormattingMode = this.TextFormattingMode; model.Update(true); model.Render(rc, this.Width, this.Height); canvas.UpdateLayout(); writer.Write(canvas); }
/// <summary> /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) /// call <see cref="M:System.Windows.Controls.Control.ApplyTemplate" /> . In simplest terms, this means the method is called /// just before a UI element displays in an application. For more information, see Remarks. /// </summary> public override void OnApplyTemplate() { base.OnApplyTemplate(); this.grid = this.GetTemplateChild(PartGrid) as Grid; if (this.grid == null) { return; } this.canvas = new Canvas(); this.grid.Children.Add(this.canvas); this.canvas.UpdateLayout(); this.renderContext = new CanvasRenderContext(this.canvas); this.overlays = new Canvas(); this.grid.Children.Add(this.overlays); this.zoomControl = new ContentControl(); this.overlays.Children.Add(this.zoomControl); }
/// <summary> /// Exports the specified plot model to a bitmap. /// </summary> /// <param name="model">The model to export.</param> /// <returns>A bitmap.</returns> public BitmapSource ExportToBitmap(IPlotModel model) { var scale = 96d / this.Resolution; var canvas = new Canvas { Width = this.Width * scale, Height = this.Height * scale, Background = model.Background.ToBrush() }; canvas.Measure(new Size(canvas.Width, canvas.Height)); canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height)); var rc = new CanvasRenderContext(canvas) { RendersToScreen = false }; rc.TextFormattingMode = TextFormattingMode.Ideal; rc.DpiScale = this.Resolution / 96; model.Update(true); model.Render(rc, new OxyRect(0, 0, canvas.Width, canvas.Height)); canvas.UpdateLayout(); var bmp = new RenderTargetBitmap(this.Width, this.Height, this.Resolution, this.Resolution, PixelFormats.Pbgra32); bmp.Render(canvas); return(bmp); // alternative implementation: // http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx // var dv = new DrawingVisual(); // using (var ctx = dv.RenderOpen()) // { // var vb = new VisualBrush(canvas); // ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height))); // } // bmp.Render(dv); }