Ejemplo n.º 1
0
        public void SaveToPDF(string fileName)
        {
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
                (int)GraphCanvas.ActualWidth, (int)GraphCanvas.ActualHeight,
                96d, 96d, PixelFormats.Pbgra32);

            GraphCanvas.Measure(new Size((int)GraphCanvas.ActualWidth, (int)GraphCanvas.ActualHeight));
            GraphCanvas.Arrange(new Rect(new Size((int)GraphCanvas.ActualWidth, (int)GraphCanvas.ActualHeight)));

            renderBitmap.Render(GraphCanvas);


            using (PdfDocument document = new PdfDocument())
            {
                PdfPage     page     = document.Pages.Add();
                PdfGraphics graphics = page.Graphics;

                using (var ms = new MemoryStream())
                {
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    encoder.Save(ms);
                    PdfBitmap image = new PdfBitmap(ms);
                    graphics.DrawImage(image, 0, 0);

                    document.Save(fileName);
                }
            }
        }
Ejemplo n.º 2
0
        public Image DrawImage(string fileName)
        {
            var ltrans = GraphCanvas.LayoutTransform;
            var rtrans = GraphCanvas.RenderTransform;

            GraphCanvas.LayoutTransform = null;
            GraphCanvas.RenderTransform = null;
            var renderSize = GraphCanvas.RenderSize;

            double scale = FitFactor;
            int    w     = (int)(GeometryGraph.Width * scale);
            int    h     = (int)(GeometryGraph.Height * scale);

            SetTransformOnViewportWithoutRaisingViewChangeEvent(scale, GeometryGraph.BoundingBox.Center, new Rectangle(0, 0, w, h));

            Size size = new Size(w, h);

            // Measure and arrange the surface
            // VERY IMPORTANT
            GraphCanvas.Measure(size);
            GraphCanvas.Arrange(new Rect(size));

            foreach (var node in drawingGraph.Nodes.Concat(drawingGraph.RootSubgraph.AllSubgraphsDepthFirstExcludingSelf()))
            {
                IViewerObject o;
                if (drawingObjectsToIViewerObjects.TryGetValue(node, out o))
                {
                    ((KosmographViewerNode)o).Invalidate();
                }
            }

            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(w, h, DpiX, DpiY, PixelFormats.Pbgra32);

            renderBitmap.Render(GraphCanvas);

            if (fileName != null)
            {
                // Create a file stream for saving image
                using (System.IO.FileStream outStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
                {
                    // Use png encoder for our data
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    // push the rendered bitmap to it
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    // save the data to the stream
                    encoder.Save(outStream);
                }
            }

            GraphCanvas.LayoutTransform = ltrans;
            GraphCanvas.RenderTransform = rtrans;
            GraphCanvas.Measure(renderSize);
            GraphCanvas.Arrange(new Rect(renderSize));

            return(new Image {
                Source = renderBitmap
            });
        }