Packages

 

com.aspose.imaging.fileformats.emf.graphics

Class MetafileRecorderGraphics2D

  • java.lang.Object
    • com.aspose.imaging.fileformats.emf.graphics.MetafileRecorderGraphics2D
  • Direct Known Subclasses:
    EmfRecorderGraphics2D, WmfRecorderGraphics2D


    public abstract class MetafileRecorderGraphics2D
    extends Object

    The metafiles recorder graphics

    Code example:

    This example shows how to create a EMF image and draw some geometric shapes on it using EmfRecorderGraphics2D.


    String dir = "c:\\temp\\";
    
    // The image size in pixels
    int deviceWidth = 600;
    int deviceHeight = 400;
    
    // The image size in millimeters
    int deviceWidthMm = (int) (deviceWidth / 100f);
    int deviceHeightMm = (int) (deviceHeight / 100f);
    
    com.aspose.imaging.Rectangle frame = new com.aspose.imaging.Rectangle(0, 0, deviceWidth, deviceHeight);
    
    // Create a EMF image.
    com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D graphics =
            new com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D(
                    frame,
                    new com.aspose.imaging.Size(deviceWidth, deviceHeight),
                    new com.aspose.imaging.Size(deviceWidthMm, deviceHeightMm));
    
    // 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, deviceWidth, deviceHeight);
    
    // 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, deviceWidth, deviceHeight);
    graphics.drawLine(new com.aspose.imaging.Pen(com.aspose.imaging.Color.getDarkGreen(), 1), 0, deviceHeight, deviceWidth, 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");
    try {
        graphics.drawImage(imageToDraw,
                new com.aspose.imaging.Rectangle(400, 200, 100, 50),
                new com.aspose.imaging.Rectangle(0, 0, deviceWidth, deviceHeight),
                com.aspose.imaging.GraphicsUnit.Pixel);
    } finally {
        imageToDraw.dispose();
    }
    
    // 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.emf.EmfImage emfImage = graphics.endRecording();
    try {
        emfImage.save(dir + "test.output.emf");
    } finally {
        emfImage.dispose();
    }
    

    • Constructor Detail

      • MetafileRecorderGraphics2D

        public MetafileRecorderGraphics2D()
    • Method Detail

      • getClip

        public Region getClip()

        Gets or sets a Region that limits the drawing region of this Graphics

        Returns:
        The clip region.
      • setClip

        public void setClip(Region value)

        Gets or sets a Region that limits the drawing region of this Graphics

        Parameters:
        value - The clip region.
      • getClipBounds

        public RectangleF getClipBounds()

        Gets the clip bounds.

        Returns:
        The clip bounds.
      • getBackgroundColor

        public Color getBackgroundColor()

        Gets the color of the background.

        Returns:
        The color of the background.
      • setBackgroundColor

        public void setBackgroundColor(Color value)

        Sets the color of the background.

        Parameters:
        value - The color of the background.
      • clear

        public void clear()

        Clears the state of the graphics object

      • drawArc

        public void drawArc(Pen pen,
                            Rectangle rect,
                            float startAngle,
                            float arcAngle)

        Draws an arc representing a portion of an ellipse specified by a Rectangle structure.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        rect - The boundaries of the ellipse.
        startAngle - Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
        arcAngle - Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.
      • drawCubicBezier

        public void drawCubicBezier(Pen pen,
                                    Point pt1,
                                    Point pt2,
                                    Point pt3,
                                    Point pt4)

        Draws the cubic bezier.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        pt1 - The starting point of the curve.
        pt2 - The first control point for the curve.
        pt3 - The second control point for the curve.
        pt4 - The ending point of the curve.
      • drawPolyCubicBezier

        public void drawPolyCubicBezier(Pen pen,
                                        Point[] points)

        Draws the poly cubic bezier.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        points - The points.
        Throws:
        com.aspose.ms.System.ArgumentOutOfRangeException - The number of points in the array should be a multiple of 3 plus 1, such as 4, 7, or 10.
      • drawEllipse

        public void drawEllipse(Pen pen,
                                Rectangle rect)

        Draws the ellipse.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        rect - The boundaries of the ellipse.
      • fillEllipse

        public void fillEllipse(Brush brush,
                                Rectangle rect)

        Fills the ellipse.

        Parameters:
        brush - Brush that determines the characteristics of the fill.
        rect - The boundaries of the ellipse.
      • drawImage

        public void drawImage(RasterImage image,
                              Point location)

        Draws the specified Image, using its original physical size, at the specified location.

        Parameters:
        image - The image to draw.
        location - The location of the upper-left corner of the drawn image.
      • drawImage

        public final void drawImage(byte[] imageBytes,
                                    Rectangle destRect,
                                    int srcUnit)

        Draws the image.

        Parameters:
        imageBytes - The image bytes.
        destRect - The dest rect.
        srcUnit - The source unit.
      • drawImage

        public void drawImage(RasterImage image,
                              Rectangle destRect,
                              Rectangle srcRect,
                              int srcUnit)

        Draws the specified portion of the specified Image at the specified location and with the specified size.

        Parameters:
        image - The image to draw.
        destRect - Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.
        srcRect - Rectangle structure that specifies the portion of the image object to draw.
        srcUnit - The units of measure used by the srcRect parameter.
        Throws:
        com.aspose.ms.System.ArgumentOutOfRangeException - srcUnit;Support only Pixel unit
      • drawLine

        public void drawLine(Pen pen,
                             int x1,
                             int y1,
                             int x2,
                             int y2)

        Draws the line.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        x1 - The x-coordinate of the first point.
        y1 - The y-coordinate of the first point.
        x2 - The x-coordinate of the second point.
        y2 - The y-coordinate of the second point.
      • drawLine

        public void drawLine(Pen pen,
                             Point pt1,
                             Point pt2)

        Draws the line.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        pt1 - The first point.
        pt2 - The second point.
      • drawPolyline

        public void drawPolyline(Pen pen,
                                 Point[] points)

        Draws the polyline.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        points - The points.
      • drawPath

        public void drawPath(Pen pen,
                             GraphicsPath path)

        Draws the path.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        path - The path to draw.
      • fillPath

        public void fillPath(Pen pen,
                             Brush brush,
                             GraphicsPath path)

        Fills the path.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        brush - Brush that determines the characteristics of the fill.
        path - The path to fill.
      • drawPie

        public void drawPie(Pen pen,
                            Rectangle rect,
                            float startAngle,
                            float sweepAngle)

        Draws the pie.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        rect - The boundaries of the ellipse.
        startAngle - Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
        sweepAngle - Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.
      • fillPie

        public void fillPie(Brush brush,
                            Rectangle rect,
                            float startAngle,
                            float sweepAngle)

        Fills the pie.

        Parameters:
        brush - Brush that determines the characteristics of the fill.
        rect - The boundaries of the ellipse.
        startAngle - Angle in degrees measured clockwise from the x-axis to the starting point of the arc.
        sweepAngle - Angle in degrees measured clockwise from the startAngle parameter to ending point of the arc.
      • drawPolygon

        public void drawPolygon(Pen pen,
                                Point[] points)

        Draws the polygon.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        points - The points.
      • fillPolygon

        public void fillPolygon(Brush brush,
                                Point[] points)

        Fills the polygon.

        Parameters:
        brush - Brush that determines the characteristics of the fill.
        points - The points.
      • fillPolygon

        public void fillPolygon(Brush brush,
                                Point[] points,
                                int fillMode)

        Fills the polygon.

        Parameters:
        brush - Brush that determines the characteristics of the fill.
        points - The points.
        fillMode - The fill mode.
      • drawRectangle

        public void drawRectangle(Pen pen,
                                  int x,
                                  int y,
                                  int width,
                                  int height)

        Draws the rectangle.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        x - The x-coordinate of the upper-left corner of the rectangle to draw.
        y - The y-coordinate of the upper-left corner of the rectangle to draw.
        width - The width of the rectangle to draw.
        height - The height of the rectangle to draw.
      • drawRectangle

        public void drawRectangle(Pen pen,
                                  Rectangle rectangle)

        Draws the rectangle.

        Parameters:
        pen - Pen that determines the color, width, and style of the figure.
        rectangle - The rectangle to draw.
      • fillRectangle

        public void fillRectangle(Brush brush,
                                  Rectangle rectangle)

        Fills the rectangle.

        Parameters:
        brush - Brush that determines the characteristics of the fill.
        rectangle - The rectangle to fill.
      • drawString

        public void drawString(String string,
                               Font font,
                               Color color,
                               int x,
                               int y)

        Draws the string.

        Parameters:
        string - The string.
        font - Font that defines the text format of the string.
        color - The text color.
        x - The x-coordinate of the upper-left corner of the drawn text.
        y - The y-coordinate of the upper-left corner of the drawn text.
        Code example:

        This example shows how to load a EMF image from a file and draw a text string over it.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.fileformats.emf.EmfImage emfImage = (com.aspose.imaging.fileformats.emf.EmfImage) com.aspose.imaging.Image.load(dir + "test.emf");
        try {
            com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D graphics =
                    com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D.fromEmfImage(emfImage);
        
            // First, get the image size
            int width = emfImage.getWidth();
            int height = emfImage.getHeight();
        
            // Second, calculate a transformation to put a text string along the main diagonal of the image -
            // from the upper-left to the bootom-right corner.
            float emFontSize = 96f;
            float d = (float) java.lang.Math.sqrt(width * width + height * height);
            float scaleFactor = d / (emFontSize * 5f);
        
            float tan = ((float) height) / width;
            double radians = java.lang.Math.atan(tan);
            double degrees = (180 * radians) / java.lang.Math.PI;
        
            com.aspose.imaging.Matrix transform = new com.aspose.imaging.Matrix();
            transform.rotate((float) degrees);
            transform.scale(scaleFactor, scaleFactor);
        
            // Then, set the transform.
            graphics.setTransform(transform);
        
            // Finally, put a watermark (text string of pink color) along the main diagonal.
            graphics.drawString("WATERMARK", new com.aspose.imaging.Font(
                            "Courier New", emFontSize),
                    com.aspose.imaging.Color.getLightPink(), 0, 0/*, (float)degrees*/);
        
            // Save the image with watermark to another EMF file.
            com.aspose.imaging.fileformats.emf.EmfImage scaledEmfImage = graphics.endRecording();
            try {
                scaledEmfImage.save(dir + "test.scaled.emf");
            } finally {
                scaledEmfImage.dispose();
            }
        } finally {
            emfImage.dispose();
        }
        

      • drawString

        public void drawString(String string,
                               Font font,
                               Color color,
                               int x,
                               int y,
                               float angle)

        Draws the string.

        Parameters:
        string - The string.
        font - Font that defines the text format of the string.
        color - The text color.
        x - The x-coordinate of the upper-left corner of the drawn text.
        y - The y-coordinate of the upper-left corner of the drawn text.
        angle - The angle in degrees, between the escapement vector and the x-axis of the device. The escapement vector is parallel to the base line of a row of text.
      • excludeClip

        public void excludeClip(Rectangle rect)

        Updates the clip region of this Graphics to exclude the area specified by a Rectangle structure.

        Parameters:
        rect - Rectangle structure that specifies the rectangle to exclude from the clip region.
      • excludeClip

        public void excludeClip(Region region)

        Updates the clip region of this Graphics to exclude the area specified by a Region.

        Parameters:
        region - Region that specifies the region to exclude from the clip region.
      • intersectClip

        public void intersectClip(RectangleF rect)

        Updates the clip region of this Graphics to the intersection of the current clip region and the specified Rectangle structure.

        Parameters:
        rect - Rectangle structure to intersect with the current clip region.
      • intersectClip

        public void intersectClip(Region region)

        Updates the clip region of this Graphics to the intersection of the current clip region and the specified Region.

        Parameters:
        region - Region to intersect with the current region.
      • resetClip

        public void resetClip()

        Resets the clip.

      • multiplyTransform

        public void multiplyTransform(Matrix matrix)

        Multiplies the world transformation of this Graphics and specified the Matrix.

        Parameters:
        matrix - The matrix that multiplies the world transformation.
      • multiplyTransform

        public void multiplyTransform(Matrix matrix,
                                      int order)

        Multiplies the world transformation of this Graphics and specified the Matrix in the specified order.

        Parameters:
        matrix - The matrix that multiplies the world transformation.
        order - The order of the multiplication.
      • translateTransform

        public void translateTransform(float x,
                                       float y)

        Changes the origin of the coordinate system by prepending the specified translation to the transformation matrix of this Graphics.

        Parameters:
        x - The x-coordinate of the translation.
        y - The y-coordinate of the translation.
      • translateTransform

        public void translateTransform(float x,
                                       float y,
                                       int order)

        Changes the origin of the coordinate system by applying the specified translation to the transformation matrix of this Graphics in the specified order.

        Parameters:
        x - The x-coordinate of the translation.
        y - The y-coordinate of the translation.
        order - Specifies whether the translation is prepended or appended to the transformation matrix.
      • rotateTransform

        public void rotateTransform(float angle)

        Applies the specified rotation to the transformation matrix of this Graphics.

        Parameters:
        angle - Angle of rotation in degrees.
      • rotateTransform

        public void rotateTransform(float angle,
                                    PointF center,
                                    int order)

        Applies the specified rotation to the transformation matrix of this Graphics in the specified order.

        Parameters:
        angle - Angle of rotation in degrees.
        center - The rotating center.
        order - Specifies whether the rotation is appended or prepended to the matrix transformation..
      • scaleTransform

        public void scaleTransform(float sx,
                                   float sy)

        Applies the specified scaling operation to the transformation matrix of this Graphics by prepending it to the object's transformation matrix.

        Parameters:
        sx - Scale factor in the x direction.
        sy - Scale factor in the y direction.
      • scaleTransform

        public void scaleTransform(float sx,
                                   float sy,
                                   int order)

        Applies the specified scaling operation to the transformation matrix of this Graphics in the specified order.

        Parameters:
        sx - Scale factor in the x direction.
        sy - Scale factor in the y direction.
        order - Specifies whether the scaling operation is prepended or appended to the transformation matrix.
      • getTransform

        public Matrix getTransform()

        Gets the world transform.

        Returns:
        The transform matrix.
      • setTransform

        public void setTransform(Matrix transform)

        Sets the transform.

        Parameters:
        transform - The new transform matrix.
        Code example:

        This example shows how to load a EMF image from a file and draw a text string over it.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.fileformats.emf.EmfImage emfImage = (com.aspose.imaging.fileformats.emf.EmfImage) com.aspose.imaging.Image.load(dir + "test.emf");
        try {
            com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D graphics =
                    com.aspose.imaging.fileformats.emf.graphics.EmfRecorderGraphics2D.fromEmfImage(emfImage);
        
            // First, get the image size
            int width = emfImage.getWidth();
            int height = emfImage.getHeight();
        
            // Second, calculate a transformation to put a text string along the main diagonal of the image -
            // from the upper-left to the bootom-right corner.
            float emFontSize = 96f;
            float d = (float) java.lang.Math.sqrt(width * width + height * height);
            float scaleFactor = d / (emFontSize * 5f);
        
            float tan = ((float) height) / width;
            double radians = java.lang.Math.atan(tan);
            double degrees = (180 * radians) / java.lang.Math.PI;
        
            com.aspose.imaging.Matrix transform = new com.aspose.imaging.Matrix();
            transform.rotate((float) degrees);
            transform.scale(scaleFactor, scaleFactor);
        
            // Then, set the transform.
            graphics.setTransform(transform);
        
            // Finally, put a watermark (text string of pink color) along the main diagonal.
            graphics.drawString("WATERMARK", new com.aspose.imaging.Font(
                            "Courier New", emFontSize),
                    com.aspose.imaging.Color.getLightPink(), 0, 0/*, (float)degrees*/);
        
            // Save the image with watermark to another EMF file.
            com.aspose.imaging.fileformats.emf.EmfImage scaledEmfImage = graphics.endRecording();
            try {
                scaledEmfImage.save(dir + "test.scaled.emf");
            } finally {
                scaledEmfImage.dispose();
            }
        } finally {
            emfImage.dispose();
        }