Creating, Opening and Saving Images

Creating Image Files

Aspose.PSD for .NET allows developers to create their own images. Use the static Create method exposed by the Image class to create new images. All you need to do is to supply the appropriate object of one of the classes from the ImageOptions namespace for the desired output image format. To create an image file, first create an instance of one of the classes from the ImageOptions namespace. These classes determine the output image format. Below are some classes from the ImageOptions namespace (note that only PSD file formats family are currently supported for creation):

PsdOptions sets the options for creating a PSD file. Image files can be created by setting an output path or by setting a stream.

Creating by Setting Path

Create PsdOptions from ImageOptions namespace and set the various properties. The most important property to set is the Source property. This property specifies where the image data resides (in a file or a stream). In the example below, the source is a file. After setting the properties, pass the object to one of the static Create methods along with width and height parameter. The width and height are defined in pixels.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Creates an instance of PsdOptions and set its various properties
PsdOptions psdOptions = new PsdOptions();
psdOptions.CompressionMethod = CompressionMethod.RLE;
// Define the source property for the instance of PsdOptions. Second boolean parameter determines if the file is temporal or not
psdOptions.Source = new FileCreateSource(desName, false);
// Creates an instance of Image and call Create method by passing the PsdOptions object
using (Image image = Image.Create(psdOptions, 500, 500))
{
image.Save();
}

Creating Using Stream

The process for creating an image using a stream is the same as for using a path. The only difference is that you need to create an instance of StreamSource by passing a Stream object to its constructor and assigning it to the Source property.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string desName = dataDir + "CreatingImageUsingStream_out.bmp";
// Creates an instance of BmpOptions and set its various properties
BmpOptions ImageOptions = new BmpOptions();
ImageOptions.BitsPerPixel = 24;
// Create an instance of System.IO.Stream
Stream stream = new FileStream(dataDir + "sample_out.bmp", FileMode.Create);
// Define the source property for the instance of BmpOptions Second boolean parameter determines if the Stream is disposed once get out of scope
ImageOptions.Source = new StreamSource(stream, true);
// Creates an instance of Image and call Create method by passing the BmpOptions object
using (Image image = Image.Create(ImageOptions, 500, 500))
{
// Do some image processing
image.Save(desName);
}

Opening Image Files

Developers can use Aspose.PSD for .NET API to open existing PSD image files for different purposes, like adding effects to the image or to convert an existing file to another format. Whatever the purpose is, Aspose.PSD provides two standard ways to open existing files: from file or from a stream.

Opening from Disk

Open an image file by passing the path and file name as a parameter to the static method Load exposed by the Image class.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
psdImage.Save(destName, new PngOptions());
}

Opening using a Stream

Sometimes the image that we need to open is stored as a stream. In such cases, use the overloaded version of the Load method. This accepts a Stream object as an argument to open the image.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
FileStream fStream = new FileStream(sourceFile, FileMode.Open);
fStream.Position = 0;
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(fStream))
{
PsdImage psdImage = (PsdImage)image;
MemoryStream stream = new MemoryStream();
psdImage.Save(stream, new PngOptions());
}

Load Image as Layer

This article demonstrates the usage of Aspose.PSD to load an image as a layer. Aspose.PSD APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.PSD has exposed the AddLayer method of the PsdImage class to add an image into a PSD file as a layer.

The steps to load an image into PSD as a layer are as simple as below:

  • Create an instance of an image using the PsdImage class with a specified width and height.
  • Load a PSD file as an image using the factory method Load exposed by Image class.
  • Create an instance of the Layer class and assign the PSD image layer to it.
  • Add the created layer using the AddLayer method expose by PsdImage class
  • Save the results.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string filePath = dataDir + "PsdExample.psd";
string outputFilePath = dataDir + "PsdResult.psd";
using (var image = new PsdImage(200, 200))
{
using (var im = Image.Load(filePath))
{
Layer layer = null;
try
{
layer = new Layer((RasterImage)im);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw;
}
}
image.Save(outputFilePath);
}

Load Image as Layer from a Stream

This article demonstrates the usage of Aspose.PSD to load an image as a layer from a stream. To load an image from a stream, simply pass a stream object that contains an image to the Layer class constructor. Add the created layer using the AddLayer method exposed by the PsdImage class and save the results.

Here is the sample code.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string outputFilePath = dataDir + "PsdResult.psd";
var filesList = new string[]
{
"PsdExample.psd",
"BmpExample.bmp",
"GifExample.gif",
"Jpeg2000Example.jpf",
"JpegExample.jpg",
"PngExample.png",
"TiffExample.tif",
};
using (var image = new PsdImage(200, 200))
{
foreach (var fileName in filesList)
{
string filePath = dataDir + fileName;
using (var stream = new FileStream(filePath, FileMode.Open))
{
Layer layer = null;
try
{
layer = new Layer(stream);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw e;
}
}
}
image.Save(outputFilePath);
}

Saving Image Files

Aspose.PSD lets you create image files from scratch. It also provides the means to edit existing image files. Once the image is created or modified, the file is usually saved to disk. Aspose.PSD provides you with methods for saving images to a disk by specifying a path or using a Stream object.

Saving to Disk

The Image class represents an image object, so this class provides all the tools needed to create, load and save an image file. Use the Image class Save method to save images. One overloaded version of the Save method accepts the file location as a string.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
psdImage.Save(destName, new PngOptions());
}

Saving to a Stream

Another overloaded version of the Save method accepts the Stream object as an argument and saves the image file to the stream.

If the image is created by specifying any of the CreateOptions in the Image constructor, the image is automatically saved to the path or stream supplied during the initialization of the Image class by calling the Save method that doesn’t accept any parameter.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
MemoryStream stream = new MemoryStream();
psdImage.Save(stream, new PngOptions());
}

Setting for Replacing Missing Fonts

Developers can use Aspose.PSD for .NET API to load existing PSD image files for different purposes, for example, to set default font name when saving PSD documents as a raster image (into PNG, JPG and BMP formats). This default font should be used as a replacement for all missing fonts (fonts that are not found in the current Operating System). Once the image is modified, the file will be saved to disk.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFileName = "sample_konstanting.psd";
string[] outputs = new string[]
{
"replacedfont0.tiff",
"replacedfont1.png",
"replacedfont2.jpg"
};
using (PsdImage image = (PsdImage)Image.Load(sourceFileName, new PsdLoadOptions()))
{
// This way you can use different fonts for different outputs
image.Save(outputs[0], new TiffOptions(TiffExpectedFormat.TiffJpegRgb) { DefaultReplacementFont = "Arial" });
image.Save(outputs[1], new PngOptions { DefaultReplacementFont = "Verdana" });
image.Save(outputs[2], new JpegOptions { DefaultReplacementFont = "Times New Roman" });
}