Conectar Graphviz con C# en una aplicación GUI
Preliminares.
Tener instalado graphviz y estar seguro que la variable de entorno está creada.
Pasos:
Crear una carpeta en el proyecto, to recomiendo ponerle "external".
Abrir la ruta bin, del graphvix, en mi caso F:\Program Files (x86)\Graphviz 2.44.1\bin
Seleccionar toda lo que contiene el bin y arrastarlo al IDE (a la carpeta que hemos creeado)
En el IDE, seleccionar todos los elementos en la carpeta "external" y dirigirse a la sección de "propiedades" y en la sección de copy poner >>always copy<<
Click derecho en la carpeta external y add, luego add existing item y nos abrirá una ventana para seleccionar archivos, tenemos que irnos a la carpeta external y seleccionar todo.
Crear la clase que se encargue de generar los gráficos (ver la sección de abajo).
CLASE ENCARGADA DE HACER EL BITMAP
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace graphviz_cSharp.external
{
public static class FileDotEngine
{
public static Bitmap Run(string dot)
{
string executable = @".\external\dot.exe";
string output = @".\external\tempgraph";
File.WriteAllText(output, dot);
System.Diagnostics.Process process = new System.Diagnostics.Process();
// Stop the process from opening a new window
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Setup executable and parameters
process.StartInfo.FileName = executable;
process.StartInfo.Arguments = string.Format(@"{0} -Tjpg -O", output);
// Go
process.Start();
// and wait dot.exe to complete and exit
process.WaitForExit();
Bitmap bitmap = null; ;
using (Stream bmpStream = System.IO.File.Open(output + ".jpg", System.IO.FileMode.Open))
{
Image image = Image.FromStream(bmpStream);
bitmap = new Bitmap(image);
}
File.Delete(output);
File.Delete(output + ".jpg");
return bitmap;
}
}
}
Método que se encarga de crear la imagen
private void drawButton_Click(object sender, EventArgs e)
{
Archive archiver = new Archive();
this.codeGraphRichTextBox.AppendText("\n}");
this.addNode.Enabled = false;
this.mergeNodes.Enabled = false;
this.saveFileDialog1.RestoreDirectory = true;
if (archiver.saveAsArchive(this.saveFileDialog1, this.codeGraphRichTextBox.Text))
{
try {
String fileInputPath = archiver.getPath();
String filename = Path.GetFileName(fileInputPath);
MessageBox.Show(filename);
String graphVizString = @"" + this.codeGraphRichTextBox.Text;
Bitmap bm = FileDotEngine.Run(graphVizString);
for (int x = 0; x < bm.Width; x++) {
for (int y = 0; y < bm.Height; y++) {
Color clr = bm.GetPixel(x,y);
Color newClr = Color.FromArgb(clr.R,0,0);
}
}
bm.Save("C:\\Users\\Daniel\\Desktop\\test\\" + @"graph.jpg");
MessageBox.Show("done");
} catch (Exception exception) {
MessageBox.Show("Error al generar archivos\n"+exception.Message);
}
}
}
Descargar el código presionando aqui Espero haberte ayudado :D
Comentarios
Publicar un comentario