PNG and JPG formats are well-known raster image file formats that contain a single layer of visual information. Photoshop Document (PSD) files contain several layers to display images. You can programmatically convert PNG or JPG images to PSD format easily using C# in.net applications. This article covers the following sections related to PNG and JPG image conversion:

  • Convert PNG images to PSD files programmatically using C#
  • Convert JPG images to PSD programmatically using C#

PNG or JPG to PSD conversions are useful in situations where different designers design layers and then combine them into a single image using the PSD format. For example, playgrounds, multi-level maps, and graphic assets contain a lot of visual information. You need to configure the Aspose.PSD for.NET API to use several supported file formats. If you haven’t used Aspose.PSD, you can download the test.

Convert PNG images to PSD files programmatically using C#

You can convert a PNG image to a PSD file by following these steps:

  • Initialize the PsdImage class object.
  • Load the input PNG image into the FileStream object.
  • Use the AddLayer () method to add the input image to the layer of the PSD image.
  • Use the Save () method to convert the image to an output PSD file.

The following code shows how to programmatically convert a PNG image to a PSD file using C# :

string fileName = "Sample.png"; // Initialize PsdImage class object PsdImage image = new PsdImage(900, 700); // Load input image into FileStream object Stream stream = new FileStream(fileName, FileMode.Open); Layer layer = null; try { layer = new Layer(stream); // Add input image as layer to PSD image image.AddLayer(layer); } catch (Exception e) { if (layer ! = null) { layer.Dispose(); } throw e; } // Convert PNG image to output PSD file image.Save("PNGtoPSD.psd");Copy the code

The following screen capture shows sample output for PNG image-to-PSD conversion:

Convert JPG images to PSD programmatically using C#

You can convert JPG images into PSD files by following these steps:

  • Initialize the PsdImage class instance.
  • Load the input JPG image into the FileStream object.
  • Use the AddLayer () method to add the input JPG file layer to the PSD file.
  • Use the psDimage.save () method to convert JPG images into output PSD files.

The following code shows how to programmatically convert JPG images into PSD files using C# :

string fileName = "Sample.jpg"; // Initialize PsdImage class object PsdImage image = new PsdImage(900, 700); // Load input image into FileStream object Stream stream = new FileStream(fileName, FileMode.Open); Layer layer = null; try { layer = new Layer(stream); // Add input image as layer to PSD image image.AddLayer(layer); } catch (Exception e) { if (layer ! = null) { layer.Dispose(); } throw e; } // Convert JPG image to output PSD file image.Save("JPGtoPSD.psd");Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.