Created
June 23, 2024 12:53
-
-
Save aspose-cloud-kb/5209da5c7df4cd52becab42fd83427d3 to your computer and use it in GitHub Desktop.
Convert PNG to JPG with NET REST API. https://kb.aspose.cloud/imaging/net/convert-png-to-jpg-with-net-rest-api/
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 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 PngToJpgConverter | |
{ | |
public void PngToJpg() | |
{ | |
var clientID = "Client ID"; | |
var clientSecret = "Client Secret"; | |
var apiBaseUrl = "https://api.aspose.cloud"; | |
var localPath = "C:/Words/"; | |
var pngToJpgImageApi = new ImagingApi(clientSecret, clientID, apiBaseUrl); | |
// Source and output file names | |
var inputFileName = "Source.png"; | |
var outputFileName = "PngtoJpg.jpg"; | |
var outputFormat = "jpg"; | |
var remoteFolder = null; // source file is saved at the root of the storage | |
var remoteStorage = null; // remote cloud Storage place name | |
try | |
{ | |
// Upload the local PNG image to Cloud Storage | |
var inpuFileStream = File.Open(localPath + '/' + inputFileName, FileMode.Open); | |
inpuFileStream.Position = 0; | |
var uploadPngFileReq = new UploadFileRequest(inputFileName, inpuFileStream, null); | |
pngToJpgImageApi.UploadFile(uploadPngFileReq); | |
var convertPngToJpgRequest = new ConvertImageRequest(inputFileName, outputFormat, | |
remoteFolder, remoteStorage); | |
var jpgDataStream = pngToJpgImageApi.ConvertImage(convertPngToJpgRequest); | |
jpgDataStream.Position = 0; | |
using (var fileStream = File.Create(localPath + outputFileName + "." + outputFormat)) | |
{ | |
jpgDataStream.Seek(0, SeekOrigin.Begin); | |
jpgDataStream.CopyTo(fileStream); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment