本文将指导您如何使用 C# 在 JPG 上编写文本。它包含 IDE 设置、步骤列表和详细的示例代码,演示如何使用 C# 向 JPG 文件添加文本。您将学习如何通过设置各种文本属性和图像背景来装饰 JPG 中的文本。
使用 C# 将文本添加到 JPG 的步骤
- 设置环境以使用 Aspose.Drawing for .NET 在 JPG 上呈现文本
- 创建 Bitmap 对象并使用它来创建 Graphics 对象以便在其上进行绘图
- 自定义 Graphics 对象来设置渲染质量
- 在 Graphics 对象中定义背景渐变颜色
- 创建字体并绘制两次字符串以获得阴影效果
- 在文本周围绘制可选边框并将其保存在磁盘上
这些步骤描述了如何在 C# 中向 JPG 文件中添加文本。您可以使用 DrawString() 方法绘制文本,方法是创建位图、将其与 Graphics 对象链接,然后创建画笔、字体和边界矩形。可选设置包括设置渲染质量、背景渐变颜色和文本样式。
使用 C# 在 JPG 中插入文本的代码
using System; | |
using Aspose.Drawing; | |
using Aspose.Drawing.Drawing2D; | |
using Aspose.Drawing.Text; | |
class AddTextToJpg | |
{ | |
static void Main() | |
{ | |
new License().SetLicense("license.lic");// License for decorated image creation | |
// Define dimensions for the output image | |
int imageWidth = 800;// Output image width | |
int imageHeight = 600;// Output image height | |
// Create the bitmap | |
using (Bitmap canvas = new Bitmap(imageWidth, imageHeight)) | |
{ | |
using (Graphics graphics = Graphics.FromImage(canvas))//Graphics object for the canvas | |
{ | |
graphics.SmoothingMode = SmoothingMode.HighQuality;// Smoothing mode | |
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;// Rendering hint | |
// Define the background gradient colors and fill the canvas | |
Color gradientStart = Color.SkyBlue; | |
Color gradientEnd = Color.Navy; | |
using (LinearGradientBrush gradientBrush = new LinearGradientBrush(//The gradient brush | |
new PointF(0, 0),// Starting point | |
new PointF(imageWidth, imageHeight),// Ending Point | |
gradientStart,// Gradient color start | |
gradientEnd)) // Gradient color end | |
{ | |
graphics.FillRectangle(gradientBrush, 0, 0, imageWidth, imageHeight); | |
} | |
string message = "The sample decorated text with shadow"; | |
Font textFont = new Font("Tahoma", 24, FontStyle.Bold | FontStyle.Italic);// Text style | |
SizeF textSize = graphics.MeasureString(message, textFont);// Text size | |
float textX = (imageWidth - textSize.Width) / 2; // Text center X | |
float textY = (imageHeight - textSize.Height) / 2; // Text center Y | |
Color shadowColor = Color.FromArgb(100, 0, 0, 0); // Semi-transparent black | |
using (Brush shadowBrush = new SolidBrush(shadowColor))// Shadow brush | |
{ | |
graphics.DrawString(message, textFont, shadowBrush, textX + 5, textY + 5); | |
} | |
Color mainTextColor = Color.White;//main text with white color | |
using (Brush textBrush = new SolidBrush(mainTextColor)) | |
{ | |
graphics.DrawString(message, textFont, textBrush, textX, textY); | |
} | |
Color borderColor = Color.White;// Border color | |
int borderThickness = 4; // Border thickness in pixel | |
using (Pen borderPen = new Pen(borderColor, borderThickness)) | |
{ | |
graphics.DrawRectangle(borderPen, 0, 0, imageWidth - 1, imageHeight - 1); | |
} | |
} | |
string outputFilePath = "TextOnImage.jpg";// Image output | |
canvas.Save(outputFilePath, Aspose.Drawing.Imaging.ImageFormat.Jpeg); | |
} | |
} | |
} |
此代码演示了如何在 C# 中向 JPEG 文件添加文本。为了添加渐变色,我们使用了 LinearGradientBrush,它采用渐变的起始位置、结束位置、起始颜色和结束颜色。为了添加阴影效果,目标字符串用不同的颜色绘制两次。
本文教我们创建 JPEG 图像并向其中添加装饰文本的过程。有关缩放图像,请参阅 在 C# 中缩放图像 上的文章。