예제 #1
0
        public static void RenderShapeToStream(string dataDir, Shape shape)
        {
            ShapeRenderer r = new ShapeRenderer(shape);

            // Define custom options which control how the image is rendered. Render the shape to the vector format EMF.
            ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Jpeg)
            {
                // Output the image in gray scale
                ImageColorMode = ImageColorMode.Grayscale,

                // Reduce the brightness a bit (default is 0.5f).
                ImageBrightness = 0.45f
            };
            dataDir = dataDir + "TestFile.RenderToStream_out_.jpg";
            FileStream stream = new FileStream(dataDir, FileMode.Create);

            // Save the rendered image to the stream using different options.
            r.Save(stream, imageOptions);

            Console.WriteLine("\nShape rendered to stream successfully.\nFile saved at " + dataDir);
        }
예제 #2
0
        public static void RenderShapeToDisk(string dataDir, Shape shape)
        {
            //ExStart
            //ExFor:ShapeRenderer
            //ExFor:ShapeBase.GetShapeRenderer
            //ExFor:ImageSaveOptions
            //ExFor:ImageSaveOptions.Scale
            //ExFor:ShapeRenderer.Save(String, ImageSaveOptions)
            //ExId:RenderShapeToDisk
            //ExSummary:Shows how to render a shape independent of the document to an EMF image and save it to disk.
            // The shape render is retrieved using this method. This is made into a separate object from the shape as it internally
            // caches the rendered shape.
            ShapeRenderer r = shape.GetShapeRenderer();

            // Define custom options which control how the image is rendered. Render the shape to the JPEG raster format.
            ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Emf)
            {
                Scale = 1.5f
            };

            // Save the rendered image to disk.
            r.Save(dataDir + "TestFile.RenderToDisk Out.emf", imageOptions);
            //ExEnd
        }
예제 #3
0
        public void GetOpaqueBoundsInPixels()
        {
            Document doc = new Document(MyDir + "Shape.TextBox.doc");

            Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

            ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Jpeg);

            MemoryStream  stream   = new MemoryStream();
            ShapeRenderer renderer = shape.GetShapeRenderer();

            renderer.Save(stream, imageOptions);
            shape.Remove();

            //Check that the opaque bounds and bounds have default values
            Assert.AreEqual(250, renderer.GetOpaqueBoundsInPixels(imageOptions.Scale, imageOptions.HorizontalResolution).Width);
            Assert.AreEqual(52, renderer.GetOpaqueBoundsInPixels(imageOptions.Scale, imageOptions.HorizontalResolution).Height);

            Assert.AreEqual(250, renderer.GetBoundsInPixels(imageOptions.Scale, imageOptions.VerticalResolution).Width);
            Assert.AreEqual(52, renderer.GetBoundsInPixels(imageOptions.Scale, imageOptions.VerticalResolution).Height);

            Assert.AreEqual((float)187.849991, renderer.OpaqueBoundsInPoints.Width);
            Assert.AreEqual((float)39.25, renderer.OpaqueBoundsInPoints.Height);
        }
