Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active September 28, 2021 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/b906111d9be3ac135058c9ad468a4b56 to your computer and use it in GitHub Desktop.
Save aspose-cloud/b906111d9be3ac135058c9ad468a4b56 to your computer and use it in GitHub Desktop.
Code snippets for converting JPG to PNG using C#
// Get ClientID from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// create an instance of ImagingApi
ImagingApi imagingApi= new ImagingApi( clientSecret,clientID,"https://api.aspose.cloud/");
// path of input JPEG image
string imageFile = "jpg-to-png.jpeg";
// output file format
string format = "png";
// resultant file name
string resultantFile = "converted.png";
// load the file from local drive
using (var file = System.IO.File.OpenRead("/Users/nshahbaz/Desktop/" + imageFile))
{
var uploadFileRequest = new UploadFileRequest(imageFile, file);
// Upload original document to Cloud Storage
imagingApi.UploadFile(uploadFileRequest);
}
try
{
// Create ImageRequest
var request = new ConvertImageRequest(imageFile, format, null, null);
// initiate the conversion operation
Stream updatedImage = imagingApi.ConvertImage(request);
// print success message if conversion is successful
if (request != null && request.Equals("OK"))
{
Console.WriteLine("JPG successfully converted to PNG !");
Console.ReadKey();
}
// call the method to save output over system drive
saveToDisk(updatedImage, "/Users/nshahbaz/Desktop/"+resultantFile);
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
// custom method to save steam object as file instance
public static void saveToDisk(Stream responseStream, String resultantFile)
{
var fileStream = File.Create(resultantFile);
responseStream.Seek(0, SeekOrigin.Begin);
responseStream.CopyTo(fileStream);
fileStream.Close();
}
Code snippets for converting JPG to PNG using Aspose.Imaging Cloud SDK For .NET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment