123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using System.IO;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Xml;
- using System.Windows.Media.Imaging;
- using System.Text.Json;
- using System.Text;
- namespace DrawGraph
- {
- public class Export
- {
- public static void ToPng(Canvas canvas, string path)
- {
- RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
- (int)canvas.Width, (int)canvas.Height,
- 96d, 96d, PixelFormats.Pbgra32);
- canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
- canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
- renderBitmap.Render(canvas);
- //JpegBitmapEncoder encoder = new JpegBitmapEncoder();
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
- using (FileStream fs = new FileStream(path, FileMode.Create))
- {
- encoder.Save(fs);
- }
- }
- public static void ToJpeg(Canvas canvas, string path)
- {
- RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
- (int)canvas.Width, (int)canvas.Height,
- 96d, 96d, PixelFormats.Pbgra32);
- canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
- canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
- renderBitmap.Render(canvas);
- JpegBitmapEncoder encoder = new JpegBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
- using (FileStream fs = new FileStream(path, FileMode.Create))
- {
- encoder.Save(fs);
- }
- }
- public static void Print(Canvas canvas)
- {
- PrintDialog pd = new PrintDialog();
- if (pd.ShowDialog() == true)
- {
- pd.PrintVisual(canvas, "Printed with DrawGraph");
- }
- }
- public static void ToGraphML(Node[] nodes, bool direction, string path)
- {
- XmlDocument doc = new XmlDocument();
- XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
- var root = doc.CreateElement("graph");
- var attr = doc.CreateAttribute("id");
- attr.InnerText = "G";
- root.Attributes.Append(attr);
- var dir = doc.CreateAttribute("edgedefault");
- dir.InnerText = direction ? "directed" : "undirected";
- root.Attributes.Append(dir);
- doc.AppendChild(declaration);
- doc.AppendChild(root);
- XmlProcessingInstruction pi =
- 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 \"");
- doc.InsertBefore(pi, doc.ChildNodes[1]);
- for (int i = 0; i < nodes.Length; i++)
- {
- XmlNode Node = doc.CreateElement("node");
- var attribute = doc.CreateAttribute("id");
- attribute.InnerText = "n" + i.ToString();
- Node.Attributes.Append(attribute);
- root.AppendChild(Node);
- }
- doc.Save(path);
- }
- public static void ToGraphML(Node[] nodes, Edge[] edges, bool direction, string path)
- {
- XmlDocument doc = new XmlDocument();
- XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
- var root = doc.CreateElement("graph");
- var attr = doc.CreateAttribute("id");
- attr.InnerText = "G";
- root.Attributes.Append(attr);
- var dir = doc.CreateAttribute("edgedefault");
- dir.InnerText = direction ? "directed" : "undirected";
- root.Attributes.Append(dir);
- doc.AppendChild(declaration);
- doc.AppendChild(root);
- XmlProcessingInstruction pi =
- 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 \"");
- doc.InsertBefore(pi, doc.ChildNodes[1]);
- for (int i = 0; i < nodes.Length; i++)
- {
- XmlNode node = doc.CreateElement("node");
- var attribute = doc.CreateAttribute("id");
- attribute.InnerText = "n" + i.ToString();
- node.Attributes.Append(attribute);
- var XCoord = doc.CreateAttribute("positionX");
- XCoord.InnerText = nodes[i].X.ToString();
- node.Attributes.Append(XCoord);
- var YCoord = doc.CreateAttribute("positionY");
- YCoord.InnerText = nodes[i].Y.ToString();
- node.Attributes.Append(YCoord);
- root.AppendChild(node);
- }
- for (int i = 0; i < edges.Length; i++)
- {
- XmlNode edge = doc.CreateElement("edge");
- var sourceAttribute = doc.CreateAttribute("source");
- for (int j = 0; j < nodes.Length; j++)
- if (Node.Compare(edges[i].SourceNode, nodes[j]))
- sourceAttribute.InnerText = "n" + j.ToString();
- edge.Attributes.Append(sourceAttribute);
- var targetAttribute = doc.CreateAttribute("target");
- for (int j = 0; j < nodes.Length; j++)
- if (Node.Compare(edges[i].TargetNode, nodes[j]))
- targetAttribute.InnerText = "n" + j.ToString();
- edge.Attributes.Append(targetAttribute);
- if (edges[i].Weight > 0)
- {
- var weightAttr = doc.CreateAttribute("weight");
- weightAttr.InnerText = edges[i].Weight.ToString();
- edge.Attributes.Append(weightAttr);
- }
- root.AppendChild(edge);
- }
- doc.Save(path);
- }
- public static void ToJSON(
- Node[] nodes, Edge[] edges, bool direction, string path)
- {
- if (edges is null)
- {
- throw new ArgumentNullException(nameof(edges));
- }
- JsonSerializerOptions options = new JsonSerializerOptions()
- {
- AllowTrailingCommas = true,
- IgnoreNullValues = false,
- PropertyNameCaseInsensitive = true
- };
- Type type = typeof(Node);
- string[] json = new string[nodes.Length];
- for(int i = 0; i < nodes.Length; i++)
- {
- json[i] = JsonSerializer.Serialize(nodes[i], type, options);
- if (File.Exists(path))
- {
- using(FileStream fs = new FileStream(path, FileMode.Append))
- {
- byte[] Byte = Encoding.ASCII.GetBytes(json[i]);
- fs.Write(Byte, 0, Byte.Length);
- }
- }
- else
- {
-
- using(FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
- {
- path += "graph.json";
- byte[] Byte = Encoding.ASCII.GetBytes(json[i]);
- fs.Write(Byte, 0, Byte.Length);
- }
- }
- }
- }
- }
- }
|