/// <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 static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; bool streamToOutput = !string.IsNullOrEmpty(Options.OutputFile) || Options.OutputOnStdOut; if (Options.NoGUI || Options.RequestHelp || streamToOutput) { //if(!Options.IsRunningOnMono) //{ // if (GetConsoleWindow() == IntPtr.Zero) // ; // AllocConsole(); //} if (Options.RequestHelp || string.IsNullOrEmpty(Options.InputFile) || !streamToOutput || Options.RootElements.Count == 0 || Options.ExpandLevel < 0 || Options.Zoom < 10.0 || Options.Zoom > 1000.0) { string version = typeof(Program).Assembly.GetName().Version.ToString(); Log(usage, version, Path.GetFileName(Environment.GetCommandLineArgs()[0])); return; } Log("Loading the file: {0}\n", Options.InputFile); Schema schema = new Schema(); schema.RequestCredential += delegate(string url, string realm, int attemptCount, out string username, out string password) { username = password = ""; if(!string.IsNullOrEmpty(Options.Username)) { if (attemptCount > 1) return false; username = Options.Username; password = Options.Password; return true; } return false; }; schema.LoadSchema(Options.InputFile); if (schema.LoadError.Count > 0) { LogError("There are errors while loading:\n"); foreach (var error in schema.LoadError) { LogError(error); } LogError("\r\n"); } Diagram diagram = new Diagram(); diagram.ShowDocumentation = Options.ShowDocumentation; diagram.ElementsByName = schema.ElementsByName; diagram.Scale = Options.Zoom / 100.0f; foreach (var rootElement in Options.RootElements) { string elementName = rootElement; string elementNamespace = null; if(!string.IsNullOrEmpty(elementName)) { var pos = rootElement.IndexOf("@"); if(pos != -1) { elementName = rootElement.Substring(0, pos); elementNamespace = rootElement.Substring(pos + 1); } } foreach (var element in schema.Elements) { if ((elementNamespace != null && elementNamespace == element.NameSpace && element.Name == elementName) || (elementNamespace == null && element.Name == elementName)) { Log("Adding '{0}' element to the diagram...\n", rootElement); diagram.Add(element.Tag, element.NameSpace); } } } Form form = new Form(); Graphics graphics = form.CreateGraphics(); graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; for (int i = 0; i < Options.ExpandLevel; i++) { Log("Expanding to level {0}...\n", i + 1); diagram.ExpandOneLevel(); } diagram.Layout(graphics); Log("Saving image...\n"); try { bool result = false; DiagramExporter exporter = new DiagramExporter(diagram); IDictionary<string, object> specificRendererParameters = new Dictionary<string, object>() { { "TextOutputFields", Options.TextOutputFields } //For future parameters, {} }; if (Options.OutputOnStdOut) { Stream stream = Console.OpenStandardOutput(); result = exporter.Export(stream, "." + Options.OutputOnStdOutExtension.ToLower(), graphics, new DiagramAlertHandler(ByPassSaveAlert), specificRendererParameters); stream.Flush(); } else { result = exporter.Export(Options.OutputFile, graphics, new DiagramAlertHandler(SaveAlert), specificRendererParameters); } if (result) Log("The diagram is now saved in the file: {0}\n", Options.OutputFile); else Log("ERROR: The diagram has not been saved!\n"); } catch (Exception ex) { Log("ERROR: The diagram has not been saved. {0}\n", ex.Message); } graphics.Dispose(); form.Dispose(); } else { if (Options.RequestHelp) { string version = typeof(Program).Assembly.GetName().Version.ToString(); MessageBox.Show(string.Format(usage, version, Environment.GetCommandLineArgs()[0])); } Application.ThreadException += HandleThreadException; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } }
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); }