public abstract class MetafileRecorderGraphics2D extends Object
The metafiles recorder graphics
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 and Description |
---|
MetafileRecorderGraphics2D() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the state of the graphics object
|
void |
drawArc(Pen pen,
Rectangle rect,
float startAngle,
float arcAngle)
Draws an arc representing a portion of an ellipse specified by a Rectangle structure.
|
void |
drawCubicBezier(Pen pen,
Point pt1,
Point pt2,
Point pt3,
Point pt4)
Draws the cubic bezier.
|
void |
drawEllipse(Pen pen,
Rectangle rect)
Draws the ellipse.
|
void |
drawImage(byte[] imageBytes,
Rectangle destRect,
int srcUnit)
Draws the image.
|
void |
drawImage(RasterImage image,
Point location)
Draws the specified Image, using its original physical size, at the specified location.
|
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.
|
void |
drawLine(Pen pen,
int x1,
int y1,
int x2,
int y2)
Draws the line.
|
void |
drawLine(Pen pen,
Point pt1,
Point pt2)
Draws the line.
|
void |
drawPath(Pen pen,
GraphicsPath path)
Draws the path.
|
void |
drawPie(Pen pen,
Rectangle rect,
float startAngle,
float sweepAngle)
Draws the pie.
|
void |
drawPolyCubicBezier(Pen pen,
Point[] points)
Draws the poly cubic bezier.
|
void |
drawPolygon(Pen pen,
Point[] points)
Draws the polygon.
|
void |
drawPolyline(Pen pen,
Point[] points)
Draws the polyline.
|
void |
drawRectangle(Pen pen,
int x,
int y,
int width,
int height)
Draws the rectangle.
|
void |
drawRectangle(Pen pen,
Rectangle rectangle)
Draws the rectangle.
|
void |
drawString(String string,
Font font,
Color color,
int x,
int y)
Draws the string.
|
void |
drawString(String string,
Font font,
Color color,
int x,
int y,
float angle)
Draws the string.
|
void |
excludeClip(Rectangle rect)
Updates the clip region of this Graphics to exclude the area specified by a Rectangle structure.
|
void |
excludeClip(Region region)
Updates the clip region of this Graphics to exclude the area specified by a Region.
|
void |
fillEllipse(Brush brush,
Rectangle rect)
Fills the ellipse.
|
void |
fillPath(Pen pen,
Brush brush,
GraphicsPath path)
Fills the path.
|
void |
fillPie(Brush brush,
Rectangle rect,
float startAngle,
float sweepAngle)
Fills the pie.
|
void |
fillPolygon(Brush brush,
Point[] points)
Fills the polygon.
|
void |
fillPolygon(Brush brush,
Point[] points,
int fillMode)
Fills the polygon.
|
void |
fillRectangle(Brush brush,
Rectangle rectangle)
Fills the rectangle.
|
Color |
getBackgroundColor()
Gets the color of the background.
|
Region |
getClip()
Gets or sets a Region that limits the drawing region of this Graphics
|
RectangleF |
getClipBounds()
Gets the clip bounds.
|
Matrix |
getTransform()
Gets the world transform.
|
void |
intersectClip(RectangleF rect)
Updates the clip region of this Graphics to the intersection of the current clip region and the specified Rectangle structure.
|
void |
intersectClip(Region region)
Updates the clip region of this Graphics to the intersection of the current clip region and the specified Region.
|
void |
multiplyTransform(Matrix matrix)
Multiplies the world transformation of this Graphics and specified the Matrix.
|
void |
multiplyTransform(Matrix matrix,
int order)
Multiplies the world transformation of this Graphics and specified the Matrix in the specified order.
|
void |
resetClip()
Resets the clip.
|
void |
rotateTransform(float angle)
Applies the specified rotation to the transformation matrix of this Graphics.
|
void |
rotateTransform(float angle,
PointF center,
int order)
Applies the specified rotation to the transformation matrix of this Graphics in the specified order.
|
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.
|
void |
scaleTransform(float sx,
float sy,
int order)
Applies the specified scaling operation to the transformation matrix of this Graphics in the specified order.
|
void |
setBackgroundColor(Color value)
Sets the color of the background.
|
void |
setClip(Region value)
Gets or sets a Region that limits the drawing region of this Graphics
|
void |
setTransform(Matrix transform)
Sets the transform.
|
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.
|
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.
|
public Region getClip()
Gets or sets a Region that limits the drawing region of this Graphics
public void setClip(Region value)
Gets or sets a Region that limits the drawing region of this Graphics
value
- The clip region.public RectangleF getClipBounds()
Gets the clip bounds.
public Color getBackgroundColor()
Gets the color of the background.
public void setBackgroundColor(Color value)
Sets the color of the background.
value
- The color of the background.public void clear()
Clears the state of the graphics object
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.
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.public void drawCubicBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)
Draws the cubic bezier.
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.public void drawPolyCubicBezier(Pen pen, Point[] points)
Draws the poly cubic bezier.
pen
- Pen that determines the color, width, and style of the figure.points
- The points.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.public void drawEllipse(Pen pen, Rectangle rect)
Draws the ellipse.
pen
- Pen that determines the color, width, and style of the figure.rect
- The boundaries of the ellipse.public void fillEllipse(Brush brush, Rectangle rect)
Fills the ellipse.
brush
- Brush that determines the characteristics of the fill.rect
- The boundaries of the ellipse.public void drawImage(RasterImage image, Point location)
Draws the specified Image, using its original physical size, at the specified location.
image
- The image to draw.location
- The location of the upper-left corner of the drawn image.public final void drawImage(byte[] imageBytes, Rectangle destRect, int srcUnit)
Draws the image.
imageBytes
- The image bytes.destRect
- The dest rect.srcUnit
- The source unit.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.
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.com.aspose.ms.System.ArgumentOutOfRangeException
- srcUnit;Support only Pixel unitpublic void drawLine(Pen pen, int x1, int y1, int x2, int y2)
Draws the line.
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.public void drawLine(Pen pen, Point pt1, Point pt2)
Draws the line.
pen
- Pen that determines the color, width, and style of the figure.pt1
- The first point.pt2
- The second point.public void drawPolyline(Pen pen, Point[] points)
Draws the polyline.
pen
- Pen that determines the color, width, and style of the figure.points
- The points.public void drawPath(Pen pen, GraphicsPath path)
Draws the path.
pen
- Pen that determines the color, width, and style of the figure.path
- The path to draw.public void fillPath(Pen pen, Brush brush, GraphicsPath path)
Fills the path.
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.public void drawPie(Pen pen, Rectangle rect, float startAngle, float sweepAngle)
Draws the pie.
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.public void fillPie(Brush brush, Rectangle rect, float startAngle, float sweepAngle)
Fills the pie.
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.public void drawPolygon(Pen pen, Point[] points)
Draws the polygon.
pen
- Pen that determines the color, width, and style of the figure.points
- The points.public void fillPolygon(Brush brush, Point[] points)
Fills the polygon.
brush
- Brush that determines the characteristics of the fill.points
- The points.public void fillPolygon(Brush brush, Point[] points, int fillMode)
Fills the polygon.
brush
- Brush that determines the characteristics of the fill.points
- The points.fillMode
- The fill mode.public void drawRectangle(Pen pen, int x, int y, int width, int height)
Draws the rectangle.
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.public void drawRectangle(Pen pen, Rectangle rectangle)
Draws the rectangle.
pen
- Pen that determines the color, width, and style of the figure.rectangle
- The rectangle to draw.public void fillRectangle(Brush brush, Rectangle rectangle)
Fills the rectangle.
brush
- Brush that determines the characteristics of the fill.rectangle
- The rectangle to fill.public void drawString(String string, Font font, Color color, int x, int y)
Draws the string.
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.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(); }
public void drawString(String string, Font font, Color color, int x, int y, float angle)
Draws the string.
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.public void excludeClip(Rectangle rect)
Updates the clip region of this Graphics to exclude the area specified by a Rectangle structure.
rect
- Rectangle structure that specifies the rectangle to exclude from the clip region.public void excludeClip(Region region)
Updates the clip region of this Graphics to exclude the area specified by a Region.
region
- Region that specifies the region to exclude from the clip region.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.
rect
- Rectangle structure to intersect with the current clip region.public void intersectClip(Region region)
Updates the clip region of this Graphics to the intersection of the current clip region and the specified Region.
region
- Region to intersect with the current region.public void resetClip()
Resets the clip.
public void multiplyTransform(Matrix matrix)
Multiplies the world transformation of this Graphics and specified the Matrix.
matrix
- The matrix that multiplies the world transformation.public void multiplyTransform(Matrix matrix, int order)
Multiplies the world transformation of this Graphics and specified the Matrix in the specified order.
matrix
- The matrix that multiplies the world transformation.order
- The order of the multiplication.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.
x
- The x-coordinate of the translation.y
- The y-coordinate of the translation.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.
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.public void rotateTransform(float angle)
Applies the specified rotation to the transformation matrix of this Graphics.
angle
- Angle of rotation in degrees.public void rotateTransform(float angle, PointF center, int order)
Applies the specified rotation to the transformation matrix of this Graphics in the specified order.
angle
- Angle of rotation in degrees.center
- The rotating center.order
- Specifies whether the rotation is appended or prepended to the matrix transformation..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.
sx
- Scale factor in the x direction.sy
- Scale factor in the y direction.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.
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.public Matrix getTransform()
Gets the world transform.
public void setTransform(Matrix transform)
Sets the transform.
transform
- The new transform matrix.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(); }