예제 #4
0
        public void RenderShapeAsJpeg()
        {
            Document doc = new Document(MyDir + "Rendering.docx");

            Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

            //ExStart:RenderShapeAsJpeg
            ShapeRenderer render = new ShapeRenderer(shape);

            ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Jpeg)
            {
                // Output the image in gray scale
                ImageColorMode = ImageColorMode.Grayscale,

                // Reduce the brightness a bit (default is 0.5f)
                ImageBrightness = 0.45f
            };

            using (FileStream stream = new FileStream(ArtifactsDir + "RenderShape.RenderShapeAsJpeg - Aspose.Words.jpg", FileMode.Create))
            {
                render.Save(stream, imageOptions);
            }
            //ExEnd:RenderShapeAsJpeg
        }
        /// <summary>
        /// Renders any node in a document to the path specified using the image save options.
        /// </summary>
        /// <param name="node">The node to render.</param>
        /// <param name="filePath">The path to save the rendered image to.</param>
        /// <param name="imageOptions">The image options to use during rendering. This can be null.</param>
        public void RenderNode(Node node, string filePath, ImageSaveOptions imageOptions)
        {
            if (imageOptions == null)
            {
                imageOptions = new ImageSaveOptions(FileFormatUtil.ExtensionToSaveFormat(Path.GetExtension(filePath)));
            }

            // Store the paper color to be used on the final image and change to transparent.
            // This will cause any content around the rendered node to be removed later on.
            Color savePaperColor = imageOptions.PaperColor;

            imageOptions.PaperColor = Color.Transparent;

            // There a bug which affects the cache of a cloned node.
            // To avoid this, we clone the entire document, including all nodes,
            // finding the matching node in the cloned document and rendering that instead.
            Document doc = (Document)node.Document.Clone(true);

            node = doc.GetChild(NodeType.Any, node.Document.GetChildNodes(NodeType.Any, true).IndexOf(node), true);

            // Create a temporary shape to store the target node in. This shape will be rendered to retrieve
            // the rendered content of the node.
            Shape   shape         = new Shape(doc, ShapeType.TextBox);
            Section parentSection = (Section)node.GetAncestor(NodeType.Section);

            // Assume that the node cannot be larger than the page in size.
            shape.Width     = parentSection.PageSetup.PageWidth;
            shape.Height    = parentSection.PageSetup.PageHeight;
            shape.FillColor = Color.Transparent;

            // Don't draw a surronding line on the shape.
            shape.Stroked = false;

            // Move up through the DOM until we find a suitable node to insert into a Shape
            // (a node with a parent can contain paragraphs, tables the same as a shape). Each parent node is cloned
            // on the way up so even a descendant node passed to this method can be rendered. Since we are working
            // with the actual nodes of the document we need to clone the target node into the temporary shape.
            Node currentNode = node;

            while (!(currentNode.ParentNode is InlineStory || currentNode.ParentNode is Story ||
                     currentNode.ParentNode is ShapeBase))
            {
                CompositeNode parent = (CompositeNode)currentNode.ParentNode.Clone(false);
                currentNode = currentNode.ParentNode;
                parent.AppendChild(node.Clone(true));
                node = parent; // Store this new node to be inserted into the shape.
            }

            // We must add the shape to the document tree to have it rendered.
            shape.AppendChild(node.Clone(true));
            parentSection.Body.FirstParagraph.AppendChild(shape);

            // Render the shape to stream so we can take advantage of the effects of the ImageSaveOptions class.
            // Retrieve the rendered image and remove the shape from the document.
            MemoryStream  stream   = new MemoryStream();
            ShapeRenderer renderer = shape.GetShapeRenderer();

            renderer.Save(stream, imageOptions);
            shape.Remove();

            Rectangle crop = renderer.GetOpaqueBoundsInPixels(imageOptions.Scale, imageOptions.HorizontalResolution,
                                                              imageOptions.VerticalResolution);

            using (Bitmap renderedImage = new Bitmap(stream))
            {
                Bitmap croppedImage = new Bitmap(crop.Width, crop.Height);
                croppedImage.SetResolution(imageOptions.HorizontalResolution, imageOptions.VerticalResolution);

                // Create the final image with the proper background color.
                using (Graphics g = Graphics.FromImage(croppedImage))
                {
                    g.Clear(savePaperColor);
                    g.DrawImage(renderedImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), crop.X,
                                crop.Y, crop.Width, crop.Height, GraphicsUnit.Pixel);

                    croppedImage.Save(filePath);
                }
            }
        }
예제 #6
0
        public static void RenderShapeToStream(string dataDir, Shape shape)
        {
            //ExStart
            //ExFor:ShapeRenderer
            //ExFor:ShapeRenderer.#ctor(ShapeBase)
            //ExFor:ImageSaveOptions.ImageColorMode
            //ExFor:ImageSaveOptions.ImageBrightness
            //ExFor:ShapeRenderer.Save(Stream, ImageSaveOptions)
            //ExId:RenderShapeToStream
            //ExSummary:Shows how to render a shape independent of the document to a JPEG image and save it to a stream.
            // We can also retrieve the renderer for a shape by using the ShapeRenderer constructor.
            ShapeRenderer r = new ShapeRenderer(shape);

            // Define custom options which control how the image is rendered. Render the shape to the vector format EMF.
            ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Jpeg)
            {
                // Output the image in gray scale
                ImageColorMode = ImageColorMode.Grayscale,

                // Reduce the brightness a bit (default is 0.5f).
                ImageBrightness = 0.45f
            };

            FileStream stream = new FileStream(dataDir + "TestFile.RenderToStream Out.jpg", FileMode.CreateNew);

            // Save the rendered image to the stream using different options.
            r.Save(stream, imageOptions);
            //ExEnd
        }