在图像中添加文本是添加上下文、品牌的好方法。它可用于建模、社交媒体帖子、营销材料等。在这篇博文中,我们将学习如何在 C# 中向图像添加文本。我们将逐步指导您如何在JPG或PNG格式的照片或其他图像上书写。让我们开始吧!
Aspose.Drawing 是具有System的跨平台2D绘图引擎,兼容绘图API。绘图库支持将直线、曲线和图形等矢量图形以及各种字体、大小和样式的文本呈现到光栅图像上。支持BMP, PNG, JPEG, GIF,和TIFF等格式。Aspose API 支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
Aspose.Drawing 最新下载(qun:761297826)https://www.evget.com/product/4237
用于向图像添加文本的 C# API
我们将使用Aspose.Drawing for .NET向图像添加文本。它是一个功能强大且多功能的 2D 图形库,允许开发人员在各种应用程序中创建和操作图形。Aspose.Drawing for .NET 支持广泛的图像处理操作,例如裁剪、调整大小、旋转、翻转和水印。对于需要为其 .NET 应用程序提供跨平台、高性能图形库的开发人员来说,它是一个不错的选择。
请下载API的DLL或使用NuGet安装。
PM> Install-Package Aspose.Drawing
在 C# 中向 JPG 图像添加文本
我们可以按照以下步骤向 JPG 图像添加文本:
- 使用Bitmap类加载 JPG 图像。
- 使用 FromImage() 方法从 Bitmap 对象创建一个新的Graphics对象。
- 使用指定的文本颜色初始化SolidBrush类对象。
- 使用所需的文本字体系列、样式和大小定义Font类对象。
- (可选)初始化一个Rectangle对象。
- 之后,使用要显示的文本、Font、Brush 和 Rectangle 类对象作为参数调用DrawString()方法。
- 最后,使用Save()方法保存输出图像。
以下代码示例演示如何使用 C# 将文本添加到 JPG 图像。
// Load the image Bitmap bitmap = new Bitmap("C:\\Files\\Sample_JPG.jpg"); Graphics graphics = Graphics.FromImage(bitmap); // Define text color Brush brush = new SolidBrush(Color.DarkBlue); // Define text font Font arial = new Font("Arial", 25, FontStyle.Regular); // Text to display string text = "Hello, this is a sample text!"; // Define rectangle Rectangle rectangle = new Rectangle(100, 100, 450, 100); // Draw text on image graphics.DrawString(text, arial, brush, rectangle); // Save the output file bitmap.Save("C:\\Files\\DrawTextOnJpg.jpg");
在 C# 中将文本添加到 PNG 图像
同样,我们可以按照前面提到的步骤向 PNG 图像添加文本。但是,我们需要在第一步加载 PNG 图像。
以下代码示例演示如何使用 C# 将文本添加到 PNG 图像。
// Load the image Bitmap bitmap = new Bitmap("C:\\Files\\Sample_PNG.png"); Graphics graphics = Graphics.FromImage(bitmap); // Define text color Brush brush = new SolidBrush(Color.Red); // Define text font Font arial = new Font("Arial", 30, FontStyle.Regular); // Text to display string text = "Hello, this is a sample text!"; // Define rectangle Rectangle rectangle = new Rectangle(400, 1500, 1600, 150); // Specify rectangle border Pen pen = new Pen(Color.White, 2); // Draw rectangle graphics.DrawRectangle(pen, rectangle); // Draw text on image graphics.DrawString(text, arial, brush, rectangle); // Save the output file bitmap.Save("C:\\Files\\DrawText.png");
为照片添加标题 - 为照片添加文字
我们还可以按照以下步骤为照片添加标题:
- 使用Bitmap类加载照片图像。
- 使用加载图像的大小创建一个新位图,并添加标题的矩形大小。
- 使用 FromImage() 方法从 Bitmap 对象创建一个新的Graphics对象。
- 使用DrawImage()方法在新创建的图像上绘制加载的图像。
- 为标题框绘制一个填充矩形。
- 使用StringFormat类指定文本字符串格式。
- 定义文本、颜色和字体
- 之后,使用要显示的文本、Font、Brush 和 Rectangle 类对象作为参数调用DrawString()方法。
- 最后,使用Save()方法保存输出图像。
以下代码示例展示了如何使用 C# 为照片添加标题。
// Load the image Bitmap bitmap = new Bitmap("C:\\Files\\tower.jpg"); var imageHeight = bitmap.Height; var imageWidth = bitmap.Width; var textHeight = 50; // Create a new bitmap with a size of loaded image + rectangle for caption Bitmap img = new Bitmap(imageWidth, imageHeight + textHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); Graphics graphics = Graphics.FromImage(img); // Draw the loaded image on newly created image graphics.DrawImage(bitmap, 0, 0); // Draw a rectangle for caption box Rectangle rectangle = new Rectangle(0, imageHeight, imageWidth, textHeight); Brush fillColor = new SolidBrush(Color.White); Pen pen = new Pen(Color.White, 2); graphics.DrawRectangle(pen, rectangle); graphics.FillRectangle(fillColor, rectangle); // Specify text string format StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; // Text color Brush textColor = new SolidBrush(Color.Black); // Text font Font arial = new Font("Arial", 18, FontStyle.Regular); // Text to display string text = "Hello, this is a sample text!"; // Draw text graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; graphics.DrawString(text, arial, textColor, rectangle, stringFormat); // Save the output img.Save("C:\\Files\\DrawTextOnPhoto.jpg");
在这篇博文中,我们向您展示了如何在 C# 中向图像添加文本。我们介绍了以编程方式在照片和图像上编写文本的基础知识以及一些更高级的技术。此外,我们还推出了一个免费的在线工具,可以随时随地向图像添加文本。如有任何疑问,欢迎联系我~