如何使用 C# 在 Tiff 中添加水印

在此操作示例中,我们将引导您完成如何使用 C# 在 TIFF 中添加水印的步骤。通过执行少量 API 调用,使用 C# 在 TIFF 中插入水印非常简单。

使用 C# 插入 Tiff 水印的步骤

  1. 安装 Aspose.Imaging for .NET NuGet 包
  2. 包括 Aspose.ImagingAspose.Imaging.ImageOptionsAspose.Imaging.Brushes 命名空间
  3. 使用 Image.Load 方法加载 Tiff 并添加 Tiff 水印
  4. 实例化 GraphicsFontSolidBrushStringFormat 类对象来设置水印的字体、颜色和文本属性
  5. 在 C# 中使用 Graphics 类对象保护带有水印的 Tiff
  6. 使用 Tiff 水印保存输出图像

在上述步骤中,我们首先使用 Image 类的 Load 方法将 Tiff 图像加载到内存中。然后我们为字体、画笔和文本格式指定了属性,以在 C# 中使用水印保护 Tiff。最后,我们使用 C# 保存 Tiff 水印。

使用 C# 插入 Tiff 水印的代码

using System;
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.ImageOptions;
namespace AddingTiffWatermark
{
class TiffWatermark
{
static void Main(string[] args)
{
string PathForTiffFile = @"Y:\Downloads\";
//Load the License file
License license = new License();
license.SetLicense(PathForTiffFile + "Conholdate.Total.Product.Family.lic");
// Use Image.Load to add tiff watermark using c# in Tiff file
using (var image = Aspose.Imaging.Image.Load(PathForTiffFile + "TiffToWatermark.tiff"))
{
// Initialize Graphics class instance for loaded Tiff Image
Graphics graphics = new Aspose.Imaging.Graphics(image);
// Initialize SizeF to store image Size
Size size = graphics.Image.Size;
// Create an instance of Font to set the font Name, Size and Style
Font font = new Aspose.Imaging.Font("Arial", 24,
Aspose.Imaging.FontStyle.Bold);
// Instantiate SolidBrush and set Color and Opacity
SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush();
brush.Color = Aspose.Imaging.Color.Red;
brush.Opacity = 0;
// initialize an object of StringFormat class and
// set its various textual properties
StringFormat format = new Aspose.Imaging.StringFormat();
format.Alignment = Aspose.Imaging.StringAlignment.Near;
format.FormatFlags = Aspose.Imaging.StringFormatFlags.FitBlackBox;
// Render the string on image with set font and brush
graphics.DrawString("PROTECTED", font,
brush, 0, 0, format);
// Save protected tiff with watermark in c#
image.Save(PathForTiffFile+"WatermarkedTiff.tiff");
}
}
}
}

使用上面的示例,您可以在 .NET 项目(包括 Windows 桌面、ASP.NET Web 或控制台应用程序)中使用 C# 中的水印轻松保护 Tiff。

 简体中文