private void saveButton_Click(object sender, EventArgs e) { // Choose file SaveFileDialog saveFileDialog = new SaveFileDialog() { Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png" }; var result = saveFileDialog.ShowDialog(); if (result == DialogResult.Abort) { return; } // Create formula object from input text. WpfMath.TexFormula formula = ParseFormula(inputTextBox.Text); if (formula == null) { return; } var renderer = formula.GetRenderer(WpfMath.TexStyle.Display, this.formula.Scale, "Arial"); // Open stream var filename = saveFileDialog.FileName; using (var stream = new FileStream(filename, FileMode.Create)) { switch (saveFileDialog.FilterIndex) { case 1: var geometry = renderer.RenderToGeometry(0, 0); var converter = new WpfMath.SVGConverter(); var svgPathText = converter.ConvertGeometry(geometry); var svgText = AddSVGHeader(svgPathText); using (var writer = new StreamWriter(stream)) writer.WriteLine(svgText); break; case 2: var bitmap = renderer.RenderToBitmap(0, 0); var encoder = new PngBitmapEncoder { Frames = { BitmapFrame.Create(bitmap) } }; encoder.Save(stream); break; default: return; } } }
private void saveSVG(string input, string filePath, string title) { //string imagesFolder = Path.Combine(imageFolderPath, "images"); string imagesFolder = imageFolderPath; if (!File.Exists(imagesFolder)) { Directory.CreateDirectory(imagesFolder); } //Console.WriteLine(filePath); string currentDic = Directory.GetCurrentDirectory(); System.IO.DirectoryInfo di = new DirectoryInfo(imagesFolder); //foreach (FileInfo file in di.GetFiles()) //{ // file.Delete(); //} //foreach (DirectoryInfo dir in di.GetDirectories()) //{ // dir.Delete(true); //} //while (di.GetDirectories().Length != 0 || di.GetFiles().Length != 0) //{ // var result = System.Windows.Forms.MessageBox.Show("Could not delete all files in temp folder", "Error Title", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation); // if (result == DialogResult.Retry) // { // foreach (FileInfo file in di.GetFiles()) // { // file.Delete(); // } // foreach (DirectoryInfo dir in di.GetDirectories()) // { // dir.Delete(true); // } // } // else // { // return; // } //} Directory.SetCurrentDirectory(imagesFolder); if (!Directory.Exists(imageFolderPath)) { Directory.CreateDirectory(imageFolderPath); } //string imagesFolder = Path.Combine(imageFolderPath, "images"); string imagePath = Path.Combine(imagesFolder, title + ".svg"); if (File.Exists(imagePath)) { //DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Formula with same title already exists in the document. Should the file be overwritten?", "Overwrite file", MessageBoxButtons.YesNo); DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(Resource_MessageBox.formulaOverwriteContent, Resource_MessageBox.formulaOverwriteTitle, MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { //System.IO.File.Delete(svgFile); //System.IO.File.Copy(svgFile, imagePath, true); } else if (dialogResult == DialogResult.No) { return; } } svgFile = imagePath; WpfMath.TexFormula formula = ParseFormula(input); if (formula == null) { return; } var renderer = formula.GetRenderer(WpfMath.TexStyle.Display, this.formula.Scale, "Arial"); // Open stream //var pngFilename = Path.Combine(imagesFolder, titleTextBox.Text + ".png"); //var svgFilename = Path.Combine(imagesFolder, titleTextBox.Text + ".svg"); //svgFile = svgFilename; //pngFile = pngFilename; //using (var stream = new FileStream(pngFilename, FileMode.Create)) //{ // //var geometry = renderer.RenderToGeometry(0, 0); // //var converter = new WpfMath.SVGConverter(); // //var svgPathText = converter.ConvertGeometry(geometry); // //var svgText = this.AddSVGHeader(svgPathText); // //using (var writer = new StreamWriter(stream)) // // writer.WriteLine(svgText); // var bitmap = renderer.RenderToBitmap(0, 0); // var encoder = new PngBitmapEncoder // { // Frames = { BitmapFrame.Create(bitmap) } // }; // encoder.Save(stream); //} using (var stream = new FileStream(svgFile, FileMode.Create)) { var geometry = renderer.RenderToGeometry(0, 0); var converter = new WpfMath.SVGConverter(); var svgPathText = converter.ConvertGeometry(geometry); var svgText = this.AddSVGHeader(svgPathText); using (var writer = new StreamWriter(stream)) writer.WriteLine(svgText); } Directory.SetCurrentDirectory(currentDic); }