本文解释了 C# 中的 image 裁剪。它包括环境配置详细信息、分步过程以及在 C# 中制作 剪辑照片 的代码片段。此外,您无需安装任何其他工具或应用程序即可使用此功能。
在 C# 中创建剪切照片的步骤
- 准备环境以使用 Aspose.Drawing for .NET 剪辑图像
- 创建一个 Bitmap 类的对象,同时指定宽度、高度和像素格式
- 使用 Graphics 类封装绘图表面
- 使用直角坐标定义剪辑
- 使用 GraphicsPath 类将一系列连接的直线和曲线定义为路径
- 添加形状或路径并设置剪辑
- 加载源图像并在指定位置绘制后再保存
这些步骤概述了 C# 中的照片剪辑 的程序流程。首先,创建一个新的位图,初始化图形,并定义剪辑。随后,加载图像、绘制图像并将其导出为剪切照片。
C# 中的照片剪辑代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Aspose.Drawing.Imaging; | |
using Aspose.Drawing; | |
using Aspose.Drawing.Drawing2D; | |
using Aspose.Drawing.Text; | |
class Program | |
{ | |
static void Main(string[] args) // Image clipping in C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Create a new bitmap | |
var bitmap = new Bitmap(225, 225, PixelFormat.Format32bppPArgb); | |
// Initialize Graphics | |
Graphics graphics = Graphics.FromImage(bitmap); | |
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
// Define Clip | |
Rectangle rect = new Rectangle(0, 0, 225, 225); | |
GraphicsPath clipPath = new GraphicsPath(); | |
clipPath.AddEllipse(rect); graphics.SetClip(clipPath); | |
// Load the image | |
Bitmap image = new Bitmap("aspose-logo.png"); | |
// Draw image at specified location | |
graphics.DrawImage(image, 0, 0); | |
bitmap.Save("clipping.png"); | |
Console.WriteLine("Image clipped successfully"); | |
} | |
} |
此代码片段演示了在 C#* 中使用*图像剪切路径的过程。但是,您可以通过更改图像尺寸或像素格式来进一步增强它。然后,您还可以根据您的要求修改剪切路径的矩形值或添加圆弧、多边形、贝塞尔曲线等。
本教程解释了 C# 中的图像剪切路径。然而,如果您想学习创建位图图像,请阅读有关 如何在 C# 中创建位图 的文章。