Example #1
0
        /// <summary>
        /// Set a new visual
        /// </summary>
        public void SetNewGraphicVisual(GraphicVisual visual, GraphicColorPrecision?colorPrecision = null)
        {
            if (visual == null)
            {
                ColorPrecisionMessage = null;
                Preview = null;
                return;
            }

            ColorPrecisionMessage = CommonViews.Helper.GetColorPrecisionText(colorPrecision);
            Preview = DrawingBrushBinaryGenerator.Generate(visual);
        }
Example #2
0
        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
            {
            }
        }
Example #3
0
        /// <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
            {
            }
        }
Example #4
0
        /// <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;
        }