Packages

 

com.aspose.imaging.fileformats.wmf.graphics

Class WmfRecorderGraphics2D



  • public final class WmfRecorderGraphics2D
    extends MetafileRecorderGraphics2D

    The Wmf recorder.

    Code example:

    This example shows how to create a WMF image and draw some geometric shapes using WmfRecorderGraphics2D.


    String dir = "c:\\temp\\";
    
    int imageWidth = 600;
    int imageHeight = 400;
    
    // This is the default screen resolution.
    int dpi = 96;
    
    com.aspose.imaging.Rectangle frame = new com.aspose.imaging.Rectangle(0, 0, imageWidth, imageHeight);
    
    // Create a WMF image.
    com.aspose.imaging.fileformats.wmf.graphics.WmfRecorderGraphics2D graphics =
            new com.aspose.imaging.fileformats.wmf.graphics.WmfRecorderGraphics2D(frame, dpi);
    
    // Draw a black rectangle along the image borders using a 1-pixel-wide black pen.
    graphics.drawRectangle(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getBlack(), 1), 0, 0, imageWidth, imageHeight);
    
    // Fill a rectangle with the color of white-smoke.
    graphics.fillRectangle(
            new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhiteSmoke()),
            new com.aspose.imaging.Rectangle(10, 10, 580, 380));
    
    // Draw two diagonal lines using a 1-pixel-wide darkgreen pen.
    graphics.drawLine(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getDarkGreen(), 1), 0, 0, imageWidth, imageHeight);
    graphics.drawLine(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getDarkGreen(), 1), 0, imageHeight, imageWidth, 0);
    
    // Draw an arc within the rectangle {0, 0, 200, 200} using a 2-pixel-wide blue pen.
    graphics.drawArc(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getBlue(), 2), new com.aspose.imaging.Rectangle(0, 0, 200, 200), 90, 270);
    
    // Fill an arc
    graphics.fillPie(
            new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getLightSkyBlue()),
            new com.aspose.imaging.Rectangle(0, 0, 150, 150), 90, 270);
    
    // Draw a cubic bezier using a 2-pixel-wide red pen.
    graphics.drawCubicBezier(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getRed(), 2),
            new com.aspose.imaging.Point(0, 0),
            new com.aspose.imaging.Point(200, 133),
            new com.aspose.imaging.Point(400, 166),
            new com.aspose.imaging.Point(600, 400));
    
    // Draw a raster image of the specified size at the specified location.
    // The image is scaled to fit the desired rectangle.
    com.aspose.imaging.RasterImage imageToDraw = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.bmp");
    {
        graphics.drawImage(imageToDraw,
                new com.aspose.imaging.Rectangle(400, 200, 100, 50),
                new com.aspose.imaging.Rectangle(0, 0, imageWidth, imageHeight),
                com.aspose.imaging.GraphicsUnit.Pixel);
    }
    
    // Draw a text string
    graphics.drawString("Hello World!", new com.aspose.imaging.Font("Arial", 48, com.aspose.imaging.FontStyle.Regular), com.aspose.imaging.Color.getDarkRed(), 200, 300);
    
    // Create a path to fill
    com.aspose.imaging.Figure figureToFill = new com.aspose.imaging.Figure();
    figureToFill.setClosed(true);
    
    com.aspose.imaging.GraphicsPath pathToFill = new com.aspose.imaging.GraphicsPath();
    pathToFill.addFigure(figureToFill);
    
    figureToFill.addShapes(new com.aspose.imaging.Shape[]
            {
                    new com.aspose.imaging.shapes.ArcShape(new com.aspose.imaging.RectangleF(400, 0, 200, 100), 45, 300),
                    new com.aspose.imaging.shapes.BezierShape(
                            new com.aspose.imaging.PointF[]
                                    {
                                            new com.aspose.imaging.PointF(300, 200),
                                            new com.aspose.imaging.PointF(400, 200),
                                            new com.aspose.imaging.PointF(500, 100),
                                            new com.aspose.imaging.PointF(600, 200),
                                    }),
                    new com.aspose.imaging.shapes.PolygonShape(
                            new com.aspose.imaging.PointF[]
                                    {
                                            new com.aspose.imaging.PointF(300, 100),
                                    }),
                    new com.aspose.imaging.shapes.RectangleShape(new com.aspose.imaging.RectangleF(0, 100, 200, 200)),
            });
    
    // Fill the path using a yellow brush and a green pen to draw outline
    graphics.fillPath(
            new com.aspose.imaging.Pen(com.aspose.imaging.Color.getGreen(), 2),
            new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getYellow()), pathToFill);
    
    // Create a path to draw
    com.aspose.imaging.GraphicsPath pathToDraw = new com.aspose.imaging.GraphicsPath();
    com.aspose.imaging.Figure figureToDraw = new com.aspose.imaging.Figure();
    pathToDraw.addFigure(figureToDraw);
    
    figureToDraw.addShapes(new com.aspose.imaging.Shape[]
            {
                    new com.aspose.imaging.shapes.ArcShape(new com.aspose.imaging.RectangleF(200, 200, 200, 200), 0, 360),
            });
    
    // Draw the path using a 5-pixel-wide orange pen.
    graphics.drawPath(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getOrange(), 5), pathToDraw);
    
    // In order to rasterize SVG we need to specify rasterization options.
    com.aspose.imaging.imageoptions.SvgRasterizationOptions rasterizationOptions = new com.aspose.imaging.imageoptions.SvgRasterizationOptions();
    com.aspose.imaging.imageoptions.PngOptions saveOptions = new com.aspose.imaging.imageoptions.PngOptions();
    saveOptions.setVectorRasterizationOptions(rasterizationOptions);
    
    // Get the final WMF image which includes all drawing commands
    com.aspose.imaging.fileformats.wmf.WmfImage wmfImage = graphics.endRecording();
    try {
        wmfImage.save(dir + "test.output.wmf");
    } finally {
        wmfImage.dispose();
    }
    

    • Constructor Detail

      • WmfRecorderGraphics2D

        public WmfRecorderGraphics2D(Rectangle frame,
                                     int inch)

        Initializes a new instance of the WmfRecorderGraphics2D class.

        Parameters:
        frame - Destination rectangle, measured in twips, for displaying the metafile.
        inch - The number of pixel per inch.
    • Method Detail

      • getBackgroundMode

        public int getBackgroundMode()

        Gets or sets the background mode.

        Value: The background mode.
        Returns:
        The background mode.
      • setBackgroundMode

        public void setBackgroundMode(short value)

        Gets or sets the background mode.

        Value: The background mode.
      • endRecording

        public final WmfImage endRecording()

        Ends the recording.

        Returns:
        The result image.
      • fromWmfImage

        public static WmfRecorderGraphics2D fromWmfImage(WmfImage wmfImage)

        Gets an instance of the Wmf recorder for the existing Wmf image.

        Parameters:
        wmfImage - The Wmf image to get a recoreder for.
        Returns:
        An instance of the WmfRecorderGraphics2D class.