The Scalable Vector Graphics (SVG) defines vector-based graphics for the Web. The SVG is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. This format is popular for rendering two-dimensional images on the internet where images can scale to any size. But, in case we have a requirement to convert SVG to PNG for lossless compression where it doesn’t lose detail and quality, then Aspose.Imaging Cloud is a viable solution.
This article provides details on how to convert SVG to PNG online.
Image Conversion API
Aspose.Imaging Cloud is our programming solution to image processing requirements. You can perform operations including resizing, cropping, rotating, scaling, flipping, searching, and exporting images to various supported formats. To further facilitate our users, we have created programming language-specific SDKs and as per the scope of this article, we are going to use Aspose.Imaging Cloud SDK for .NET. Now please visit the following link to learn more about How to install Aspose.Cloud SDKs.
Convert SVG to PNG in C#
Please follow the instructions below to convert an SVG image already available in Cloud storage to PNG format.
- The first step is to create an instance of ImagingApi while passing ClientID and ClientSecret details as arguments.
- Secondly, upload the SVG image to Cloud storage using the UploadFile(..) method of ImagingApi.
- Thirdly, create an instance of ConvertImageRequest class while passing the name of input SVG and resultant format as arguments.
- Now call the ConvertImage(..) method to perform the conversion operation. The resultant PNG is returned as a Stream instance.
- Finally, call the custom method using File.Create to save Stream instance as a file on a local drive.
For your reference, the sample images used in above example can be downloaded from trashloader2.svg and Converted.png.
SVG to PNG using cURL Commands
In this section, we are going to perform the conversion of SVG to PNG using cURL Commands. The first step is to generate a JWT access token based on client credentials. Please execute the following command to generate one:
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=4ccf1790-accc-41e9-8d18-a78dbb2ed1aa&client_secret=caac6e3d4a4724b2feb53f4e460eade3" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Now execute the following cURL command to convert SVG to PNG where the result is returned as a response stream and can be saved to a local drive.
curl -X GET "https://api.aspose.cloud/v3.0/imaging/trashloader2.svg/convert?format=png" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o Converted.png
In case you have a requirement to convert SVG image passed as zero-indexed multipart/form-data content or as raw body stream.
curl -X POST "https://api.aspose.cloud/v3.0/imaging/convert?format=png" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: multipart/form-data" \
-d {"imageData":{}} \
-o Converted.png
Conclusion
We have discussed the image conversion capabilities of Aspose.Imaging Cloud API. The specific focus has been on the conversion of SVG to PNG using C# .NET code snippet. At the same time, we have discussed the details on how to save SVG to PNG through cURL Commands. Please note that the API can also be accessed via the Swagger interface and you can test the API within the web browser. Nevertheless, the Cloud SDKs are developed under an MIT license, so a complete source code can be downloaded from GitHub.
In case you encounter any issues while using the API or you have any related queries, please feel free to contact us via the free product support forum.
Related Links
We recommend visiting the following links to learn more about: