图像中的水印通常用于声明内容的所有权。另一方面,对图像加水印,以避免未经授权的使用或伪造。在各种情况下,可能需要在Web或桌面应用程序中自动执行水印功能。
- 在C#中向图像添加水印
- 在C#中向图像添加对角水印
为了向图像添加水印,我们将使用Aspose.Imaging。它是功能强大的图像处理API,支持多种图像格式。此外,API使您更轻松地操作图像。还没使用过的朋友可以最新版

在C#中向图像添加水印
以下是使用C#向图像添加水印的步骤。
- 使用Image类加载图像。
- 创建Graphics类的实例,并使用Image对象对其进行初始化。
- 使用Font类设置字体系列,大小和外观。
- 创建SolidBrush类的实例并设置其属性,例如颜色等。
- 实例化StringFormat类以设置文本对齐方式。
- 使用Graphics.DrawString(String,Font,SolidBrush,0,0,StringFormat)方法将水印添加到图像。
- 使用Image.Save(String)方法保存图像。
以下代码示例显示了如何在C#中向图像添加水印。
// Load an existing PNG imageusing (Image image = Image.Load("image.png")){ // Declare a String object with Watermark Text string theString = "This is watermark"; // Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size Graphics graphics = new Graphics(image); SizeF sz = graphics.Image.Size; // Creates an instance of Font, initialize it with Font Face, Size and Style Font font = new Font("Times New Roman", 20, FontStyle.Bold); // Create an instance of SolidBrush and set its various properties SolidBrush brush = new SolidBrush(); brush.Color = Color.Red; brush.Opacity = 0; // Initialize an object of StringFormat class and set its various properties StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; // Draw the string on Image Save output to disk graphics.DrawString(theString, font, brush, 0, 0, format); image.Save("watermarked.jpg");}

在C#中向图像添加对角水印
在各种情况下,水印对角地应用于图像。为此,Aspose.Imaging for .NET允许以特定角度转换水印文本。以下是向图像添加对角水印的步骤。
- 使用Image类加载图像。
- 创建Graphics类的实例,并使用Image对象对其进行初始化。
- 使用Font类设置字体系列,大小和外观。
- 创建SolidBrush类的实例并设置其属性,例如颜色等。
- 实例化StringFormat类以设置文本对齐方式。
- 创建Matrix类的实例并设置变换角度。
- 将Matrix对象分配给Graphics.Transform属性。
- 使用Graphics.DrawString(String,Font,SolidBrush,0,0,StringFormat)方法添加水印。
- 使用Image.Save(String)方法保存图像。
下面的代码示例演示如何使用C#向图像添加对角水印。
// Load an existing PNG imageusing (Image image = Image.Load("Image.png")){ // Declare a String object with Watermark Text string theString = "45 Degree Rotated Text"; // Create and initialize an instance of Graphics class and Initialize an object of SizeF to store image Size Graphics graphics = new Graphics(image); SizeF sz = graphics.Image.Size; // Creates an instance of Font, initialize it with Font Face, Size and Style Font font = new Font("Times New Roman", 20, FontStyle.Bold); // Create an instance of SolidBrush and set its various properties SolidBrush brush = new SolidBrush(); brush.Color = Color.Red; brush.Opacity = 0; // Initialize an object of StringFormat class and set its various properties StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; // Create an object of Matrix class for transformation Matrix matrix = new Matrix(); // First a translation then a rotation matrix.Translate(sz.Width / 2, sz.Height / 2); matrix.Rotate(-45.0f); // Set the Transformation through Matrix graphics.Transform = matrix; // Draw the string on Image Save output to disk graphics.DrawString(theString, font, brush, 0, 0, format); image.Save("watermarked.png");}

如果你想试用Aspose的全部完整功能,可 联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!