TIFF(标签图像文件格式)因其能够存储高质量的光栅图形而广泛用于出版、图形设计和摄影。与 JPEGPNG 不同,TIFF 文件可以在一个文件中保存多个图像或帧。这些多帧 TIFF 图像通常用于医学成像、扫描文档和分层设计。

图像处理 API

Aspose.Imaging Cloud SDK for .NET 是一个基于 REST 架构的惊人 API,提供在线操纵和处理图像的能力。它为从多帧 TIFF 图像中提取 TIFF 帧提供了简单易用的方法,适用于 C# .NET 代码片段。该 SDK 可从 NuGetGitHub 下载。现在,安装 REST API 以开始操纵 TIFF 和其他图像格式。

Install-Package Aspose.Imaging-Cloud

Aspose Cloud dashboard 获取您的客户端 ID 和客户端密钥。

使用 C# 提取 TIFF 帧

本节解释了如何使用 C# .NET 轻松提取 TIFF 帧的详细信息。

  • 首先,创建一个 ImagingApi 的对象,同时提供 ClientID 和 ClientSecret 作为参数。
  • 其次,创建一个 GetImageFrameRequest 对象,它将 TIFF 名称、frameID 和其他参数作为参数输入。
  • 现在,调用 GetImageFrame(..) 方法的 ImagingApi,我们将 GetImageFrameRequest 对象作为输入参数。
  • 最后,保存提取的图像。
// 从 https://dashboard.aspose.cloud/ 获取 ClientID 和 ClientSecter。
string clientID = "XXXXX-XXXXXXXXXX-bff4-f5a14a4b6466"; 
string clientSecret = "XXXXXXXXXX";

// 创建 ImagingApi 的实例
ImagingApi imagingApi = new ImagingApi(clientSecret, clientID,"https://api.aspose.cloud/","v3.0",false);

// 输入的 TIFF 图像可在云存储中获得
String fileName = "TiffSampleImage.tiff";

int? frameId = 5; // Index of a frame
int? newWidth = 300;
int? newHeight = 450;
int? x = 10;
int? y = 10;
int? rectWidth = 200;
int? rectHeight = 300;

string rotateFlipMethod = "RotateNoneFlipNone";

// 结果将仅包括指定的框架
bool? saveOtherFrames = false;

string folder = null; // Input file is saved at the root of the storage
string storage = null; // We are using default Cloud Storage

GetImageFrameRequest getImageFrameRequest = new GetImageFrameRequest(fileName, frameId, newWidth, newHeight,
                                x, y, rectWidth, rectHeight, rotateFlipMethod, saveOtherFrames, folder, storage);

Stream imageFrame = imagingApi.GetImageFrame(getImageFrameRequest);

// 将更新后的图像保存到本地存储
using (var fileStream = File.Create("/Users/nayyer/Downloads/MyResultant.tiff"))
{
    imageFrame.Seek(0, SeekOrigin.Begin);
    imageFrame.CopyTo(fileStream);
}

我们可以提供任何值给 rotateFlipMethod 属性。

RotateFlip 方法 (Rotate180FlipNone, Rotate180FlipX, Rotate180FlipXY, Rotate180FlipY, Rotate270FlipNone, Rotate270FlipX, Rotate270FlipXY, Rotate270FlipY, Rotate90FlipNone, Rotate90FlipX, Rotate90FlipXY, Rotate90FlipY, RotateNoneFlipNone, RotateNoneFlipX, RotateNoneFlipXY, RotateNoneFlipY)。 默认是 RotateNoneFlipNone。

使用 cURL 获取 TIFF 框架

此外,我们还可以使用 cURL 命令来执行此操作。您可以考虑使用以下任一 API 调用来执行此操作。

  1. GET /imaging/{name}/frames/{frameId} - 从云存储加载图像并在响应中获取图像。
  2. POST /imaging/frames/{frameId} - 在请求体中传递 TIFF,并在响应中获取结果图像。

第一步是根据您的 ClientID 和 ClientSecret 生成 JSON Web Token (JWT)。因此,在终端中执行以下命令以生成 JWT 令牌:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=XXXXXX-XXXXX-XXX-bff4-f5a14a4b6466&client_secret=XXXXXXXXXXX" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

现在,执行以下命令以从 TiffSampleImage.tiff 中提取第二帧,该图像已上传到云存储。

curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/TiffSampleImage.tiff/frames/2?saveOtherFrames=true" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-o myresultant.tiff

结论

从多帧 TIFF 图像中提取单独帧的能力在处理扫描的多页文档、分层图像文件或医学成像数据集时非常有价值。使用 Aspose.Imaging Cloud SDK for .NET,您可以轻松自动化此过程,使用 REST API 调用或 C# 代码。

推荐文章

我们推荐您访问以下链接以了解: