MetafileRecorderGraphics2D.DrawString
内容
[
隐藏
]DrawString(string, Font, Color, int, int)
绘制字符串。
public void DrawString(string @string, Font font, Color color, int x, int y)
| 参数 | 类型 | 描述 |
|---|---|---|
| 字符串 | String | 该字符串。 |
| font | Font | 定义字符串文本格式的字体。 |
| 颜色 | 颜色 | 文本颜色。 |
| x | Int32 | 绘制文本左上角的 x 坐标。 |
| y | Int32 | 绘制文本左上角的 y 坐标。 |
示例
本示例展示了如何从文件加载 EMF 图像并在其上绘制文本字符串。
[C#]
string dir = "c:\\temp\\";
using (Aspose.Imaging.FileFormats.Emf.EmfImage emfImage = (Aspose.Imaging.FileFormats.Emf.EmfImage)Aspose.Imaging.Image.Load(dir + "test.emf"))
{
Aspose.Imaging.FileFormats.Emf.Graphics.EmfRecorderGraphics2D graphics =
Aspose.Imaging.FileFormats.Emf.Graphics.EmfRecorderGraphics2D.FromEmfImage(emfImage);
// 第一步,获取图像尺寸
int width = emfImage.Width;
int height = emfImage.Height;
// 第二步,计算一个变换,以将文本字符串放置在图像的主对角线上 -
// 从左上角到右下角。
float emFontSize = 96f;
float d = (float)System.Math.Sqrt(width * width + height * height);
float scaleFactor = d / (emFontSize * 5f);
float tan = ((float)height) / width;
double radians = System.Math.Atan(tan);
double degrees = (180 * radians) / System.Math.PI;
Aspose.Imaging.Matrix transform = new Aspose.Imaging.Matrix();
transform.Rotate((float)degrees);
transform.Scale(scaleFactor, scaleFactor);
// 然后,设置变换。
graphics.SetTransform(transform);
// 最后,在主对角线上放置水印(粉色文本字符串)。
graphics.DrawString("WATERMARK", new Aspose.Imaging.Font("Courier New", emFontSize), Aspose.Imaging.Color.LightPink, 0, 0/*, (float)degrees*/);
// 将带有水印的图像保存为另一个 EMF 文件。
using (Aspose.Imaging.FileFormats.Emf.EmfImage scaledEmfImage = graphics.EndRecording())
{
scaledEmfImage.Save(dir + "test.scaled.emf");
}
}
此示例展示了如何使用 WmfRecorderGraphics2D 创建 WMF 图像并绘制一些几何形状。
[C#]
string dir = "c:\\temp\\";
int imageWidth = 600;
int imageHeight = 400;
// 这是默认的屏幕分辨率。
int dpi = 96;
Aspose.Imaging.Rectangle frame = new Aspose.Imaging.Rectangle(0, 0, imageWidth, imageHeight);
// 创建 WMF 图像。
Aspose.Imaging.FileFormats.Wmf.Graphics.WmfRecorderGraphics2D graphics =
new Aspose.Imaging.FileFormats.Wmf.Graphics.WmfRecorderGraphics2D(frame, dpi);
// 使用 1 像素宽的黑色笔在图像边缘绘制一个黑色矩形。
graphics.DrawRectangle(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Black, 1), 0, 0, imageWidth, imageHeight);
// 使用 white-smoke 颜色填充矩形。
graphics.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.WhiteSmoke), new Aspose.Imaging.Rectangle(10, 10, 580, 380));
// 使用 1 像素宽的 darkgreen 笔绘制两条对角线。
graphics.DrawLine(new Aspose.Imaging.Pen(Aspose.Imaging.Color.DarkGreen, 1), 0, 0, imageWidth, imageHeight);
graphics.DrawLine(new Aspose.Imaging.Pen(Aspose.Imaging.Color.DarkGreen, 1), 0, imageHeight, imageWidth, 0);
// 使用 2 像素宽的蓝色笔在矩形 {0, 0, 200, 200} 内绘制弧线。
graphics.DrawArc(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Blue, 2), new Aspose.Imaging.Rectangle(0, 0, 200, 200), 90, 270);
// 填充弧线
graphics.FillPie(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.LightSkyBlue), new Aspose.Imaging.Rectangle(0, 0, 150, 150), 90, 270);
// 使用 2 像素宽的红色笔绘制三次贝塞尔曲线。
graphics.DrawCubicBezier(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Red, 2),
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(200, 133),
new Aspose.Imaging.Point(400, 166),
new Aspose.Imaging.Point(600, 400));
// 在指定位置绘制指定大小的光栅图像。
// 图像已缩放以适应所需的矩形。
using (Aspose.Imaging.RasterImage imageToDraw = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
graphics.DrawImage(imageToDraw,
new Aspose.Imaging.Rectangle(400, 200, 100, 50),
new Aspose.Imaging.Rectangle(0, 0, imageWidth, imageHeight),
Aspose.Imaging.GraphicsUnit.Pixel);
}
// 绘制文本字符串
graphics.DrawString("Hello World!", new Aspose.Imaging.Font("Arial", 48, Aspose.Imaging.FontStyle.Regular), Aspose.Imaging.Color.DarkRed, 200, 300);
// 创建用于填充的路径
Aspose.Imaging.Figure figureToFill = new Aspose.Imaging.Figure();
figureToFill.IsClosed = true;
Aspose.Imaging.GraphicsPath pathToFill = new Aspose.Imaging.GraphicsPath();
pathToFill.AddFigure(figureToFill);
figureToFill.AddShapes(new Shape[]
{
new Aspose.Imaging.Shapes.ArcShape(new Aspose.Imaging.Rectangle(400, 0, 200, 100), 45, 300),
new Aspose.Imaging.Shapes.BezierShape(
new Aspose.Imaging.PointF[]
{
new Aspose.Imaging.PointF(300, 200),
new Aspose.Imaging.PointF(400, 200),
new Aspose.Imaging.PointF(500, 100),
new Aspose.Imaging.PointF(600, 200),
}),
new Aspose.Imaging.Shapes.PolygonShape(
new Aspose.Imaging.PointF[]
{
new Aspose.Imaging.PointF(300, 100),
}),
new Aspose.Imaging.Shapes.RectangleShape(new Aspose.Imaging.RectangleF(0, 100, 200, 200)),
});
// 使用黄色画笔填充路径,并使用绿色钢笔绘制轮廓
graphics.FillPath(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Green, 2), new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Yellow), pathToFill);
// 创建用于绘制的路径
Aspose.Imaging.GraphicsPath pathToDraw = new Aspose.Imaging.GraphicsPath();
Aspose.Imaging.Figure figureToDraw = new Aspose.Imaging.Figure();
pathToDraw.AddFigure(figureToDraw);
figureToDraw.AddShapes(new Aspose.Imaging.Shape[]
{
new Aspose.Imaging.Shapes.ArcShape(new Aspose.Imaging.RectangleF(200, 200, 200, 200), 0, 360),
});
// 使用宽度为5像素的橙色钢笔绘制路径。
graphics.DrawPath(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Orange, 5), pathToDraw);
// 为了光栅化 SVG,我们需要指定光栅化选项。
Aspose.Imaging.ImageOptions.SvgRasterizationOptions rasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions();
Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
saveOptions.VectorRasterizationOptions = rasterizationOptions;
// 获取包含所有绘图命令的最终 WMF 图像
using (Aspose.Imaging.FileFormats.Wmf.WmfImage wmfImage = graphics.EndRecording())
{
wmfImage.Save(dir + "test.output.wmf");
}
此示例展示了如何使用 EmfRecorderGraphics2D 创建 EMF 图像并在其上绘制一些几何形状。
[C#]
string dir = "c:\\temp\\";
// 图像的像素大小
int deviceWidth = 600;
int deviceHeight = 400;
// 图像的毫米大小
int deviceWidthMm = (int)(deviceWidth / 100f);
int deviceHeightMm = (int)(deviceHeight / 100f);
Aspose.Imaging.Rectangle frame = new Aspose.Imaging.Rectangle(0, 0, deviceWidth, deviceHeight);
// 创建一个 EMF 图像。
Aspose.Imaging.FileFormats.Emf.Graphics.EmfRecorderGraphics2D graphics =
new Aspose.Imaging.FileFormats.Emf.Graphics.EmfRecorderGraphics2D(
frame,
new Aspose.Imaging.Size(deviceWidth, deviceHeight),
new Aspose.Imaging.Size(deviceWidthMm, deviceHeightMm));
// 使用 1 像素宽的黑色笔在图像边缘绘制一个黑色矩形。
graphics.DrawRectangle(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Black, 1), 0, 0, deviceWidth, deviceHeight);
// 使用 white-smoke 颜色填充矩形。
graphics.FillRectangle(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.WhiteSmoke), new Aspose.Imaging.Rectangle(10, 10, 580, 380));
// 使用 1 像素宽的 darkgreen 笔绘制两条对角线。
graphics.DrawLine(new Aspose.Imaging.Pen(Aspose.Imaging.Color.DarkGreen, 1), 0, 0, deviceWidth, deviceHeight);
graphics.DrawLine(new Aspose.Imaging.Pen(Aspose.Imaging.Color.DarkGreen, 1), 0, deviceHeight, deviceWidth, 0);
// 使用 2 像素宽的蓝色笔在矩形 {0, 0, 200, 200} 内绘制弧线。
graphics.DrawArc(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Blue, 2), new Aspose.Imaging.Rectangle(0, 0, 200, 200), 90, 270);
// 填充弧线
graphics.FillPie(new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.LightSkyBlue), new Aspose.Imaging.Rectangle(0, 0, 150, 150), 90, 270);
// 使用 2 像素宽的红色笔绘制三次贝塞尔曲线。
graphics.DrawCubicBezier(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Red, 2),
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(200, 133),
new Aspose.Imaging.Point(400, 166),
new Aspose.Imaging.Point(600, 400));
// 在指定位置绘制指定大小的光栅图像。
// 图像已缩放以适应所需的矩形。
using (Aspose.Imaging.RasterImage imageToDraw = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
graphics.DrawImage(imageToDraw,
new Aspose.Imaging.Rectangle(400, 200, 100, 50),
new Aspose.Imaging.Rectangle(0, 0, deviceWidth, deviceHeight),
Aspose.Imaging.GraphicsUnit.Pixel);
}
// 绘制文本字符串
graphics.DrawString("Hello World!", new Aspose.Imaging.Font("Arial", 48, Aspose.Imaging.FontStyle.Regular), Aspose.Imaging.Color.DarkRed, 200, 300);
// 创建用于填充的路径
Aspose.Imaging.Figure figureToFill = new Aspose.Imaging.Figure();
figureToFill.IsClosed = true;
Aspose.Imaging.GraphicsPath pathToFill = new Aspose.Imaging.GraphicsPath();
pathToFill.AddFigure(figureToFill);
figureToFill.AddShapes(new Shape[]
{
new Aspose.Imaging.Shapes.ArcShape(new Aspose.Imaging.Rectangle(400, 0, 200, 100), 45, 300),
new Aspose.Imaging.Shapes.BezierShape(
new Aspose.Imaging.PointF[]
{
new Aspose.Imaging.PointF(300, 200),
new Aspose.Imaging.PointF(400, 200),
new Aspose.Imaging.PointF(500, 100),
new Aspose.Imaging.PointF(600, 200),
}),
new Aspose.Imaging.Shapes.PolygonShape(
new Aspose.Imaging.PointF[]
{
new Aspose.Imaging.PointF(300, 100),
}),
new Aspose.Imaging.Shapes.RectangleShape(new Aspose.Imaging.RectangleF(0, 100, 200, 200)),
});
// 使用黄色画笔填充路径,并使用绿色钢笔绘制轮廓
graphics.FillPath(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Green, 2), new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Yellow), pathToFill);
// 创建用于绘制的路径
Aspose.Imaging.GraphicsPath pathToDraw = new Aspose.Imaging.GraphicsPath();
Aspose.Imaging.Figure figureToDraw = new Aspose.Imaging.Figure();
pathToDraw.AddFigure(figureToDraw);
figureToDraw.AddShapes(new Aspose.Imaging.Shape[]
{
new Aspose.Imaging.Shapes.ArcShape(new Aspose.Imaging.RectangleF(200, 200, 200, 200), 0, 360),
});
// 使用宽度为5像素的橙色钢笔绘制路径。
graphics.DrawPath(new Aspose.Imaging.Pen(Aspose.Imaging.Color.Orange, 5), pathToDraw);
// 为了光栅化 SVG,我们需要指定光栅化选项。
Aspose.Imaging.ImageOptions.SvgRasterizationOptions rasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions();
Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
saveOptions.VectorRasterizationOptions = rasterizationOptions;
// 获取包含所有绘图命令的最终 WMF 图像
using (Aspose.Imaging.FileFormats.Emf.EmfImage emfImage = graphics.EndRecording())
{
emfImage.Save(dir + "test.output.emf");
}
另请参见
- class Font
- struct Color
- class MetafileRecorderGraphics2D
- namespace Aspose.Imaging.FileFormats.Emf.Graphics
- assembly Aspose.Imaging
DrawString(string, Font, Color, int, int, float)
绘制字符串。
public void DrawString(string @string, Font font, Color color, int x, int y, float angle)
| 参数 | 类型 | 描述 |
|---|---|---|
| 字符串 | String | 该字符串。 |
| font | Font | 定义字符串文本格式的字体。 |
| 颜色 | 颜色 | 文本颜色。 |
| x | Int32 | 绘制文本左上角的 x 坐标。 |
| y | Int32 | 绘制文本左上角的 y 坐标。 |
| 角度 | 单精度 | 设备上转向向量与 x 轴之间的角度(以度为单位)。转向向量与文本行的基线平行。 |
另请参见
- class Font
- struct Color
- class MetafileRecorderGraphics2D
- namespace Aspose.Imaging.FileFormats.Emf.Graphics
- assembly Aspose.Imaging