SVG to PNG – Convert SVG to PNG in C#

Programmatic file conversion

SVG stands for Scalable Vector Graphics which defines vector-based graphics for the Web· SVG is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. It is popular for rendering two-dimensional images on the internet where images and 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 after image compression, then Aspose.Imaging Cloud is a programmatic solution.

Image Processing API

Aspose.Imaging Cloud is our programming solution to image processing requirements. Perform all operations including resizing, cropping, rotating, scaling, flipping, searching, exporting images to other supported file formats. As the API is built as per REST architecture, so it can be accessed on any platform. The APIs empowers you to incorporate image processing capabilities within Desktop, Web, Mobile, and Cloud-based applications. Now in order to further facilitate our customers, we have created programming language-specific SDKs as a wrapper around REST APIs. So you can get all the benefits/features of Cloud API within the programming language of your choice. But before proceeding further, the first step is the installation of SDK on the local system. 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 UploadFile(..) method of ImagingApi
  • Thirdly, create an instance of ConvertImageRequest class while passing name of input SVG and resultant format as arguments
  • Now call the ConvertImage(..) method to perfrom the conversion operation. The resultant PNG is returned as Stream isntance
  • Finally, call the custom method using File.Create to save Stream instance as file on local drive

For your reference, the sample images used in above example can be dowbloaded from trashloader2.svg and Converted.png.

SVG to PNG Conversion preview
Image 1:- SVG to PNG conversion preview.

SVG to PNG using cURL

Like all REST APIs, Aspose.Imaging Cloud can also be accessed via cURL commands. However, in order to ensure data integrity and privacy, you need to generate 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 files already available in Cloud storage to PNG format. 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 being offered by Aspose.Imaging Cloud. Apart from accessing API through either of the approaches mentioned above, it can also be accessed via Swagger interface where you can test the API within the web browser. Also please note that the Cloud SDKs are developed under MIT license, so a complete source code can be downloaded from GitHub.

In case you encounter any issue while using the API or you have any related query, please feel free to contact us via free product support forum.

Related Links

We recommend visiting the following links to learn more about