Creating Image files

Skip to end of metadata
Go to start of metadata
This page is no longer maintained. For an updated page, please visit http://www.aspose.com/docs/display/imagingjava/Drawing+and+Formatting+Images
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 [com.aspose.imaging.imageoptions] package. These classes determine output image format.

Below are some classes from the Aspose.Imaging.imageoptions namespace:

  • BmpOptions sets the options for creating a BMP file.
  • GifOptions sets the options for creating a GIF file.
  • ImageOptions sets the options for creating an image.
  • JpegOptions sets the options for creating a JPEG file.
  • PngOptions sets the options for creating a PNG file.
  • PsdOptions sets the options for creating a PSD file.
  • TiffOptions sets the options for creating a TIFF file.

Image files can be created setting an output path or by setting a stream. Both examples are described below.

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.

Programming Sample

Java
//Creates an instance of BmpOptions and set its various properties 
com.aspose.imaging.imageoptions.BmpOptions createOptions = new com.aspose.imaging.imageoptions.BmpOptions();

createOptions.setBitsPerPixel(24);

//Create an instance of FileCreateSource and assign it as Source for the instance of BmpOptions
//If second parameter is not passed, then by default the file has IsTemporal set to True
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("C:\\output.bmp"));

//Creates an instance of Image
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions,500,500);

//do some image processing
 

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.

Programming Sample

Java
//Create an instance of BmpCreateOptions and set its various properties
com.aspose.imaging.imageoptions.BmpOptions createOptions = new com.aspose.imaging.imageoptions.BmpOptions();

createOptions.setBitsPerPixel(24);

//Create an instance of FileCreateSource and assign it as Source for the instance of BmpOptions
//Second Boolean parameter determines if the file to be created IsTemporal or not
createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("C:\\output.bmp",false));

//Create an instance of Image and initialize it with instance of BmpOptions by calling Create method
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions,500,500);

//do some image processing
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.