C# 中的圖像裁剪

圖像剪輯,也稱為圖像遮罩或對象提取,是一種用於圖形設計和圖像處理的技術。它用於從圖像中隔離特定的對像或主題。在本文中,我們將探索 C# 中的圖像剪輯實現。學習分步技術來提取對象並增強您的設計。

本文應涵蓋以下主題:

  1. C# Image Clipping API - 免費下載
  2. 如何實現C#圖像裁剪
  3. 在 C# 中從圖像中提取對象
  4. 免費學習資源

C# Image Clipping API - 免費下載

為了在 C# 中實現圖像剪輯,我們將使用 Aspose.Drawing for .NET API。它是一個跨平台的 2D 圖形庫,用於以編程方式繪製文本、幾何圖形和圖像。它允許加載、保存和操作支持的文件格式

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

PM> Install-Package Aspose.Drawing

C# 圖像裁剪

我們可以按照下面給出的步驟以編程方式實現圖像裁剪:

  1. 首先,創建 Bitmap 類的實例。
  2. 接下來,使用 FromImage() 方法創建 Graphics 類對象。
  3. 然後,使用 GraphicsPath 類定義剪輯路徑。
  4. 之後,使用 SetClip() 方法設置剪輯路徑。
  5. 最後,使用 Save() 方法保存輸出圖像。

下面的代碼示例展示瞭如何在 C# 中實現圖像裁剪。

// 創建一個新的位圖
var bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

// 初始化圖形
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

// 定義剪輯
Rectangle rectangle = new Rectangle(200, 200, 600, 400);
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(rectangle);
graphics.SetClip(clipPath);

// 指定字符串
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.Red);
Font arial = new Font("Arial", 20, FontStyle.Regular);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
    "Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. " +
    "Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. " +
    "Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, " +
    "eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. " +
    "Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. " +
    "Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. " +
    "Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. " +
    "Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";

// 拉繩
graphics.DrawString(text, arial, brush, rectangle, stringFormat);

// 節省
bitmap.節省("C:\\Files\\Clipping.png");
C# 圖像裁剪

C# 圖像裁剪

在 C# 中從圖像中提取對象

我們可以按照前面提到的步驟使用圖像剪輯來提取圖像的特定部分。但是,我們只需要加載現有圖像並使用 DrawImage() 方法繪製它。

以下代碼示例展示瞭如何在 C# 中使用從圖像中提取對象。

// 創建一個新的位圖
var bitmap = new Bitmap(225, 225, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

// 初始化圖形
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

// 定義剪輯
Rectangle rectangle = new Rectangle(0, 0, 225, 225);
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(rectangle);
graphics.SetClip(clipPath);

// 加載圖像
Bitmap image = new Bitmap(@"C:\Files\aspose-logo.png");

// 在指定位置繪製圖像
graphics.DrawImage(image,0,0);

bitmap.Save("C:\\Files\\extract-object.png");
在 C# 中從圖像中提取對象

在 C# 中從圖像中提取對象

獲得免費的臨時許可證

您可以獲得免費的臨時許可證 試用 Aspose.Drawing for .NET,沒有評估限制。

Aspose.Drawing – 免費學習資源

除了 C# 中的圖像剪輯之外,您還可以使用下面提供的資源了解有關庫的各種功能的更多信息:

結論

在本文中,我們學習瞭如何在 C# 中實現圖像裁剪。按照分步說明和代碼示例,您可以輕鬆地從圖像中精確提取對象。如有任何疑問,請隨時通過我們的 免費支持論壇 與我們聯繫。

也可以看看