Convert JPG to PNG or PDF in C#

convert JPG to PNG

JPEG and PNG are among the popular raster image formats and are popular for their methods of lossy compression. You get the option to adjust the compression level to achieve the desired quality level and at the same time reduce the storage size. However, sometimes your system only accepts a particular format, so you need to load your existing set of images and save them to the desired output. Instead of using conventional applications with limited features and incurring manual provision of input files, programming APIs stay one step ahead due to their flexibility and the capabilities to perform all the operations in a batch format.

Image processing API

Aspose.Imaging for .NET is an amazing programming API offering the capabilities to create, manipulate and convert supported file formats. It functions independently of other graphics applications and does not require any image editor to be installed on the machine. It can be used with ASP.NET web applications or Windows desktop applications. Aspose.Imaging for .NET is bundled inside Conholdate.Total for .NET suite. So if you have purchased a subscription for Conholdate.Total for .NET, then you definitely can perform all the image processing operations using Aspose.Imaging for .NET API.

In order to use the API, the first step is its installation. You may follow either of the steps to perform the installation.

  • Download DLL files and manually reference them in your project
  • Open NuGet package manager, search for Aspose.Imaging and install it.
  • Run the following command from the NuGet Package Manager Console
Install-Package Aspose.Imaging

Convert JPG to PNG in C#

The API is robust enough to identify the format of the input image and you only need to specify the source image either in the form of a Stream instance or by providing the path to the file on the local system. In the following steps, we are going to explain how to load a JPEG image and save the output in PNG format.

  1. First of all, we need to create an instance of Aspose.Imaging.License object. Call the SetLicense(…) and provide the path of Conholdate.Total.NET.lic file as an argument
  2. Secondly, create an object of Image class which is a base class of all image types, and pass the result of Aspose.Imaging.Image.Load(..) method which takes the Steam or string path of an image file to be loaded
  3. Create an instance of PngOptions class
  4. Finally, call the Save(String) method of the Image class and pass the location where you want to save the resultant PNG file

The PngOptions class also provides various properties and in the example below, we have specified to generate the color type of resultant image as Grayscale. Also, the compression level for a resultant image is specified as 4. Please note that the CompressionLevel property accepts values between 0-9 where 9 is maximum compression and 0 is the default value.

The source file and resultant grayscale image can be downloaded from following links

JPG to PDF conversion in C#

Aspose.Imaging for .NET is equally capable of converting JPG images to PDF (Portable Document Format). During the conversion, you also get the capabilities to set DocumentInfo as well as PDF/A compliance details. The following steps explain the process of loading raster images and their conversion to PDF format.

  1. The first step is to create an instance of the License class.
  2. Secondly, call the SetLicense(…) method and provide the path of Conholdate.Total.NET.lic file. The license is initialized to eliminate all the restrictions which are present in the evaluation version
  3. Thirdly, create an object of Image class which is a base class of all image types, and pass the output of Aspose.Imaging.Image.Load(..) method
  4. Now create an instance of PdfOptions class
  5. In order to set PDF document information such as Author, Title, Subject, etc, create an object of PdfDocumentInfo class and pass its value to PdfDocumentInfo object of PdfOptions class
  6. Now in order to save the PDF file with PDF/A compliance information, please create an instance of PdfCoreOptions class and associate it with PdfCoreOptions property of PdfOptions object
  7. Finally, call the Save(String) method of the Image class to generate the output PDF document

The sample files used in the above example can be downloaded from the following links

Get a Free License

You may request a free temporary license to try the API without any evaluation limitations.

Conclusion

In this article, we have discussed the various capabilities of Aspose.Imaging for .NET specifically to convert JPG to PNG format as well as their rendering to PDF format. Please note that Aspose.Imaging for .NET is way more powerful and offers a plethora of options, comparing to what has been discussed above. It enables your .NET applications to draw as well as perform basic to advanced level processing of raster & vector images.

Furthermore, Aspose.Imaging for .NET offers robust image compression and high processing speed through native byte access and a range of efficient algorithms. It not only manipulate, export and convert images but also lets you dynamically draw objects using pixel manipulation and Graphics Path. For more information, please explore product documentation and in case you encounter any issue while using the API, please feel free to contact via the product support forum.

See Also