/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnPrintPage(object sender, PrintPageEventArgs e) { Graphics graphics = e.Graphics; SmoothingMode enumSmooth = graphics.SmoothingMode; TextRenderingHint enumHint = graphics.TextRenderingHint; CompositingQuality enumQuality = graphics.CompositingQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.CompositingQuality = CompositingQuality.HighQuality; _diagram.Layout(graphics); Size bbSize = _diagram.BoundingBox.Size + _diagram.Padding + _diagram.Padding; Size totalSize = new Size((int)(bbSize.Width * _diagram.Scale), (int)(bbSize.Height * _diagram.Scale)); int columnNumber = 1 + totalSize.Width / e.MarginBounds.Width; int rowNumber = 1 + totalSize.Height / e.MarginBounds.Height; int pageNumber = columnNumber * rowNumber; int row, column = Math.DivRem(_currentPage, rowNumber, out row); Rectangle clipping = new Rectangle(new Point(column * e.MarginBounds.Width, row * e.MarginBounds.Height), new Size((column + 1) * e.MarginBounds.Width, (row + 1) * e.MarginBounds.Height)); graphics.Clip = new Region(e.MarginBounds); //Point virtualPoint = this.panelDiagram.VirtualPoint; graphics.TranslateTransform(-(float)(clipping.Left - e.MarginBounds.Left), -(float)(clipping.Top - e.MarginBounds.Top)); DiagramGdiRenderer.Draw(_diagram, graphics, clipping); if (_currentPage < pageNumber - 1) { _currentPage++; e.HasMorePages = true; } graphics.SmoothingMode = enumSmooth; graphics.TextRenderingHint = enumHint; graphics.CompositingQuality = enumQuality; }
public bool Export(Stream stream, string extension, Graphics referenceGraphics, DiagramAlertHandler alerteDelegate, IDictionary <string, object> specificRendererParameters) { bool result = false; if (extension.Equals(".emf", StringComparison.OrdinalIgnoreCase)) { float scaleSave = _diagram.Scale; try { _diagram.Scale = 1.0f; _diagram.Layout(referenceGraphics); IntPtr hdc = referenceGraphics.GetHdc(); Metafile metafile = new Metafile(stream, hdc); Graphics graphics = Graphics.FromImage(metafile); graphics.SmoothingMode = SmoothingMode.HighQuality; _diagram.Layout(graphics); DiagramGdiRenderer.Draw(_diagram, graphics); referenceGraphics.ReleaseHdc(hdc); metafile.Dispose(); graphics.Dispose(); result = true; } finally { _diagram.Scale = scaleSave; _diagram.Layout(referenceGraphics); } } else if (extension.Equals(".png", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase)) { Rectangle bbox = _diagram.ScaleRectangle(_diagram.BoundingBox); bool bypassAlert = true; if (alerteDelegate != null && (bbox.Width > 10000 || bbox.Height > 10000)) { bypassAlert = alerteDelegate("Huge image generation", String.Format("Do you agree to generate a {0}x{1} image?", bbox.Width, bbox.Height)); } if (bypassAlert) { Bitmap bitmap = new Bitmap(bbox.Width, bbox.Height); Graphics graphics = Graphics.FromImage(bitmap); graphics.FillRectangle(Brushes.White, 0, 0, bbox.Width, bbox.Height); DiagramGdiRenderer.Draw(_diagram, graphics); if (extension.CompareTo(".png") == 0) { bitmap.Save(stream, ImageFormat.Png); } else { bitmap.Save(stream, ImageFormat.Jpeg); } result = true; } } else if (extension.CompareTo(".txt") == 0 || extension.CompareTo(".csv") == 0) { float scaleSave = _diagram.Scale; try { _diagram.Scale = 1.0f; _diagram.Layout(referenceGraphics); using (StreamWriter sw = new StreamWriter(stream)) { using (DiagramTxtRenderer renderer = new DiagramTxtRenderer(sw)) { renderer.IsCSV = extension.CompareTo(".csv") == 0; IDictionary <string, object> parameters = specificRendererParameters as IDictionary <string, object>; object o; if (parameters != null) { if (parameters.TryGetValue("TextOutputFields", out o)) { renderer.TextOutputFields = o as IList <string>; } if (parameters.TryGetValue("DisplayAttributes", out o) && o is bool) { renderer.DisplayAttributes = (bool)o; } if (parameters.TryGetValue("Schema", out o)) { renderer.Schema = o as Schema; } } renderer.Render(_diagram); } sw.Close(); } result = true; } finally { _diagram.Scale = scaleSave; _diagram.Layout(referenceGraphics); } } else //if (extension.CompareTo(".svg") == 0) { float scaleSave = _diagram.Scale; try { _diagram.Scale = 1.0f; _diagram.Layout(referenceGraphics); using (StreamWriter streamWriter = new StreamWriter(stream)) { using (DiagramSvgRenderer renderer = new DiagramSvgRenderer(streamWriter, referenceGraphics)) { renderer.Render(_diagram); } streamWriter.Close(); } result = true; } finally { _diagram.Scale = scaleSave; _diagram.Layout(referenceGraphics); } } return(result); }