Creating, Opening and Saving Images
Creating Image Files
Aspose.Imaging for .Java 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 output image format.
Below are some classes from the ImageOptions namespace:
- BmpOptions sets the options for creating a BMP file
- GifOptions sets the options for creating a GIF file
- JpegOptions sets the options for creating a JPEG file
- PngOptions sets the options for creating a PNG file
- TiffOptions sets the options for creating a TIFF file
- PsdOptions sets the options for creating a PSD file
Image files can be created setting an output path or by setting a stream. Both examples are described below.
Aspose.Imaging for Java 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.
Creating an Image by Setting a Path
Create an object of any desired class from the imageoptions namespace and set the various properties like dimension (width, height) of the new image. The width and height are defined in pixels. 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. In the example below, we are creating a BMP file so we need to create an instance of imageoptions.BmpOptions.
Creating through Stream
The process for creating an image using a stream is 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. Below is an example.
Opening Image Files
The Image class is Aspose.Imaging’s core class. The Image class provides methods for manipulating a single image frame, for example a JPEG image. Use the Image class to load existing image files to add effects to the image or to convert a file to another format, for example. Whatever the purpose is, Aspose.Imaging provides two standard ways to open existing files: from file or from a stream. They are discussed below with examples.
Opening an Image File 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 as shown below.
Opening an Image using a Stream
Sometimes the image that we need to open is stored as a stream of bytes.When this is the case, use the overloaded version of the load method. This accepts a Stream object as an argument to open the image.
Saving Image Files
Aspose.Imaging 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.Imaging 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. Below is an example of how this can be used.
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 ImageOptionsBase in the Image.create method, 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.
Save image file extension aware
Using Aspose.Imaging file extension maps to appropriative image options if you did not specified them.
Setting for Replacing Missing Fonts
Developers can use Aspose.Imaging for Java API to load existing image files for different purposes, than with that developers will be able to set default font name when saving PSD documents as 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 current Operating System). Once the image is modified, the file will be saved to disk.