/// <summary> /// Gets the various export parameters for the chosen format /// </summary> private void AddExportParameters(PixelImageExporter pixelImageExport, string format) { if (format.Equals("image/jpeg")) { // get jpeg quality pixelImageExport.Quality = (double)Handler.GetValue(JPG, QUALITY); } else if (format.Equals("image/tiff")) { // get tiff compression string compressionChoice = (string)Handler.GetValue(TIFF, COMPRESSION); pixelImageExport.Compression = !compressionChoice.Equals(COMPRESSION_NONE) ? TiffCompressOption.Default : TiffCompressOption.None; } }
/// <summary> /// Gets the various export parameters for the chosen format /// </summary> private void AddExportParameters(PixelImageExporter pixelImageExport, string format) { if (format.Equals("image/jpeg")) { // get jpeg quality pixelImageExport.Quality = (double)Handler.GetValue(JPG, QUALITY); } else if (format.Equals("image/tiff")) { // get tiff color depth int colorDepthChoice = (int)Handler.GetValue(TIFF, COLOR_DEPTH); pixelImageExport.ColorDepth = ColorDepthList[colorDepthChoice]; // get tiff compression string compressionChoice = (string)Handler.GetValue(TIFF, COMPRESSION); pixelImageExport.Compression = !compressionChoice.Equals(COMPRESSION_NONE); } pixelImageExport.SmoothingMode = (SmoothingMode)Handler.GetValue(GRAPHICS, SMOOTHING); pixelImageExport.TextRenderingHint = (TextRenderingHint)Handler.GetValue(GRAPHICS, TEXTRENDERING); pixelImageExport.PixelOffsetMode = (PixelOffsetMode)Handler.GetValue(GRAPHICS, PIXELOFFSET); pixelImageExport.InterpolationMode = (InterpolationMode)Handler.GetValue(GRAPHICS, INTERPOLATION); }
private void ImageExportToFile() { GraphControl control = graphControl; // check if the rectangular region or the whole viewport should be printed 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, FlowDirection = graphControl.FlowDirection }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); if (format.Equals("XPS")) { // create the exporter XpsExporter emfImageExporter = new XpsExporter(config); exporter = emfImageExporter; saveFileDialog.Reset(); saveFileDialog.Filter = "XPS files (*.xps)|*.xps"; if (saveFileDialog.ShowDialog(this) == true) { string fileName = saveFileDialog.FileName; if (File.Exists(fileName)) { File.Delete(fileName); } using (FileStream stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite)) { exporter.Export(control, stream); } } } else { var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = control.Background ?? Brushes.White; } exporter = pixelImageExport; saveFileDialog.Reset(); saveFileDialog.Filter = format + "|*." + format.Substring(6); if (saveFileDialog.ShowDialog(this) == true) { FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write); exporter.Export(control, stream); stream.Flush(); stream.Close(); } } }
private ImageSource 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, FlowDirection = graphControl.FlowDirection, Background = graphControl.Background, Projection = graphControl.Projection }; } IImageExporter exporter; ContextConfigurator config = GetConfig(bounds, !useRect); var transparentBackground = (bool)Handler.GetValue(PNG, TRANSPARENT); if (format.Equals("XPS")) { // create the exporter XpsExporter xpsExporter = new XpsExporter(config); string tempFileName = Path.GetTempFileName(); using (var fileStream = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite)) { xpsExporter.Export(control, fileStream); } documentViewer.Visibility = Visibility.Visible; previewCanvas.Visibility = Visibility.Hidden; documentViewer.Document = new XpsDocument(tempFileName, FileAccess.Read).GetFixedDocumentSequence(); return(null); } else { // create the exporter PixelImageExporter pixelImageExport = new PixelImageExporter(config); AddExportParameters(pixelImageExport, format); pixelImageExport.OutputFormat = format; // check if the format is transparent PNG if ((!format.Equals("image/png") || !transparentBackground)) { // if not, set the background color pixelImageExport.Background = control.Background ?? Brushes.White; } exporter = pixelImageExport; var memoryStream = new MemoryStream(); try { exporter.Export(control, memoryStream); // reset the stream memoryStream.Position = 0; // and read back the image for display var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); if (previewCanvas.RootGroup.First != null) { previewCanvas.RootGroup.First.Remove(); } documentViewer.Visibility = Visibility.Hidden; previewCanvas.Visibility = Visibility.Visible; var image = new Image { Source = bitmapImage, Width = bitmapImage.Width, Height = bitmapImage.Height }; image.SetCanvasArrangeRect(new Rect(0, 0, image.Width, image.Height)); previewCanvas.RootGroup.AddChild(image, CanvasObjectDescriptors.Visual); previewCanvas.ContentRect = new RectD(0, 0, bitmapImage.Width, bitmapImage.Height).GetEnlarged(20); return(bitmapImage); } catch (IOException exception) { MessageBox.Show(exception.Message, "I/O Error", MessageBoxButton.OK); return(null); } } }
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); } } }