Export.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.IO;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.Xml;
  7. using System.Windows.Media.Imaging;
  8. using System.Text.Json;
  9. using System.Text;
  10. using System.Web.Script.Serialization;
  11. namespace DrawGraph
  12. {
  13. public class Export
  14. {
  15. public static void ToPng(Canvas canvas, string path)
  16. {
  17. RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
  18. (int)canvas.Width, (int)canvas.Height,
  19. 96d, 96d, PixelFormats.Pbgra32);
  20. canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
  21. canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
  22. renderBitmap.Render(canvas);
  23. //JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  24. PngBitmapEncoder encoder = new PngBitmapEncoder();
  25. encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  26. using (FileStream fs = new FileStream(path, FileMode.Create))
  27. {
  28. encoder.Save(fs);
  29. }
  30. }
  31. public static void ToJpeg(Canvas canvas, string path)
  32. {
  33. RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
  34. (int)canvas.Width, (int)canvas.Height,
  35. 96d, 96d, PixelFormats.Pbgra32);
  36. canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
  37. canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
  38. renderBitmap.Render(canvas);
  39. JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  40. encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  41. using (FileStream fs = new FileStream(path, FileMode.Create))
  42. {
  43. encoder.Save(fs);
  44. }
  45. }
  46. public static void Print(Canvas canvas)
  47. {
  48. PrintDialog pd = new PrintDialog();
  49. if (pd.ShowDialog() == true)
  50. {
  51. pd.PrintVisual(canvas, "Printed with DrawGraph");
  52. }
  53. }
  54. public static void ToGraphML(Node[] nodes, bool direction, string path)
  55. {
  56. XmlDocument doc = new XmlDocument();
  57. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  58. var root = doc.CreateElement("graph");
  59. var attr = doc.CreateAttribute("id");
  60. attr.InnerText = "G";
  61. root.Attributes.Append(attr);
  62. var dir = doc.CreateAttribute("edgedefault");
  63. dir.InnerText = direction ? "directed" : "undirected";
  64. root.Attributes.Append(dir);
  65. doc.AppendChild(declaration);
  66. doc.AppendChild(root);
  67. XmlProcessingInstruction pi =
  68. doc.CreateProcessingInstruction("graphml", "xmlns=\"http://graphml.graphdrawing.org/xmlns \" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance \" xsi: schemaLocation = \"http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd \"");
  69. doc.InsertBefore(pi, doc.ChildNodes[1]);
  70. for (int i = 0; i < nodes.Length; i++)
  71. {
  72. XmlNode Node = doc.CreateElement("node");
  73. var attribute = doc.CreateAttribute("id");
  74. attribute.InnerText = "n" + i.ToString();
  75. Node.Attributes.Append(attribute);
  76. root.AppendChild(Node);
  77. }
  78. doc.Save(path);
  79. }
  80. public static void ToGraphML(Node[] nodes, Edge[] edges, bool direction, string path)
  81. {
  82. XmlDocument doc = new XmlDocument();
  83. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  84. var root = doc.CreateElement("graph");
  85. var attr = doc.CreateAttribute("id");
  86. attr.InnerText = "G";
  87. root.Attributes.Append(attr);
  88. var dir = doc.CreateAttribute("edgedefault");
  89. dir.InnerText = direction ? "directed" : "undirected";
  90. root.Attributes.Append(dir);
  91. doc.AppendChild(declaration);
  92. doc.AppendChild(root);
  93. XmlProcessingInstruction pi =
  94. doc.CreateProcessingInstruction("graphml", "xmlns=\"http://graphml.graphdrawing.org/xmlns \" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance \" xsi: schemaLocation = \"http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd \"");
  95. doc.InsertBefore(pi, doc.ChildNodes[1]);
  96. for (int i = 0; i < nodes.Length; i++)
  97. {
  98. XmlNode node = doc.CreateElement("node");
  99. var attribute = doc.CreateAttribute("id");
  100. attribute.InnerText = "n" + i.ToString();
  101. node.Attributes.Append(attribute);
  102. var XCoord = doc.CreateAttribute("positionX");
  103. XCoord.InnerText = nodes[i].X.ToString();
  104. node.Attributes.Append(XCoord);
  105. var YCoord = doc.CreateAttribute("positionY");
  106. YCoord.InnerText = nodes[i].Y.ToString();
  107. node.Attributes.Append(YCoord);
  108. root.AppendChild(node);
  109. }
  110. for (int i = 0; i < edges.Length; i++)
  111. {
  112. XmlNode edge = doc.CreateElement("edge");
  113. var sourceAttribute = doc.CreateAttribute("source");
  114. for (int j = 0; j < nodes.Length; j++)
  115. if (Node.Compare(edges[i].SourceNode, nodes[j]))
  116. sourceAttribute.InnerText = "n" + j.ToString();
  117. edge.Attributes.Append(sourceAttribute);
  118. var targetAttribute = doc.CreateAttribute("target");
  119. for (int j = 0; j < nodes.Length; j++)
  120. if (Node.Compare(edges[i].TargetNode, nodes[j]))
  121. targetAttribute.InnerText = "n" + j.ToString();
  122. edge.Attributes.Append(targetAttribute);
  123. if (edges[i].Weight > 0)
  124. {
  125. var weightAttr = doc.CreateAttribute("weight");
  126. weightAttr.InnerText = edges[i].Weight.ToString();
  127. edge.Attributes.Append(weightAttr);
  128. }
  129. root.AppendChild(edge);
  130. }
  131. doc.Save(path);
  132. }
  133. public static void ToJSON(Node[] nodes, Edge[] edges, bool direction, string path)
  134. {
  135. if (edges is null)
  136. {
  137. throw new ArgumentNullException(nameof(edges));
  138. }
  139. JavaScriptSerializer serializer = new JavaScriptSerializer();
  140. var json = serializer.Serialize(nodes);
  141. File.WriteAllText(path, json);
  142. }
  143. }
  144. }