/// <summary> /// Draws the given cells using a Graphics2D canvas and returns the buffered image /// that represents the cells. /// </summary> public static mxICanvas DrawCells(mxGraph graph, Object[] cells, double scale, mxRectangle clip, CanvasFactory factory) { mxICanvas canvas = null; if (cells == null) { cells = new Object[] { graph.Model.Root }; } if (cells != null) { // Gets the current state of the view mxGraphView view = graph.View; Dictionary <Object, mxCellState> states = view.States; double oldScale = view.Scale; // Keeps the existing translation as the cells might // be aligned to the grid in a different way in a graph // that has a translation other than zero bool eventsEnabled = view.IsEventsEnabled; // Disables firing of scale events so that there is no // repaint or update of the original graph view.IsEventsEnabled = false; try { // TODO: Factor-out into mxTemporaryCellStates class view.States = new Dictionary <Object, mxCellState>(); view.Scale = scale; // Validates the vertices and edges without adding them to // the model so that the original cells are not modified for (int i = 0; i < cells.Length; i++) { view.ValidateCellState(view.ValidateCell(cells[i])); } if (clip == null) { clip = graph.GetPaintBounds(cells); } if (clip != null && clip.Width > 0 && clip.Height > 0) { Rectangle rect = clip.GetRectangle(); canvas = factory.CreateCanvas(rect.Width + 1, rect.Height + 1); if (canvas != null) { double previousScale = canvas.Scale; Point previousTranslate = canvas.Translate; try { canvas.Translate = new Point(-rect.X, -rect.Y); canvas.Scale = view.Scale; for (int i = 0; i < cells.Length; i++) { graph.DrawCell(canvas, cells[i]); } } finally { canvas.Scale = previousScale; canvas.Translate = previousTranslate; } } } } finally { view.Scale = oldScale; view.States = states; view.IsEventsEnabled = eventsEnabled; } } return(canvas); }