Packages

 

com.aspose.imaging

Interfaces

Classes

Exceptions

com.aspose.imaging

Class Image

  • All Implemented Interfaces:
    IObjectWithBounds, com.aspose.imaging_internal.progressmanagement.IProgressEventHandler, com.aspose.imaging_internal.progressmanagement.IProgressInformer, com.aspose.ms.System.IDisposable, Closeable, AutoCloseable
    Direct Known Subclasses:
    CdrImage, CdrImagePage, MetafileImage, MetaImage, MultiPageImage, RasterImage, SvgImage, VectorImage


    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.

    See Also:
    DataStreamSupporter, IObjectWithBounds
    Code example:

    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();
    }
    

    • Method Detail

      • canLoad

        public static boolean canLoad(String filePath)

        Determines whether image can be loaded from the specified file path.

        Parameters:
        filePath - The file path.
        Returns:
        true if image can be loaded from the specified file; otherwise, false.
        Code example:

        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");
        

      • canLoad

        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.

        Parameters:
        filePath - The file path.
        loadOptions - The load options.
        Returns:
        true if image can be loaded from the specified file; otherwise, false.
      • canLoad

        public static boolean canLoad(InputStream stream)

        Determines whether image can be loaded from the specified stream.

        Parameters:
        stream - The stream to load from.
        Returns:
        true if image can be loaded from the specified stream; otherwise, false.
        Code example:

        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);
        }
        

      • canLoad

        public static boolean canLoad(InputStream stream,
                                      LoadOptions loadOptions)

        Determines whether image can be loaded from the specified stream and optionally using the specified loadOptions.

        Parameters:
        stream - The stream to load from.
        loadOptions - The load options.
        Returns:
        true if image can be loaded from the specified stream; otherwise, false.
      • create

        public static Image create(ImageOptionsBase imageOptions,
                                   int width,
                                   int height)

        Creates a new image using the specified create options.

        Parameters:
        imageOptions - The image options.
        width - The width.
        height - The height.
        Returns:
        The newly created image.
        Code example:

        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();
        }
        

      • getFileFormat

        public static long getFileFormat(String filePath)

        Gets the file format.

        Parameters:
        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.

        Returns:
        The determined file format.
        Code example:

        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);
        

      • getFileFormat

        public static long getFileFormat(InputStream stream)

        Gets the file format.

        Parameters:
        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.

        Returns:
        The determined file format.
        Code example:

        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
        

      • getFittingRectangle

        public static Rectangle getFittingRectangle(Rectangle rectangle,
                                                    int width,
                                                    int height)

        Gets rectangle which fits the current image.

        Parameters:
        rectangle - The rectangle to get fitting rectangle for.
        width - The object width.
        height - The object height.
        Returns:
        The fitting rectangle or exception if no fitting rectangle can be found.
      • getFittingRectangle

        public static Rectangle getFittingRectangle(Rectangle rectangle,
                                                    int[] pixels,
                                                    int width,
                                                    int height)

        Gets rectangle which fits the current image.

        Parameters:
        rectangle - The rectangle to get fitting rectangle for.
        pixels - The 32-bit ARGB pixels.
        width - The object width.
        height - The object height.
        Returns:
        The fitting rectangle or exception if no fitting rectangle can be found.
      • load

        public static Image load(String filePath,
                                 LoadOptions loadOptions)

        Loads a new image from the specified file.

        Parameters:
        filePath - The file path to load image from.
        loadOptions - The load options.
        Returns:
        The loaded image.
      • load

        public static Image load(String filePath)

        Loads a new image from the specified file.

        Parameters:
        filePath - The file path to load image from.
        Returns:
        The loaded image.
        Code example:

        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();
        }
        

      • load

        public static Image load(RandomAccessFile file,
                                 LoadOptions loadOptions)

        Loads a new image from the specified stream.

        Parameters:
        file - The file to load image from.
        loadOptions - The load options.
        Returns:
        The loaded image.
      • load

        public static Image load(RandomAccessFile file)

        Loads a new image from the specified stream.

        Parameters:
        file - The file to load image from.
        Returns:
        The loaded image.
      • load

        public static Image load(InputStream stream,
                                 LoadOptions loadOptions)

        Loads a new image from the specified stream.

        Parameters:
        stream - The stream to load image from.
        loadOptions - The load options.
        Returns:
        The loaded image.
      • load

        public static Image load(InputStream stream)

        Loads a new image from the specified stream.

        Parameters:
        stream - The stream to load image from.
        Returns:
        The loaded image.
        Code example:

        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();
        }
        

      • getProportionalWidth

        public static int getProportionalWidth(int width,
                                               int height,
                                               int newHeight)

        Gets a proportional width.

        Parameters:
        width - The width.
        height - The height.
        newHeight - The new height.
        Returns:
        The proportional width.
      • getProportionalHeight

        public static int getProportionalHeight(int width,
                                                int height,
                                                int newWidth)

        Gets a proportional height.

        Parameters:
        width - The width.
        height - The height.
        newWidth - The new width.
        Returns:
        The proportional height.
      • getBitsPerPixel

        public abstract int getBitsPerPixel()

        Gets the image bits per pixel count.

        Returns:
        The image bits per pixel count.
      • getContainer

        public Image getContainer()

        Gets the Image container.

        Value: The Image container.

        If this property is not null it indicates the image is contained whithin another image.

      • getHeight

        public abstract int getHeight()

        Gets the image height.

        Specified by:
        getHeight in interface IObjectWithBounds
        Returns:
        The image height.
      • getPalette

        public IColorPalette getPalette()

        Gets the color palette.

        Returns:
        The color palette.
      • setPalette

        public void setPalette(IColorPalette value)

        Sets the color palette.

        Parameters:
        value - The color palette.
      • getSize

        public Size getSize()

        Gets the image size.

        Specified by:
        getSize in interface IObjectWithBounds
        Returns:
        The image size.
        Code example:

        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
        

      • getWidth

        public abstract int getWidth()

        Gets the image width.

        Specified by:
        getWidth in interface IObjectWithBounds
        Returns:
        The image width.
      • getInterruptMonitor

        public InterruptMonitor getInterruptMonitor()

        Gets the interrupt monitor.

        Returns:
        the interrupt monitor.
      • setInterruptMonitor

        public void setInterruptMonitor(InterruptMonitor value)

        Sets the interrupt monitor.

        Parameters:
        value - the interrupt monitor.
      • getBufferSizeHint

        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 buffers
        Returns:
        the buffer size hint which is defined max allowed size for all internal buffers.
      • setBufferSizeHint

        public 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 buffers
        Parameters:
        value - the buffer size hint which is defined max allowed size for all internal buffers.
      • isAutoAdjustPalette

        public boolean isAutoAdjustPalette()

        Gets a value indicating whether automatic adjust palette.

        Returns:
        true if enable automatic adjust palette; otherwise, false.
      • setAutoAdjustPalette

        public void setAutoAdjustPalette(boolean value)

        Sets a value indicating whether automatic adjust palette.

        Parameters:
        value - true if enable automatic adjust palette; otherwise, false.
      • hasBackgroundColor

        public boolean hasBackgroundColor()

        Gets a value indicating whether image has background color.

      • getFileFormat

        public long getFileFormat()

        Gets a value of file format

      • getBackgroundColor

        public Color getBackgroundColor()

        Gets or sets a value for the background color.

      • setBackgroundColor

        public void setBackgroundColor(boolean value)

        Gets or sets a value indicating whether image has background color.

      • setBackgroundColor

        public void setBackgroundColor(Color value)

        Gets or sets a value for the background color.

      • getProgressEventHandler

        public final ProgressEventHandler getProgressEventHandler()

        Gets the progress event handler information.

        Specified by:
        getProgressEventHandler in interface com.aspose.imaging_internal.progressmanagement.IProgressEventHandler
        Returns:
        the progress event handler information.
      • getProgressEventHandlerInfo

        public final ProgressEventHandlerInfo getProgressEventHandlerInfo()

        Gets the progress event handler information.

        Value: The progress event handler information.
        Specified by:
        getProgressEventHandlerInfo in interface com.aspose.imaging_internal.progressmanagement.IProgressInformer
        Returns:
        the progress event handler information.
      • canSave

        public boolean canSave(ImageOptionsBase options)

        Determines whether image can be saved to the specified file format represented by the passed save options.

        Parameters:
        options - The save options to use.
        Returns:
        true if image can be saved to the specified file format represented by the passed save options; otherwise, false.
        Code example:

        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();
        }
        

      • resize

        public void resize(int newWidth,
                           int newHeight)

        Resizes the image. The default ResizeType.LeftTopToLeftTop is used.

        Parameters:
        newWidth - The new width.
        newHeight - The new height.
        Code example:

        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();
            }
        }
        

      • resize

        public abstract void resize(int newWidth,
                                    int newHeight,
                                    int resizeType)

        Resizes the image.

        Parameters:
        newWidth - The new width.
        newHeight - The new height.
        resizeType - The resize type.
        Code example:

        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();
        }
        

      • resize

        public abstract void resize(int newWidth,
                                    int newHeight,
                                    ImageResizeSettings settings)

        Resizes the image.

        Parameters:
        newWidth - The new width.
        newHeight - The new height.
        settings - The resize settings.
        Code example:

        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();
        }
        

      • getDefaultOptions

        public ImageOptionsBase getDefaultOptions(Object[] args)

        Gets the default options.

        Parameters:
        args - The arguments.
        Returns:
        Default options
      • getOriginalOptions

        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.

        Returns:
        The options based on the original file settings.
      • resizeWidthProportionally

        public void resizeWidthProportionally(int newWidth)

        Resizes the width proportionally.

        Parameters:
        newWidth - The new width.
      • resizeHeightProportionally

        public void resizeHeightProportionally(int newHeight)

        Resizes the height proportionally.

        Parameters:
        newHeight - The new height.
      • resizeWidthProportionally

        public void resizeWidthProportionally(int newWidth,
                                              int resizeType)

        Resizes the width proportionally.

        Parameters:
        newWidth - The new width.
        resizeType - Type of the resize.
        Code example:

        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");
        }
        

      • resizeHeightProportionally

        public void resizeHeightProportionally(int newHeight,
                                               int resizeType)

        Resizes the height proportionally.

        Parameters:
        newHeight - The new height.
        resizeType - Type of the resize.
        Code example:

        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();
        }
        

      • resizeWidthProportionally

        public void resizeWidthProportionally(int newWidth,
                                              ImageResizeSettings settings)

        Resizes the width proportionally.

        Parameters:
        newWidth - The new width.
        settings - The image resize settings.
      • resizeHeightProportionally

        public void resizeHeightProportionally(int newHeight,
                                               ImageResizeSettings settings)

        Resizes the height proportionally.

        Parameters:
        newHeight - The new height.
        settings - The image resize settings.
      • rotateFlip

        public abstract void rotateFlip(int rotateFlipType)

        Rotates, flips, or rotates and flips the image.

        Parameters:
        rotateFlipType - Type of the rotate flip.
        Code example:

        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();
        }
        

      • save

        public final void save()

        Saves the image data to the underlying stream.

        Overrides:
        save in class DataStreamSupporter
        Code example:

        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
        

      • save

        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.

        Parameters:
        filePath - The file path.
        options - The options.
        Code example:

        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();
        }
        

      • save

        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.

        Parameters:
        filePath - The file path.
        options - The options.
        boundsRectangle - The destination image bounds rectangle. Set the empty rectangle for use sourse bounds.
        Throws:
        com.aspose.ms.System.ArgumentNullException - options
        ImageSaveException - Image saving failed.
        Code example:

        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();
        }
        

      • save

        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.

        Parameters:
        file - The file to save the image's data to.
        options - The options.
      • save

        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.

        Parameters:
        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.
        Throws:
        com.aspose.ms.System.ArgumentNullException - optionsBase
        com.aspose.ms.System.ArgumentException - Cannot save to the specified format as it is not supported at the moment.;optionsBase
        ImageSaveException - Image export failed.
      • save

        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.

        Parameters:
        stream - The stream to save the image's data to.
        optionsBase - The save options.
        Throws:
        com.aspose.ms.System.ArgumentNullException - optionsBase
        com.aspose.ms.System.ArgumentException - Cannot save to the specified format as it is not supported at the moment.;optionsBase
        ImageSaveException - Image export failed.
        Code example:

        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();
        }
        

      • save

        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.

        Parameters:
        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 sourse bounds.
        Throws:
        com.aspose.ms.System.ArgumentNullException - optionsBase
        com.aspose.ms.System.ArgumentException - Cannot save to the specified format as it is not supported at the moment.;optionsBase
        ImageSaveException - Image export failed.
        Code example:

        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();
        }
        

      • setPalette

        public abstract void setPalette(IColorPalette palette,
                                        boolean updateColors)

        Sets the image palette.

        Parameters:
        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.