在 C# 中為影像新增標註 |照片標註|圖像標註

圖像標註是一種圖形元素,有助於吸引人們對圖像或照片的特定部分的注意。標註是讓任何視覺內容更清晰、更具影響力的秘密武器。將它們視為照亮影像最重要部分的微小聚光燈。在本文中,我們將學習如何在 C# 中為圖像添加標註。

本文涵蓋以下主題:

  1. 什麼是影像標註
  2. 用於新增影像標註的 C# API
  3. 如何在 C# 中為影像新增標註
  4. 免費資源

什麼是圖像標註?

圖像標註是疊加在圖像上的資訊標籤,用於引起對特定部分的注意並解釋其重要性。圖像標註可以採用引導線或箭頭的形式,在視覺上將標註文字連接到圖像的突出顯示區域。它們可以是標註文字框,包含所指出元素的簡短說明或標識。

影像標註常用於:

  • 技術文件和教學:以螢幕截圖或圖表指出特定特性或功能。
  • 演示:強調圖像中的關鍵元素。
  • 教育材料:為插圖或圖表的各個部分加上標籤。

用於新增圖像標註的 C# API

為了在 C# 中以程式設計方式新增影像標註,我們將使用 Aspose.Drawing for .NET API。它是一個跨平台 2D 圖形庫,用於以程式設計方式繪製文字、幾何圖形和圖像。它提供了創建標註並將其添加到圖像的功能。

下載 API 的 DLL 或使用 NuGet 安裝它。

PM> Install-Package Aspose.Drawing

如何在 C# 中為圖像添加標註

我們可以按照以下步驟輕鬆地以程式設計方式為圖像添加標註:

  1. 使用 Image.FromFile() 方法載入映像。
  2. 使用 FromImage() 方法建立一個新的 Graphics 物件。
  3. 使用指定的顏色和大小初始化 Pen 類別物件來繪製標註。
  4. 使用指定的顏色和大小定義標註元素的位置。
  5. 之後,使用 DrawString() 方法寫入文字。
  6. 最後,使用 Save() 方法儲存輸出影像。

以下程式碼範例展示如何在 C# 中向圖像添加標註。

using System.Drawing;
using System.Drawing.AsposeDrawing;
using System.Drawing.Imaging;
using System.Drawing.Text;

using (var image = Image.FromFile("gears.png"))
{
    var graphics = Graphics.FromImage(image);

    graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
    graphics.PageUnit = GraphicsUnit.Pixel;

    DrawCallOut(graphics, new PointF(107, 55), new PointF(179, 5), 74, "mm");

    DrawCallOut(graphics, new PointF(111, 146), new PointF(29, 180), 28, "mm");

    image.Save("gears_with_callout.jpg");
}


void DrawCallOut(Graphics graphic, PointF startAnchor, PointF endAnchor, int value, string unit)
{
    Pen pen = new Pen(Color.Red, 1);

    Font font = new Font("Arial", 10, FontStyle.Bold);

    string outputValue = $"{value} {unit}";

    var textSize = graphic.MeasureString(outputValue, font);

    int diameterSymbolSize = 12;
    int spaceSize = 3;

    textSize.Width += diameterSymbolSize + spaceSize;

    float callOutMiddleX = endAnchor.X > startAnchor.X ? endAnchor.X - textSize.Width : endAnchor.X + textSize.Width;
    float callOutMiddleY = endAnchor.Y > startAnchor.Y ? endAnchor.Y - textSize.Height : endAnchor.Y + textSize.Height;

    graphic.DrawLine(pen, startAnchor.X, startAnchor.Y, callOutMiddleX, callOutMiddleY);

    float textAnchorX = Math.Min(callOutMiddleX, endAnchor.X);
    float textAnchorY = callOutMiddleY;
    graphic.DrawLine(pen, callOutMiddleX, callOutMiddleY,
                        textAnchorX == callOutMiddleX ? textAnchorX + textSize.Width : textAnchorX,
                        callOutMiddleY);

    graphic.DrawEllipse(pen, new Rectangle((int)textAnchorX + spaceSize,
                                            (int)(textAnchorY - textSize.Height) + spaceSize, 10, 10));

    graphic.DrawLine(pen, (int)textAnchorX + 1, (int)textAnchorY - 1,
                            (int)textAnchorX + diameterSymbolSize + 2,
                            (int)textAnchorY - diameterSymbolSize - 2);

    SolidBrush brush = new SolidBrush(Color.Blue);

    graphic.DrawString(outputValue, font, brush, (int)textAnchorX + diameterSymbolSize + spaceSize,
                                                    (int)(textAnchorY - textSize.Height));
}
在 C# 中為影像新增標註

在 C# 中為影像新增標註

取得免費許可證

您可以獲得免費的臨時許可證來嘗試 Aspose.Drawing for .NET,而不受評估限制。

照片標註 – 免費資源

除了為照片和圖像添加標註之外,您還可以使用以下資源探索該庫的各種其他功能:

結論

在這篇文章中,我們學習如何在 C# 中以程式設計方式為圖像和照片添加標註。為影像添加標註是增強內容視覺吸引力和可用性的簡單而有效的方法。透過遵循概述的步驟並使用提供的程式碼範例,您可以輕鬆地將注意力吸引到圖像中的關鍵元素。如有任何疑問,請隨時透過我們的免費支援論壇與我們聯繫。

也可以看看