Export.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. namespace DrawGraph
  11. {
  12. public class Export
  13. {
  14. public static void ToPng(Canvas canvas, string path)
  15. {
  16. RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
  17. (int)canvas.Width, (int)canvas.Height,
  18. 96d, 96d, PixelFormats.Pbgra32);
  19. canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
  20. canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
  21. renderBitmap.Render(canvas);
  22. //JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  23. PngBitmapEncoder encoder = new PngBitmapEncoder();
  24. encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  25. using (FileStream fs = new FileStream(path, FileMode.Create))
  26. {
  27. encoder.Save(fs);
  28. }
  29. }
  30. public static void ToJpeg(Canvas canvas, string path)
  31. {
  32. RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
  33. (int)canvas.Width, (int)canvas.Height,
  34. 96d, 96d, PixelFormats.Pbgra32);
  35. canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
  36. canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
  37. renderBitmap.Render(canvas);
  38. JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  39. encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  40. using (FileStream fs = new FileStream(path, FileMode.Create))
  41. {
  42. encoder.Save(fs);
  43. }
  44. }
  45. public static void Print(Canvas canvas)
  46. {
  47. PrintDialog pd = new PrintDialog();
  48. if (pd.ShowDialog() == true)
  49. {
  50. pd.PrintVisual(canvas, "Printed with DrawGraph");
  51. }
  52. }
  53. public static void ToGraphML(Node[] nodes, bool direction, string path)
  54. {
  55. XmlDocument doc = new XmlDocument();
  56. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  57. var root = doc.CreateElement("graph");
  58. var attr = doc.CreateAttribute("id");
  59. attr.InnerText = "G";
  60. root.Attributes.Append(attr);
  61. var dir = doc.CreateAttribute("edgedefault");
  62. dir.InnerText = direction ? "directed" : "undirected";
  63. root.Attributes.Append(dir);
  64. doc.AppendChild(declaration);
  65. doc.AppendChild(root);
  66. XmlProcessingInstruction pi =
  67. 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 \"");
  68. doc.InsertBefore(pi, doc.ChildNodes[1]);
  69. for (int i = 0; i < nodes.Length; i++)
  70. {
  71. XmlNode Node = doc.CreateElement("node");
  72. var attribute = doc.CreateAttribute("id");
  73. attribute.InnerText = "n" + i.ToString();
  74. Node.Attributes.Append(attribute);
  75. root.AppendChild(Node);
  76. }
  77. doc.Save(path);
  78. }
  79. public static void ToGraphML(Node[] nodes, Edge[] edges, bool direction, string path)
  80. {
  81. XmlDocument doc = new XmlDocument();
  82. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  83. var root = doc.CreateElement("graph");
  84. var attr = doc.CreateAttribute("id");
  85. attr.InnerText = "G";
  86. root.Attributes.Append(attr);
  87. var dir = doc.CreateAttribute("edgedefault");
  88. dir.InnerText = direction ? "directed" : "undirected";
  89. root.Attributes.Append(dir);
  90. doc.AppendChild(declaration);
  91. doc.AppendChild(root);
  92. XmlProcessingInstruction pi =
  93. 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 \"");
  94. doc.InsertBefore(pi, doc.ChildNodes[1]);
  95. for (int i = 0; i < nodes.Length; i++)
  96. {
  97. XmlNode node = doc.CreateElement("node");
  98. var attribute = doc.CreateAttribute("id");
  99. attribute.InnerText = "n" + i.ToString();
  100. node.Attributes.Append(attribute);
  101. var XCoord = doc.CreateAttribute("positionX");
  102. XCoord.InnerText = nodes[i].X.ToString();
  103. node.Attributes.Append(XCoord);
  104. var YCoord = doc.CreateAttribute("positionY");
  105. YCoord.InnerText = nodes[i].Y.ToString();
  106. node.Attributes.Append(YCoord);
  107. root.AppendChild(node);
  108. }
  109. for (int i = 0; i < edges.Length; i++)
  110. {
  111. XmlNode edge = doc.CreateElement("edge");
  112. var sourceAttribute = doc.CreateAttribute("source");
  113. for (int j = 0; j < nodes.Length; j++)
  114. if (Node.Compare(edges[i].SourceNode, nodes[j]))
  115. sourceAttribute.InnerText = "n" + j.ToString();
  116. edge.Attributes.Append(sourceAttribute);
  117. var targetAttribute = doc.CreateAttribute("target");
  118. for (int j = 0; j < nodes.Length; j++)
  119. if (Node.Compare(edges[i].TargetNode, nodes[j]))
  120. targetAttribute.InnerText = "n" + j.ToString();
  121. edge.Attributes.Append(targetAttribute);
  122. if (edges[i].Weight > 0)
  123. {
  124. var weightAttr = doc.CreateAttribute("weight");
  125. weightAttr.InnerText = edges[i].Weight.ToString();
  126. edge.Attributes.Append(weightAttr);
  127. }
  128. root.AppendChild(edge);
  129. }
  130. doc.Save(path);
  131. }
  132. public static void ToJSON(
  133. Node[] nodes, Edge[] edges, bool direction, string path)
  134. {
  135. if (edges is null)
  136. {
  137. throw new ArgumentNullException(nameof(edges));
  138. }
  139. JsonSerializerOptions options = new JsonSerializerOptions()
  140. {
  141. AllowTrailingCommas = true,
  142. IgnoreNullValues = false,
  143. PropertyNameCaseInsensitive = true
  144. };
  145. Type type = typeof(Node);
  146. string[] json = new string[nodes.Length];
  147. for(int i = 0; i < nodes.Length; i++)
  148. {
  149. json[i] = JsonSerializer.Serialize(nodes[i], type, options);
  150. if (File.Exists(path))
  151. {
  152. using(FileStream fs = new FileStream(path, FileMode.Append))
  153. {
  154. byte[] Byte = Encoding.ASCII.GetBytes(json[i]);
  155. fs.Write(Byte, 0, Byte.Length);
  156. }
  157. }
  158. else
  159. {
  160. using(FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
  161. {
  162. path += "graph.json";
  163. byte[] Byte = Encoding.ASCII.GetBytes(json[i]);
  164. fs.Write(Byte, 0, Byte.Length);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }