/// <summary> /// Export to an image format /// </summary> public static void ExportSvg(GraphicVisual visual, int width, string filename) { XNamespace ns = "http://www.w3.org/2000/svg"; XElement root = new XElement(ns + "svg"); root.Add(new XAttribute("Version", "1.1")); var normalizer = new NormalizeVisual(); var normalizedVisual = normalizer.Normalize(visual, NormalizeAspect.Width, width); double height = normalizer.AspectRatio * width; root.Add(new XAttribute("viewBox", $"0 0 {width} {height}")); XElement definitions = new XElement(ns + "defs"); int definitionsCount = 0; var element = Generate(normalizedVisual, ns, definitions, ref definitionsCount); if (definitions.HasElements) { root.Add(definitions); } root.Add(element); using (var writer = XmlWriter.Create(filename, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true })) { root.Save(writer); } }
/// <summary> /// Export to EPS /// </summary> public void Export(GraphicVisual visual, int width, string filename, out string message) { outputStream = new StreamWriter(filename, false, Encoding.ASCII); var normalizer = new NormalizeVisual(); var normalizedVisual = normalizer.Normalize(visual, NormalizeAspect.Width, width); double height = normalizer.AspectRatio * width; InitEpsFile(width, height); Generate(normalizedVisual); FinalizeEpsFile(); outputStream.Close(); outputStream.Dispose(); if (nonSupportedOpacityFound) { message = "EPS does not support opacity, opacity ignored"; } else { message = string.Empty; } }
/// <summary> /// Create the xml export structure /// </summary> private static XElement CreateXmlExport(GraphicVisual visual, int width) { XNamespace ns = "http://www.w3.org/2000/svg"; XElement root = new XElement(ns + "svg"); root.Add(new XAttribute("Version", "1.1")); var normalizer = new NormalizeVisual(); var normalizedVisual = normalizer.Normalize(visual, NormalizeAspect.Width, width); double height = normalizer.AspectRatio * width; root.Add(new XAttribute("viewBox", $"0 0 {width} {height}")); XElement definitions = new XElement(ns + "defs"); int definitionsCount = 0; var element = GenerateXmlTree(normalizedVisual, ns, definitions, ref definitionsCount); if (definitions.HasElements) { root.Add(definitions); } root.Add(element); return(root); }
/// <summary> /// Update the XAML source code /// </summary> private void UpdateSourceCode() { var path = selectedVisual; if (path == null) { return; } if (Normalize) { var normalizer = new NormalizeVisual(); path = normalizer.Normalize(selectedVisual, NormalizeAspect.Both, 100); } var geometryGeneratorType = SelectedGeometryType.GeometryGeneratorType; switch (SelectedCodeTypeItem.CodeGeneratorType) { case XamlCodeGeneratorType.DrawingBrush: SourceCode = DrawingBrushSourceGenerator.Generate(path, geometryGeneratorType); break; case XamlCodeGeneratorType.Path: SourceCode = PathSourceGenerator.GeneratePath(path, geometryGeneratorType); break; } }
/// <summary> /// Generates geometry source code /// </summary> public string GenerateSource(GraphicVisual visual, NormalizeGeometrySourceAspect normalizeGeometrySourceAspect, bool includeOffset, string filename) { this.normalizeAspect = normalizeGeometrySourceAspect; this.includeOffset = includeOffset; this.filename = filename; Code = new StringBuilder(); var normalizer = new NormalizeVisual(); NormalizeAspect normalizeType; switch (normalizeAspect) { case NormalizeGeometrySourceAspect.Width: normalizeType = NormalizeAspect.Width; scaleWidthVariableName = "scale"; scaleHeightVariableName = "scale"; break; case NormalizeGeometrySourceAspect.Height: normalizeType = NormalizeAspect.Height; scaleWidthVariableName = "scale"; scaleHeightVariableName = "scale"; break; default: normalizeType = NormalizeAspect.Individual; scaleWidthVariableName = "width"; scaleHeightVariableName = "height"; break; } var normalizedVisual = normalizer.Normalize(visual, normalizeType, 1.0); aspectRatio = normalizer.AspectRatio; InitCode(visual); Init(visual); GenerateSourceRecursive(normalizedVisual); Terminate(); FinalizeCode(); return Code.ToString(); }
/// <summary> /// Update all code generators /// </summary> private void UpdateAll() { if (StreamCode == null || DrawingBrushCode == null || Preview == null) { return; } var path = selectedPath; if (path == null) { Preview.Data = null; StreamCode.Text = string.Empty; DrawingBrushCode.Text = string.Empty; GeometryCode.Text = string.Empty; return; } if (NormalizeCheckBox.IsChecked == true) { var normalizer = new NormalizeVisual(); path = (GraphicPath)normalizer.Normalize(selectedPath, NormalizeAspect.Both, 100); } var xamlStream = StreamSourceGenerator.GeneratePath(path); StreamCode.Text = xamlStream; var drawingBrushSource = DrawingBrushSourceGenerator.Generate(path); DrawingBrushCode.Text = drawingBrushSource; var geometry = GeometryBinaryGenerator.GenerateGeometry(path.Geometry); Preview.Data = geometry; UpdatePreviewAll(); UpdateGeometrySourceCode(); }
/// <summary> /// Generates geometry source code /// </summary> public string GenerateSource(GraphicVisual visual) { code = new StringBuilder(); var normalizer = new NormalizeVisual(); NormalizeAspect normalizeType; switch (NormalizeAspect) { case NormalizeGeometrySourceAspect.Width: normalizeType = Generators.NormalizeAspect.Width; scaleWidthVariableName = "scale"; scaleHeightVariableName = "scale"; break; case NormalizeGeometrySourceAspect.Height: normalizeType = Generators.NormalizeAspect.Height; scaleWidthVariableName = "scale"; scaleHeightVariableName = "scale"; break; default: normalizeType = Generators.NormalizeAspect.Individual; scaleWidthVariableName = "width"; scaleHeightVariableName = "height"; break; } var normalizedVisual = normalizer.Normalize(visual, normalizeType, 1.0); aspectRatio = normalizer.AspectRatio; Init(visual); GenerateSourceRecursive(normalizedVisual); Terminate(); return(code.ToString()); }
/// <summary> /// Update the resource source code /// </summary> private void UpdateSourceCode() { var path = selectedVisual; if (path == null) { SourceCode = string.Empty; return; } if (Normalize) { var normalizer = new NormalizeVisual(); path = normalizer.Normalize(selectedVisual, NormalizeAspect.Both, 100); } switch (SelectedGeometryTypeItem.GeometryGeneratorType) { case ResourceGeometryGeneratorType.Stream: { var streams = StreamSourceGenerator.GenerateStreamGeometries(path); SourceCode = string.Join("\n", streams); break; } case ResourceGeometryGeneratorType.Geometry: { SourceCode = GeometrySourceGenerator.GenerateGeometry(path); break; } case ResourceGeometryGeneratorType.PathGeometry: { SourceCode = PathGeometrySourceGenerator.GeneratePathGeometry(path); break; } } }
public static void ExportImage(GraphicVisual visual, int width, bool addMargin, string filename) { // https://stackoverflow.com/questions/9080231/how-to-save-geometry-as-image var fileExtension = Path.GetExtension(filename).ToLower(); var hasTransparentBackground = fileExtension == ".png"; try { var normalizer = new NormalizeVisual(); var paths = normalizer.Normalize(visual, NormalizeAspect.Width, width); double height = normalizer.AspectRatio * width; var drawingBrush = DrawingBrushBinaryGenerator.Generate(paths); double margin = 0; if (addMargin) { margin = (width > height ? width : height) / 20.0; } double imageWidth = width + 2 * margin; double imageHeight = Math.Ceiling(height + 2 * margin); DrawingVisual viz = new DrawingVisual(); using (DrawingContext dc = viz.RenderOpen()) { if (!hasTransparentBackground) { dc.DrawRectangle(Brushes.White, null, new Rect(0, 0, imageWidth, imageHeight)); } dc.DrawRectangle(drawingBrush, null, new Rect(margin, margin, width, height)); } RenderTargetBitmap bmp = new RenderTargetBitmap((int)imageWidth, (int)imageHeight, // Size 96, 96, // DPI PixelFormats.Pbgra32); bmp.Render(viz); BitmapEncoder encoder = null; switch (fileExtension) { case ".png": encoder = new PngBitmapEncoder(); break; case ".jpg": encoder = new JpegBitmapEncoder(); break; case ".tiff": encoder = new TiffBitmapEncoder(); break; case ".bmp": encoder = new BmpBitmapEncoder(); break; case ".gif": encoder = new GifBitmapEncoder(); break; } encoder.Frames.Add(BitmapFrame.Create(bmp)); using (FileStream file = new FileStream(filename, FileMode.Create)) { encoder.Save(file); } } catch { } }
/// <summary> /// Export to an .ICO format /// </summary> public static void ExportIco(GraphicVisual visual, string filename) { try { int[] resolutions = { 16, 32, 64, 128, 256 }; FileStream file = new FileStream(filename, FileMode.Create); BinaryWriter binWriter = new BinaryWriter(file); List <long> offsets = new List <long>(); // icon dir // must be 0 Int16 int16 = 0; binWriter.Write(int16); // type int16 = 1; binWriter.Write(int16); // # of images int16 = (Int16)resolutions.Length; binWriter.Write(int16); // ICONDIRENTRY foreach (var resolution in resolutions) { // width byte int8 = (byte)(resolution == 256 ? 0 : resolution); binWriter.Write(int8); // height binWriter.Write(int8); // # colors in palette int8 = 0; binWriter.Write(int8); // reserved => 0 binWriter.Write(int8); // # color planes int16 = 1; binWriter.Write(int16); // # bit per pixel int16 = 0; binWriter.Write(int16); offsets.Add(file.Position); UInt32 uint32 = 0; // size of the image binWriter.Write(uint32); // offset of the image in file binWriter.Write(uint32); } int resIndex = 0; foreach (var resolution in resolutions) { var startPosition = file.Position; var normalizer = new NormalizeVisual(); var normalizedVisual = normalizer.Normalize(visual, NormalizeAspect.Both, resolution); double height = normalizer.AspectRatio * resolution; var drawingBrush = DrawingBrushBinaryGenerator.Generate(normalizedVisual); DrawingVisual viz = new DrawingVisual(); using (DrawingContext dc = viz.RenderOpen()) { dc.DrawRectangle(drawingBrush, null, new Rect(0, 0, resolution, resolution)); } RenderTargetBitmap bmp = new RenderTargetBitmap((int)resolution, (int)resolution, // Size 96, 96, // DPI PixelFormats.Pbgra32); bmp.Render(viz); BitmapEncoder encoder = null; encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); encoder.Save(file); var currentPosition = file.Position; var size = currentPosition - startPosition; file.Position = offsets[resIndex]; // patch size var uint32 = (UInt32)size; binWriter.Write(uint32); // patch offset of the image uint32 = (UInt32)startPosition; binWriter.Write(uint32); // set position back to the end file.Position = currentPosition; resIndex++; } file.Close(); file.Dispose(); } catch { } }
/// <summary> /// Update the Stream /// </summary> private void UpdateStreamSourceCode() { if (TypeComboBox == null || StreamCode == null) { return; } GraphicVisual visual = selectedVisual; if (NormalizeCheckBox.IsChecked == true) { var normalizer = new NormalizeVisual(); visual = normalizer.Normalize(selectedVisual, NormalizeAspect.Both, 100); } string xamlStream; GraphicPath graphicPath = visual as GraphicPath; if (graphicPath != null) { enableAllItems = true; SetTypeItemStatus(); StreamCode.TextWrapping = TextWrapping.NoWrap; } else { enableAllItems = false; SetTypeItemStatus(); if (TypeComboBox.SelectedIndex >= 2) { TypeComboBox.SelectedIndex = 1; } StreamCode.TextWrapping = TextWrapping.NoWrap; } if (graphicPath != null) { if (TypeComboBox.SelectedIndex == 0) { var streams = StreamSourceGenerator.GenerateStreamGeometries(visual); xamlStream = string.Join("\n", streams); } else if (TypeComboBox.SelectedIndex == 2) { xamlStream = StreamSourceGenerator.GeneratePathGeometry(graphicPath); } else if (TypeComboBox.SelectedIndex == 3) { xamlStream = StreamSourceGenerator.GenerateGeometry(graphicPath); } else { xamlStream = StreamSourceGenerator.GeneratePath(visual); } } else if (TypeComboBox.SelectedIndex == 0) { var streams = StreamSourceGenerator.GenerateStreamGeometries(visual); xamlStream = string.Join("\n", streams); } else { xamlStream = StreamSourceGenerator.GeneratePath(visual); } StreamCode.Text = xamlStream; }
/// <summary> /// Update all views with the new selection /// </summary> private void UpdateAll() { if (selectionChangedFromCode || PathSelectionBox == null || StreamCode == null || DrawingBrushCode == null || Preview == null) { return; } // get the selected paths selectedVisual = BuildSelectedDrawing(graphicVisual); // handle the color warning indicator if (selectedVisual == null) { SetColorWarning(""); Preview.Fill = null; StreamCode.Text = string.Empty; DrawingBrushCode.Text = string.Empty; GeometryCode.Text = string.Empty; return; } GraphicColorPrecision colorPrecision = GetColorPrecision(selectedVisual); switch (colorPrecision) { case GraphicColorPrecision.Precise: SetColorWarning(""); break; case GraphicColorPrecision.Estimated: SetColorWarning("Colors are estimated"); break; case GraphicColorPrecision.Placeholder: SetColorWarning("Colors are placeholders"); break; } // on request normalize the drawin GraphicVisual visual = selectedVisual; if (NormalizeCheckBox.IsChecked == true) { var normalizer = new NormalizeVisual(); visual = normalizer.Normalize(selectedVisual, NormalizeAspect.Both, 100); } // update the preview var drawingBrush = DrawingBrushBinaryGenerator.Generate(visual); Preview.Fill = drawingBrush; // update the stream source code UpdateStreamSourceCode(); // update the drawing brush source code var drawingBrushSource = DrawingBrushSourceGenerator.Generate(visual); DrawingBrushCode.Text = drawingBrushSource; // update the geometry source code UpdateGeometrySourceCode(); ExportMessage.Visibility = Visibility.Collapsed; }