public abstract class Image extends DataStreamSupporter implements IObjectWithBounds, com.aspose.imaging_internal.progressmanagement.IProgressInformer, com.aspose.imaging_internal.progressmanagement.IProgressEventHandler
The image is the base class for all type of images.
DataStreamSupporter
,
IObjectWithBounds
This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance. Several properties for BmpOptions instance are set before creating the actual image. Especially the Source property, that refers to the actual disk location in this case.
// Create an instance of BmpOptions and set its various properties com.aspose.imaging.imageoptions.BmpOptions bmpOptions = new com.aspose.imaging.imageoptions.BmpOptions(); bmpOptions.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 bmpOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("C:\\temp\\sample.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(bmpOptions, 500, 500); try { // Do some image processing // Save all changes image.save(); } finally { image.dispose(); }
Modifier and Type | Method and Description |
---|---|
static boolean |
canLoad(InputStream stream)
Determines whether image can be loaded from the specified stream.
|
static boolean |
canLoad(InputStream stream,
LoadOptions loadOptions)
Determines whether image can be loaded from the specified stream and optionally using the specified
loadOptions . |
static boolean |
canLoad(String filePath)
Determines whether image can be loaded from the specified file path.
|
static boolean |
canLoad(String filePath,
LoadOptions loadOptions)
Determines whether image can be loaded from the specified file path and optionally using the specified open options.
|
boolean |
canSave(ImageOptionsBase options)
Determines whether image can be saved to the specified file format represented by the passed save options.
|
static Image |
create(ImageOptionsBase imageOptions,
int width,
int height)
Creates a new image using the specified create options.
|
Color |
getBackgroundColor()
Gets or sets a value for the background color.
|
abstract int |
getBitsPerPixel()
Gets the image bits per pixel count.
|
Rectangle |
getBounds()
Gets the image bounds.
|
int |
getBufferSizeHint()
Gets the buffer size hint which is defined max allowed size for all internal buffers.
|
Image |
getContainer()
Gets the
Image container. |
ImageOptionsBase |
getDefaultOptions(Object[] args)
Gets the default options.
|
long |
getFileFormat()
Gets a value of file format
|
static long |
getFileFormat(InputStream stream)
Gets the file format.
|
static long |
getFileFormat(String filePath)
Gets the file format.
|
static Rectangle |
getFittingRectangle(Rectangle rectangle,
int[] pixels,
int width,
int height)
Gets rectangle which fits the current image.
|
static Rectangle |
getFittingRectangle(Rectangle rectangle,
int width,
int height)
Gets rectangle which fits the current image.
|
abstract int |
getHeight()
Gets the image height.
|
InterruptMonitor |
getInterruptMonitor()
Gets the interrupt monitor.
|
ImageOptionsBase |
getOriginalOptions()
Gets the options based on the original file settings.
|
IColorPalette |
getPalette()
Gets the color palette.
|
ProgressEventHandler |
getProgressEventHandler()
Gets the progress event handler information.
|
ProgressEventHandlerInfo |
getProgressEventHandlerInfo()
Gets the progress event handler information.
|
static int |
getProportionalHeight(int width,
int height,
int newWidth)
Gets a proportional height.
|
static int |
getProportionalWidth(int width,
int height,
int newHeight)
Gets a proportional width.
|
Size |
getSize()
Gets the image size.
|
abstract int |
getWidth()
Gets the image width.
|
boolean |
hasBackgroundColor()
Gets a value indicating whether image has background color.
|
boolean |
isAutoAdjustPalette()
Gets a value indicating whether automatic adjust palette.
|
static Image |
load(InputStream stream)
Loads a new image from the specified stream.
|
static Image |
load(InputStream stream,
LoadOptions loadOptions)
Loads a new image from the specified stream.
|
static Image |
load(RandomAccessFile file)
Loads a new image from the specified stream.
|
static Image |
load(RandomAccessFile file,
LoadOptions loadOptions)
Loads a new image from the specified stream.
|
static Image |
load(String filePath)
Loads a new image from the specified file.
|
static Image |
load(String filePath,
LoadOptions loadOptions)
Loads a new image from the specified file.
|
void |
resize(int newWidth,
int newHeight)
Resizes the image.
|
abstract void |
resize(int newWidth,
int newHeight,
ImageResizeSettings settings)
Resizes the image.
|
abstract void |
resize(int newWidth,
int newHeight,
int resizeType)
Resizes the image.
|
void |
resizeHeightProportionally(int newHeight)
Resizes the height proportionally.
|
void |
resizeHeightProportionally(int newHeight,
ImageResizeSettings settings)
Resizes the height proportionally.
|
void |
resizeHeightProportionally(int newHeight,
int resizeType)
Resizes the height proportionally.
|
void |
resizeWidthProportionally(int newWidth)
Resizes the width proportionally.
|
void |
resizeWidthProportionally(int newWidth,
ImageResizeSettings settings)
Resizes the width proportionally.
|
void |
resizeWidthProportionally(int newWidth,
int resizeType)
Resizes the width proportionally.
|
abstract void |
rotateFlip(int rotateFlipType)
Rotates, flips, or rotates and flips the image.
|
void |
save()
Saves the image data to the underlying stream.
|
void |
save(OutputStream stream,
ImageOptionsBase optionsBase)
Saves the image's data to the specified stream in the specified file format according to save options.
|
void |
save(OutputStream stream,
ImageOptionsBase optionsBase,
Rectangle boundsRectangle)
Saves the image's data to the specified stream in the specified file format according to save options.
|
void |
save(RandomAccessFile file,
ImageOptionsBase options)
Saves the object's data to the specified file location in the specified file format according to save options.
|
void |
save(RandomAccessFile file,
ImageOptionsBase optionsBase,
Rectangle boundsRectangle)
Saves the image's data to the specified stream in the specified file format according to save options.
|
void |
save(String filePath,
ImageOptionsBase options)
Saves the object's data to the specified file location in the specified file format according to save options.
|
void |
save(String filePath,
ImageOptionsBase options,
Rectangle boundsRectangle)
Saves the object's data to the specified file location in the specified file format according to save options.
|
void |
setAutoAdjustPalette(boolean value)
Sets a value indicating whether automatic adjust palette.
|
void |
setBackgroundColor(boolean value)
Gets or sets a value indicating whether image has background color.
|
void |
setBackgroundColor(Color value)
Gets or sets a value for the background color.
|
void |
setBufferSizeHint(int value)
Sets the buffer size hint which is defined max allowed size for all internal buffers.
|
void |
setInterruptMonitor(InterruptMonitor value)
Sets the interrupt monitor.
|
void |
setPalette(IColorPalette value)
Sets the color palette.
|
abstract void |
setPalette(IColorPalette palette,
boolean updateColors)
Sets the image palette.
|
cacheData, getDataStreamContainer, isCached, save, save, save, save
close, dispose, getDisposed
public static boolean canLoad(String filePath)
Determines whether image can be loaded from the specified file path.
filePath
- The file path.true
if image can be loaded from the specified file; otherwise, false
.This example determines whether image can be loaded from a file.
// Use an absolute path to the file boolean canLoad = com.aspose.imaging.Image.canLoad("c:\\temp\\sample.gif");
public static boolean canLoad(String filePath, LoadOptions loadOptions)
Determines whether image can be loaded from the specified file path and optionally using the specified open options.
filePath
- The file path.loadOptions
- The load options.true
if image can be loaded from the specified file; otherwise, false
.public static boolean canLoad(InputStream stream)
Determines whether image can be loaded from the specified stream.
stream
- The stream to load from.true
if image can be loaded from the specified stream; otherwise, false
.This example determines whether image can be loaded from a file stream.
String dir = "c:\\temp\\"; boolean canLoad; // Use a file stream java.io.InputStream stream = new java.io.FileInputStream(dir + "sample.bmp"); try { canLoad = com.aspose.imaging.Image.canLoad(stream); } finally { stream.close(); } // The following data is not a valid image stream, so CanLoad returns false. byte[] imageData = new byte[]{0, 0, 0, 0, 0, 0, 0, 0}; stream = new java.io.ByteArrayInputStream(imageData); { canLoad = com.aspose.imaging.Image.canLoad(stream); }
public static boolean canLoad(InputStream stream, LoadOptions loadOptions)
Determines whether image can be loaded from the specified stream and optionally using the specified loadOptions
.
stream
- The stream to load from.loadOptions
- The load options.true
if image can be loaded from the specified stream; otherwise, false
.public static Image create(ImageOptionsBase imageOptions, int width, int height)
Creates a new image using the specified create options.
imageOptions
- The image options.width
- The width.height
- The height.This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance. Several properties for BmpOptions instance are set before creating the actual image. Especially the Source property, that refers to the actual disk location in this case.
// Create an instance of BmpOptions and set its various properties com.aspose.imaging.imageoptions.BmpOptions bmpOptions = new com.aspose.imaging.imageoptions.BmpOptions(); bmpOptions.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 bmpOptions.setSource(new com.aspose.imaging.sources.FileCreateSource("C:\\temp\\sample.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(bmpOptions, 500, 500); try { // Do some image processing // Save all changes image.save(); } finally { image.dispose(); }
public static long getFileFormat(String filePath)
Gets the file format.
filePath
- The file path.
The file format determined does not mean that the specified image may be loaded. Use one of the CanLoad method overloads to determine whether file may be loaded.
This example shows how to determine the image format without loading the entire image from a file.
String dir = "c:\\temp\\"; // Use an absolute path to the file long format = com.aspose.imaging.Image.getFileFormat(dir + "sample.gif"); // A string represenation of the file format. String strFormat; if (format == com.aspose.imaging.FileFormat.Bmp) { strFormat = "BMP"; } else if (format == com.aspose.imaging.FileFormat.Gif) { strFormat = "GIF"; } else if (format == com.aspose.imaging.FileFormat.Dicom) { strFormat = "DICOM"; } else if (format == com.aspose.imaging.FileFormat.Djvu) { strFormat = "DJVU"; } else if (format == com.aspose.imaging.FileFormat.Dng) { strFormat = "DNG"; } else if (format == com.aspose.imaging.FileFormat.Png) { strFormat = "PNG"; } else if (format == com.aspose.imaging.FileFormat.Jpeg) { strFormat = "JPEG"; } else if (format == com.aspose.imaging.FileFormat.Jpeg2000) { strFormat = "JPEG2000"; } else if (format == com.aspose.imaging.FileFormat.Psd) { strFormat = "PSD"; } else if (format == com.aspose.imaging.FileFormat.Tiff) { strFormat = "Tiff"; } else if (format == com.aspose.imaging.FileFormat.Webp) { strFormat = "WEBP"; } else if (format == com.aspose.imaging.FileFormat.Cdr) { strFormat = "CDR"; } else if (format == com.aspose.imaging.FileFormat.Cmx) { strFormat = "CMX"; } else if (format == com.aspose.imaging.FileFormat.Emf) { strFormat = "EMF"; } else if (format == com.aspose.imaging.FileFormat.Wmf) { strFormat = "WMF"; } else if (format == com.aspose.imaging.FileFormat.Svg) { strFormat = "SVG"; } else if (format == com.aspose.imaging.FileFormat.Odg) { strFormat = "ODG"; } else if (format == com.aspose.imaging.FileFormat.Eps) { strFormat = "EPS"; } else { strFormat = "UNDEFINED"; } System.out.println("The file format is " + strFormat);
public static long getFileFormat(InputStream stream)
Gets the file format.
stream
- The stream.
The file format determined does not mean that the specified image may be loaded. Use one of the CanLoad method overloads to determine whether stream may be loaded.
This example shows how to determine the image format without loading the entire image from a file stream.
// The helper class used in the main example below. class Utils { // The helper method to get a string representation of the file format. public String getFileFormatString(long fileFormat) { if (fileFormat == com.aspose.imaging.FileFormat.Bmp) { return "BMP"; } else if (fileFormat == com.aspose.imaging.FileFormat.Gif) { return "GIF"; } else if (fileFormat == com.aspose.imaging.FileFormat.Dicom) { return "DICOM"; } else if (fileFormat == com.aspose.imaging.FileFormat.Djvu) { return "DJVU"; } else if (fileFormat == com.aspose.imaging.FileFormat.Dng) { return "DNG"; } else if (fileFormat == com.aspose.imaging.FileFormat.Png) { return "PNG"; } else if (fileFormat == com.aspose.imaging.FileFormat.Jpeg) { return "JPEG"; } else if (fileFormat == com.aspose.imaging.FileFormat.Jpeg2000) { return "JPEG2000"; } else if (fileFormat == com.aspose.imaging.FileFormat.Psd) { return "PSD"; } else if (fileFormat == com.aspose.imaging.FileFormat.Tiff) { return "Tiff"; } else if (fileFormat == com.aspose.imaging.FileFormat.Webp) { return "WEBP"; } else if (fileFormat == com.aspose.imaging.FileFormat.Cdr) { return "CDR"; } else if (fileFormat == com.aspose.imaging.FileFormat.Cmx) { return "CMX"; } else if (fileFormat == com.aspose.imaging.FileFormat.Emf) { return "EMF"; } else if (fileFormat == com.aspose.imaging.FileFormat.Wmf) { return "WMF"; } else if (fileFormat == com.aspose.imaging.FileFormat.Svg) { return "SVG"; } else if (fileFormat == com.aspose.imaging.FileFormat.Odg) { return "ODG"; } else if (fileFormat == com.aspose.imaging.FileFormat.Eps) { return "EPS"; } else { return "UNDEFINED"; } } } // Here is the main example Utils utils = new Utils(); String dir = "c:\\temp\\"; // Use a file stream java.io.InputStream stream = new java.io.FileInputStream(dir + "sample.bmp"); { long format = com.aspose.imaging.Image.getFileFormat(stream); System.out.println("The file format is " + utils.getFileFormatString(format)); } // The following data is not a valid image stream, so GetFileFormat returns FileFormat.Undefined. byte[] imageData = new byte[]{0, 0, 0, 0, 0, 0, 0, 0}; stream = new java.io.ByteArrayInputStream(imageData); { long format = com.aspose.imaging.Image.getFileFormat(stream); System.out.println("The file format is " + utils.getFileFormatString(format)); } // The output may look like this: // The file format is BMP // The file format is UNDEFINED
public static Rectangle getFittingRectangle(Rectangle rectangle, int width, int height)
Gets rectangle which fits the current image.
rectangle
- The rectangle to get fitting rectangle for.width
- The object width.height
- The object height.public static Rectangle getFittingRectangle(Rectangle rectangle, int[] pixels, int width, int height)
Gets rectangle which fits the current image.
rectangle
- The rectangle to get fitting rectangle for.pixels
- The 32-bit ARGB pixels.width
- The object width.height
- The object height.public static Image load(String filePath, LoadOptions loadOptions)
Loads a new image from the specified file.
filePath
- The file path to load image from.loadOptions
- The load options.public static Image load(String filePath)
Loads a new image from the specified file.
filePath
- The file path to load image from.This example demonstrates the loading of an existing Image file into an instance of com.aspose.imaging.Image using file path specified
// Create Image instance and initialize it with an existing image file from disk location com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp"); try { // Do some image processing. } finally { image.dispose(); }
public static Image load(RandomAccessFile file, LoadOptions loadOptions)
Loads a new image from the specified stream.
file
- The file to load image from.loadOptions
- The load options.public static Image load(RandomAccessFile file)
Loads a new image from the specified stream.
file
- The file to load image from.public static Image load(InputStream stream, LoadOptions loadOptions)
Loads a new image from the specified stream.
stream
- The stream to load image from.loadOptions
- The load options.public static Image load(InputStream stream)
Loads a new image from the specified stream.
stream
- The stream to load image from.This example demonstrates the use of InputStream object to load an existing Image file
// Create an instance of FileInputStream java.io.InputStream stream = new java.io.FileInputStream("C:\\temp\\sample.bmp"); try { // Create an instance of Image class and load an existing file through FileStream object by calling Load method com.aspose.imaging.Image image = com.aspose.imaging.Image.load(stream); try { // Do some image processing. } finally { image.dispose(); } } finally { stream.close(); }
public static int getProportionalWidth(int width, int height, int newHeight)
Gets a proportional width.
width
- The width.height
- The height.newHeight
- The new height.public static int getProportionalHeight(int width, int height, int newWidth)
Gets a proportional height.
width
- The width.height
- The height.newWidth
- The new width.public abstract int getBitsPerPixel()
Gets the image bits per pixel count.
public Rectangle getBounds()
Gets the image bounds.
getBounds
in interface IObjectWithBounds
public Image getContainer()
Gets the Image
container.
Image
container.
If this property is not null it indicates the image is contained whithin another image.
public abstract int getHeight()
Gets the image height.
getHeight
in interface IObjectWithBounds
public IColorPalette getPalette()
Gets the color palette.
public void setPalette(IColorPalette value)
Sets the color palette.
value
- The color palette.public Size getSize()
Gets the image size.
getSize
in interface IObjectWithBounds
This example shows how to load a DJVU image from a file stream and print information about the pages.
String dir = "c:\\temp\\"; // Load a DJVU image from a file stream. java.io.FileInputStream stream = new java.io.FileInputStream(dir + "sample.djvu"); try { com.aspose.imaging.fileformats.djvu.DjvuImage djvuImage = new com.aspose.imaging.fileformats.djvu.DjvuImage(stream); try { System.out.println("The total number of pages: " + djvuImage.getPages().length); System.out.println("The active page number: " + djvuImage.getActivePage().getPageNumber()); System.out.println("The first page number: " + djvuImage.getFirstPage().getPageNumber()); System.out.println("The last page number: " + djvuImage.getLastPage().getPageNumber()); for (com.aspose.imaging.fileformats.djvu.DjvuPage djvuPage : djvuImage.getPages()) { System.out.println("--------------------------------------------------"); System.out.println("Page number: " + djvuPage.getPageNumber()); System.out.println("Page size: " + djvuPage.getSize()); System.out.println("Page raw format: " + djvuPage.getRawDataFormat()); } } finally { djvuImage.dispose(); } } finally { stream.close(); } //The output may look like this: //The total number of pages: 2 //The active page number: 1 //The first page number: 1 //The last page number: 2 //-------------------------------------------------- //Page number: 1 //Page size: { Width = 2481, Height = 3508} //Page raw format: RgbIndexed1Bpp, used channels: 1 //-------------------------------------------------- //Page number: 2 //Page size: { Width = 2481, Height = 3508} //Page raw format: RgbIndexed1Bpp, used channels: 1
public abstract int getWidth()
Gets the image width.
getWidth
in interface IObjectWithBounds
public InterruptMonitor getInterruptMonitor()
Gets the interrupt monitor.
public void setInterruptMonitor(InterruptMonitor value)
Sets the interrupt monitor.
value
- the interrupt monitor.public final int getBufferSizeHint()
Gets the buffer size hint which is defined max allowed size for all internal buffers.
Value: The buffer size hint, in megabytes. Non-positive value means no memory limitation for internal bufferspublic final void setBufferSizeHint(int value)
Sets the buffer size hint which is defined max allowed size for all internal buffers.
Value: The buffer size hint, in megabytes. Non-positive value means no memory limitation for internal buffersvalue
- the buffer size hint which is defined max allowed size for all internal buffers.public boolean isAutoAdjustPalette()
Gets a value indicating whether automatic adjust palette.
true
if enable automatic adjust palette; otherwise, false
.public void setAutoAdjustPalette(boolean value)
Sets a value indicating whether automatic adjust palette.
value
- true
if enable automatic adjust palette; otherwise, false
.public boolean hasBackgroundColor()
Gets a value indicating whether image has background color.
public long getFileFormat()
Gets a value of file format
public Color getBackgroundColor()
Gets or sets a value for the background color.
public void setBackgroundColor(boolean value)
Gets or sets a value indicating whether image has background color.
public void setBackgroundColor(Color value)
Gets or sets a value for the background color.
public final ProgressEventHandler getProgressEventHandler()
Gets the progress event handler information.
getProgressEventHandler
in interface com.aspose.imaging_internal.progressmanagement.IProgressEventHandler
public final ProgressEventHandlerInfo getProgressEventHandlerInfo()
Gets the progress event handler information.
Value: The progress event handler information.getProgressEventHandlerInfo
in interface com.aspose.imaging_internal.progressmanagement.IProgressInformer
public boolean canSave(ImageOptionsBase options)
Determines whether image can be saved to the specified file format represented by the passed save options.
options
- The save options to use.true
if image can be saved to the specified file format represented by the passed save options; otherwise, false
.This example shows how to determine whether image can be saved to the specified file format represented by the passed save options.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { com.aspose.imaging.imageoptions.JpegOptions saveOptions = new com.aspose.imaging.imageoptions.JpegOptions(); saveOptions.setQuality(50); // Determine whether the image can be saved to Jpeg boolean canSave = image.canSave(saveOptions); } finally { image.dispose(); }
public void resize(int newWidth, int newHeight)
Resizes the image. The default ResizeType.LeftTopToLeftTop
is used.
newWidth
- The new width.newHeight
- The new height.The following example shows how to resize a metafile (WMF and EMF).
String baseFolder = "c:\\temp\\"; String[] files = new String[]{"image3.emf", "image4.wmf"}; for (String fileName : files) { String inputFile = baseFolder + fileName; String outputFile = baseFolder + "Resize_" + fileName; com.aspose.imaging.fileformats.emf.MetaImage image = (com.aspose.imaging.fileformats.emf.MetaImage) com.aspose.imaging.Image.load(inputFile); try { image.resize(image.getWidth() / 4, image.getHeight() / 4); image.save(outputFile); } finally { image.close(); } }
public abstract void resize(int newWidth, int newHeight, int resizeType)
Resizes the image.
newWidth
- The new width.newHeight
- The new height.resizeType
- The resize type.This example loads an image and resizes it using various resizing methods.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Nearest Neighbour resampling. image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "upsample.nearestneighbour.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Nearest Neighbour resampling. image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "downsample.nearestneighbour.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Bilinear resampling. image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "upsample.bilinear.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Bilinear resampling. image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "downsample.bilinear.gif"); } finally { image.dispose(); }
public abstract void resize(int newWidth, int newHeight, ImageResizeSettings settings)
Resizes the image.
newWidth
- The new width.newHeight
- The new height.settings
- The resize settings.This example loads an image and resizes it using various resizing settings.
String dir = "c:\\temp\\"; com.aspose.imaging.ImageResizeSettings resizeSettings = new com.aspose.imaging.ImageResizeSettings(); // The adaptive algorithm based on weighted and blended rational function and lanczos3 interpolation. resizeSettings.setMode(com.aspose.imaging.ResizeType.AdaptiveResample); // The small rectangular filter resizeSettings.setFilterType(com.aspose.imaging.ImageFilterType.SmallRectangular); // The number of colors in the palette. resizeSettings.setEntriesCount(256); // The color quantization is not used resizeSettings.setColorQuantizationMethod(com.aspose.imaging.ColorQuantizationMethod.None); // The euclidian method resizeSettings.setColorCompareMethod(com.aspose.imaging.ColorCompareMethod.Euclidian); com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using adaptive resampling. image.resize(image.getWidth() / 2, image.getHeight() / 2, resizeSettings); image.save(dir + "downsample.adaptive.gif"); } finally { image.dispose(); }
public ImageOptionsBase getDefaultOptions(Object[] args)
Gets the default options.
args
- The arguments.public ImageOptionsBase getOriginalOptions()
Gets the options based on the original file settings.
This can be helpful to keep bit-depth and other parameters of the original image unchanged.
For example, if we load a black-white PNG image with 1 bit per pixel and then save it using the
DataStreamSupporter.Save(string)
method, the output PNG image with 8-bit per pixel will be produced.
To avoid it and save PNG image with 1-bit per pixel, use this method to get corresponding saving options and pass them
to the Image.Save(string, ImageOptionsBase)
method as the second parameter.
public void resizeWidthProportionally(int newWidth)
Resizes the width proportionally.
newWidth
- The new width.public void resizeHeightProportionally(int newHeight)
Resizes the height proportionally.
newHeight
- The new height.public void resizeWidthProportionally(int newWidth, int resizeType)
Resizes the width proportionally.
newWidth
- The new width.resizeType
- Type of the resize.This example loads an image and resizes it proportionally using various resizing methods. Only the width is specified, the height is calculated automatically.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Nearest Neighbour resampling. image.resizeWidthProportionally(image.getWidth() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "upsample.nearestneighbour.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Nearest Neighbour resampling. image.resizeWidthProportionally(image.getWidth() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "downsample.nearestneighbour.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Bilinear resampling. image.resizeWidthProportionally(image.getWidth() * 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "upsample.bilinear.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); { // Scale down by 2 times using Bilinear resampling. image.resizeWidthProportionally(image.getWidth() / 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "downsample.bilinear.gif"); }
public void resizeHeightProportionally(int newHeight, int resizeType)
Resizes the height proportionally.
newHeight
- The new height.resizeType
- Type of the resize.This example loads an image and resizes it proportionally using various resizing methods. Only the height is specified, the width is calculated automatically.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Nearest Neighbour resampling. image.resizeHeightProportionally(image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "upsample.nearestneighbour.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Nearest Neighbour resampling. image.resizeHeightProportionally(image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "upsample.nearestneighbour.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Bilinear resampling. image.resizeHeightProportionally(image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "upsample.bilinear.gif"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Bilinear resampling. image.resizeHeightProportionally(image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "downsample.bilinear.gif"); } finally { image.dispose(); }
public void resizeWidthProportionally(int newWidth, ImageResizeSettings settings)
Resizes the width proportionally.
newWidth
- The new width.settings
- The image resize settings.public void resizeHeightProportionally(int newHeight, ImageResizeSettings settings)
Resizes the height proportionally.
newHeight
- The new height.settings
- The image resize settings.public abstract void rotateFlip(int rotateFlipType)
Rotates, flips, or rotates and flips the image.
rotateFlipType
- Type of the rotate flip.This example demonstrates the use of Rotate operation on an image. Example loads an existing image file from some disk location and performs the Rotate operation on the image according to the value of Enum com.aspose.imaging.RotateFlipType
// Create an instance of image class and initialize it with an existing image file through File path com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp"); try { // Rotate the image at 180 degree about X axis image.rotateFlip(com.aspose.imaging.RotateFlipType.Rotate180FlipX); // Save all changes. image.save(); } finally { image.dispose(); }
public final void save()
Saves the image data to the underlying stream.
save
in class DataStreamSupporter
The following example shows how to save an entire BMP image or part of it to a file or stream.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.bmp"); try { com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = (com.aspose.imaging.fileformats.bmp.BmpImage) image; // Convert to a black-white image bmpImage.binarizeOtsu(); // Save to the same location with default options. image.save(); com.aspose.imaging.imageoptions.BmpOptions saveOptions = new com.aspose.imaging.imageoptions.BmpOptions(); // A palette contains only two colors: Black and White in this case. saveOptions.setPalette(com.aspose.imaging.ColorPaletteHelper.createMonochrome()); // For all monochrome images (including black-white ones) it is enough to allocate 1 bit per pixel. saveOptions.setBitsPerPixel(1); // Save to another location with the specified options. image.save(dir + "sample.bw.palettized.bmp", saveOptions); // Save only the central part of the image. com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(image.getWidth() / 4, image.getHeight() / 4, image.getWidth() / 2, image.getHeight() / 2); image.save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds); // Save the entire image to a memory stream java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream(); try { image.save(stream, saveOptions); System.out.println("The size of the whole image in bytes: " + stream.size()); } finally { stream.close(); } // Save the central part of the image to a memory stream stream = new java.io.ByteArrayOutputStream(); try { image.save(stream, saveOptions, bounds); System.out.println("The size of the central part of the image in bytes: " + stream.size()); } finally { stream.close(); } } finally { image.close(); } //The output may look like this: //The size of the whole image in bytes: 1662 //The size of the central part of the image in bytes: 462
public void save(String filePath, ImageOptionsBase options)
Saves the object's data to the specified file location in the specified file format according to save options.
filePath
- The file path.options
- The options.This example shows the simple steps to Save an Image. To demonstrate this operation, we load an existing file from some disk location and save the image in PSD format
// Load an existing file from disk. com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp"); try { // Save the Image as PSD to File Path with default PsdOptions settings image.save("C:\\temp\\output.psd", new com.aspose.imaging.imageoptions.PsdOptions()); } finally { image.dispose(); }
public void save(String filePath, ImageOptionsBase options, Rectangle boundsRectangle)
Saves the object's data to the specified file location in the specified file format according to save options.
filePath
- The file path.options
- The options.boundsRectangle
- The destination image bounds rectangle. Set the empty rectangle for use sourse bounds.com.aspose.ms.System.ArgumentNullException
- optionsImageSaveException
- Image saving failed.The following example loads a BMP image from a file, then saves a rectangular part of the image to a PNG file.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.bmp"); try { // Save the upper half of the image to a PNG file. com.aspose.imaging.imageoptions.PngOptions saveOptions = new com.aspose.imaging.imageoptions.PngOptions(); com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(0, 0, image.getWidth(), image.getHeight() / 2); image.save(dir + "output.png", saveOptions, bounds); } finally { image.dispose(); }
public void save(RandomAccessFile file, ImageOptionsBase options)
Saves the object's data to the specified file location in the specified file format according to save options.
file
- The file to save the image's data to.options
- The options.public void save(RandomAccessFile file, ImageOptionsBase optionsBase, Rectangle boundsRectangle)
Saves the image's data to the specified stream in the specified file format according to save options.
file
- The file to save the image's data to.optionsBase
- The save options.boundsRectangle
- The destination image bounds rectangle. Set the empty rectangle for use sourse bounds.com.aspose.ms.System.ArgumentNullException
- optionsBasecom.aspose.ms.System.ArgumentException
- Cannot save to the specified format as it is not supported at the moment.;optionsBaseImageSaveException
- Image export failed.public void save(OutputStream stream, ImageOptionsBase optionsBase)
Saves the image's data to the specified stream in the specified file format according to save options.
stream
- The stream to save the image's data to.optionsBase
- The save options.com.aspose.ms.System.ArgumentNullException
- optionsBasecom.aspose.ms.System.ArgumentException
- Cannot save to the specified format as it is not supported at the moment.;optionsBaseImageSaveException
- Image export failed.This example shows the process of saving an Image to memory buffer. To demonstrate this operation, example loads an existing file from some disk location and save the image in PSD format.
java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream(); try { //Create an instance of image class and initialize it with an existing image file through File path com.aspose.imaging.Image image = com.aspose.imaging.Image.load("C:\\temp\\sample.bmp"); try { //Save the image to PSD memory stream with default PsdOptions settings image.save(stream, new com.aspose.imaging.imageoptions.PsdOptions()); } finally { image.dispose(); } } finally { stream.close(); }
public void save(OutputStream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)
Saves the image's data to the specified stream in the specified file format according to save options.
stream
- The stream to save the image's data to.optionsBase
- The save options.boundsRectangle
- The destination image bounds rectangle. Set the empty rectangle for use source bounds.com.aspose.ms.System.ArgumentNullException
- optionsBasecom.aspose.ms.System.ArgumentException
- Cannot save to the specified format as it is not supported at the moment.;optionsBaseImageSaveException
- Image export failed.The following example loads an image from a file, then saves a rectangular part of the image to a PNG file stream.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.bmp"); try { com.aspose.imaging.imageoptions.PngOptions saveOptions = new com.aspose.imaging.imageoptions.PngOptions(); com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(0, 0, image.getWidth(), image.getHeight() / 2); java.io.OutputStream outputStream = new java.io.FileOutputStream(dir + "sample.output.png"); try { // Save the upper half of the image to a file stream. image.save(outputStream, saveOptions, bounds); } finally { outputStream.close(); } } finally { image.dispose(); }
public abstract void setPalette(IColorPalette palette, boolean updateColors)
Sets the image palette.
palette
- The palette to set.updateColors
- if set to true
colors will be updated according to the new palette; otherwise color indexes remain unchanged. Note that unchanged indexes may crash the image on loading if some indexes have no corresponding palette entries.