翻譯|行業資訊|編輯:胡濤|2023-10-11 09:40:06.397|閱讀 128 次
概述:在這篇博文中,我們將學習如何在 C# 中向圖像添加文本,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在圖像中添加文本是添加上下文、品牌的好方法。它可用于建模、社交媒體帖子、營銷材料等。在這篇博文中,我們將學習如何在 C# 中向圖像添加文本。我們將逐步指導您如何在JPG或PNG格式的照片或其他圖像上書寫。讓我們開始吧!
Aspose.Drawing 是具有System的跨平臺2D繪圖引擎,兼容繪圖API。繪圖庫支持將直線、曲線和圖形等矢量圖形以及各種字體、大小和樣式的文本呈現到光柵圖像上。支持BMP, PNG, JPEG, GIF,和TIFF等格式。Aspose API 支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
我們將使用Aspose.Drawing for .NET向圖像添加文本。它是一個功能強大且多功能的 2D 圖形庫,允許開發人員在各種應用程序中創建和操作圖形。Aspose.Drawing for .NET 支持廣泛的圖像處理操作,例如裁剪、調整大小、旋轉、翻轉和水印。對于需要為其 .NET 應用程序提供跨平臺、高性能圖形庫的開發人員來說,它是一個不錯的選擇。
請下載API的DLL或使用NuGet安裝。
PM> Install-Package Aspose.Drawing
我們可以按照以下步驟向 JPG 圖像添加文本:
以下代碼示例演示如何使用 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");
	 
 
同樣,我們可以按照前面提到的步驟向 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");
	 
 
我們還可以按照以下步驟為照片添加標題:
以下代碼示例展示了如何使用 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# 中向圖像添加文本。我們介紹了以編程方式在照片和圖像上編寫文本的基礎知識以及一些更高級的技術。此外,我們還推出了一個免費的在線工具,可以隨時隨地向圖像添加文本。如有任何疑問,歡迎聯系我們~
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn