在这篇基础文章中,您将学习如何使用 NET REST API 将 SVG 转换为 JPEG2000。我们将按照以下部分中提到的步骤,使用 C# Low Code API 开发一个 SVG 到 JPEG2000 转换器。开发的应用程序可以与 Windows、macOS 或 Linux 环境中支持的任何 .NET 应用程序集成,并且可以免费进行转换。
先决条件
创建账户并获取 API 凭证 执行 SVG 到 JPEG2000 的转换
下载 Aspose.Imaging Cloud SDK for .NET to convert SVG to JPEG2000
使用上述 SDK 设置 C# .NET 项目,以将 SVG 渲染为 JPEG2000
使用 NET REST API 将 SVG 转换为 JPEG2000 的步骤
- 设置 API 的客户端 ID 和客户端密钥以将 SVG 转换为 JPEG2000
- 使用客户端凭据创建 ImagingAPI 类的实例,以执行 SVG 到 JPEG2000 的转换
- 指定源 SVG 和输出 JPEG2000 文件名
- 访问并加载源 SVG 文件并将其上传到云存储
- 使用输入 SVG 文件流和输出 JPEG2000 格式创建 ConvertImageRequest 实例
- 调用 ConvertImage 方法使用 NET REST API 将 SVG 转换为 JPEG2000
- 将渲染的 JPEG2000 文件流保存在本地磁盘上
简要步骤包括使用 C# Low Code API 将文件类型从 SVG 渲染为 JPEG2000。我们将首先使用 ImagingAPI 类的实例初始化 SDK。然后,我们将使用磁盘中的 FileStream 获取源 SVG 文件,然后使用 ConvertImageRequest 类实例使用 ConvertImage() 方法执行到 JPEG2000 的转换。
NET Low Code API 中 SVG 到 JPEG2000 转换的代码
using Aspose.Imaging.Cloud.Sdk.Api; | |
using Aspose.Imaging.Cloud.Sdk.Model.Requests; | |
using Aspose.Imaging.Cloud.Sdk.Model; | |
using System; | |
using System.IO; | |
namespace Kb_Aspose.KB | |
{ | |
public class SvgToJp2Converter | |
{ | |
public void SvgToJp2() | |
{ | |
var clientID = "Client ID"; | |
var clientSecret = "Client Secret"; | |
var apiBaseUrl = "https://api.aspose.cloud"; | |
var localPath = "C:/Words/"; | |
var svgToJp2ImageApi = new ImagingApi(clientSecret, clientID, apiBaseUrl); | |
// Source and output file names | |
var inputFileName = "Source.svg"; | |
var outputFileName = "SvgtoJp2.jp2"; | |
var outputFormat = "jp2"; | |
var remoteFolder = null; // source file is saved at the root of the storage | |
var remoteStorage = null; // remote cloud Storage place name | |
try | |
{ | |
// Upload the sample SVG image file to Cloud Storage | |
var inpuFileStream = File.Open(localPath + '/' + inputFileName, FileMode.Open); | |
inpuFileStream.Position = 0; | |
var uploadSvgFileReq = new UploadFileRequest(inputFileName, inpuFileStream, null); | |
svgToJp2ImageApi.UploadFile(uploadSvgFileReq); | |
var convertSvgToJp2Request = new ConvertImageRequest(inputFileName, outputFormat, | |
remoteFolder, remoteStorage); | |
var jp2DataStream = svgToJp2ImageApi.ConvertImage(convertSvgToJp2Request); | |
jp2DataStream.Position = 0; | |
using (var fileStream = File.Create(localPath + outputFileName + "." + outputFormat)) | |
{ | |
jp2DataStreamDataStream.Seek(0, SeekOrigin.Begin); | |
jp2DataStream.CopyTo(fileStream); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} |
This precise example code exhibits how to convert SVG to JPEG2000 with C# Cloud API. After fulfilling the required pre-requisite steps, you have to provide a path to load the source SVG image on the disk using Aspose.Imaging REST API SDK completes the rendering to JPEG2000. 转换完成后得到响应的JPEG2000图像文件流,然后您可以将其保存在本地磁盘上。
在本文中,我们介绍了使用 Cloud API 将 SVG 转换为 JPEG2000。如果您有兴趣执行 SVG 到 GIF 的转换,请参阅有关如何 使用 NET REST API 将 SVG 转换为 GIF 的文章。