private void ImageExportToFile() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be exported bool useRect = (bool)handler.GetValue(OUTPUT, EXPORT_RECTANGLE); RectD bounds = useRect ? exportRect.ToRectD() : control.Viewport; // get the format string formatChoice = (string)Handler.GetValue(OUTPUT, FORMAT); string format = Formats[formatChoice]; // check whether decorations (selection, handles, ...) should be hidden bool hide = (bool)handler.GetValue(OUTPUT, HIDE_DECORATIONS); if (hide) { // if so, create a new graphcontrol whith the same graph control = new GraphControl { Graph = graphControl.Graph }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); if (format.Equals("EMF")) { var transparentBackground = (bool)Handler.GetValue(EMF, TRANSPARENT); // create the exporter EmfImageExporter emfImageExporter = new EmfImageExporter(config); if (!transparentBackground) { emfImageExporter.Background = new SolidBrush(graphControl.BackColor); } exporter = emfImageExporter; saveFileDialog.Reset(); saveFileDialog.Filter = "EMF files (*.emf)|*.emf"; if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write); exporter.Export(control, stream); stream.Flush(); stream.Close(); } } else { // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = new SolidBrush(control.BackColor); } exporter = pixelImageExport; saveFileDialog.Reset(); saveFileDialog.Filter = format + "|*." + format.Substring(6); if (saveFileDialog.ShowDialog(this) == DialogResult.OK) { FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write); try { exporter.Export(control, stream); } catch (IOException exception) { MessageBox.Show(exception.Message, "I/O Error", MessageBoxButtons.OK); } finally { stream.Flush(); stream.Close(); } } } }
private Image ImageExport() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be exported bool useRect = (bool)handler.GetValue(OUTPUT, EXPORT_RECTANGLE); RectD bounds = useRect ? exportRect.ToRectD() : control.Viewport; // get the format string formatChoice = (string)Handler.GetValue(OUTPUT, FORMAT); string format = Formats[formatChoice]; // check whether decorations (selection, handles, ...) should be hidden bool hide = (bool)handler.GetValue(OUTPUT, HIDE_DECORATIONS); if (hide) { // if so, create a new graph control with the same graph control = new GraphControl { Graph = graphControl.Graph, BackColor = graphControl.BackColor, Projection = graphControl.Projection }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); if (format.Equals("EMF")) { var transparentBackground = (bool)Handler.GetValue(EMF, TRANSPARENT); // create the exporter EmfImageExporter emfImageExporter = new EmfImageExporter(config); if (!transparentBackground) { emfImageExporter.Background = new SolidBrush(graphControl.BackColor); } exporter = emfImageExporter; var memoryStream = new MemoryStream(); exporter.Export(control, memoryStream); // reset the stream memoryStream.Position = 0; // and read back the metafile for display return(new Metafile(memoryStream)); } else { // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = new SolidBrush(control.BackColor); } exporter = pixelImageExport; var memoryStream = new MemoryStream(); try { exporter.Export(control, memoryStream); // reset the stream memoryStream.Position = 0; // and read back the image for display return(new Bitmap(memoryStream)); } catch (IOException exception) { MessageBox.Show(exception.Message, "I/O Error", MessageBoxButtons.OK); return(null); } } }