Image.Save

Save()

Saves the image data to the underlying stream.

public void Save()

Examples

The following example shows how to save an entiree BMP image or part of it to a file or stream.

[C#]

string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
        
    // Convert to a black-white image
    bmpImage.BinarizeOtsu();

    // Save to the same location with default options.
    image.Save();

    Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

    // A palette contains only two colors: Black and White in this case.
    saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.CreateMonochrome();

    // For all monochrome images (including black-white ones) it is enough to allocate 1 bit per pixel.
    saveOptions.BitsPerPixel = 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.
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
    image.Save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds);

    // Save the entire image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions);
        System.Console.WriteLine("The size of the whole image in bytes: {0}", stream.Length);
    }

    // Save the central part of the image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions, bounds);
        System.Console.WriteLine("The size of the central part of the image in bytes: {0}", stream.Length);
    }
}
//The output may look like this:
//The size of the whole image in bytes: 24062
//The size of the central part of the image in bytes: 6046

See Also


Save(string)

Saves the image to the specified file location.

public override void Save(string filePath)
ParameterTypeDescription
filePathStringThe file path to save the image to.

See Also


Save(string, ImageOptionsBase)

Saves the object’s data to the specified file location in the specified file format according to save options.

public virtual void Save(string filePath, ImageOptionsBase options)
ParameterTypeDescription
filePathStringThe file path.
optionsImageOptionsBaseThe options.

Examples

The following example loads a BMP image from a file, then saves the image to a PNG file.

[C#]

string dir = "c:\\temp\\";

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    // Save the entire image to a PNG file.
    Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
    image.Save(dir + "output.png", saveOptions);
}

This example shows the simple steps to Save an Image. To demonstrate this operation, we load an existing file from some disk location, performs Rotate operation on the image and Save the image in PSD format using File Path

[C#]

string dir = "c:\\temp\\";

//Create an instance of image class and initialize it with an existing file through File path
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    //Rotate the image at 180 degree about X axis
    image.RotateFlip(Aspose.Imaging.RotateFlipType.Rotate180FlipX);

    //Save the Image as PSD to File Path with default PsdOptions settings
    image.Save(dir + "output.psd", new Aspose.Imaging.ImageOptions.PsdOptions());
}

The following example shows how to save an entiree BMP image or part of it to a file or stream.

[C#]

string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
        
    // Convert to a black-white image
    bmpImage.BinarizeOtsu();

    // Save to the same location with default options.
    image.Save();

    Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

    // A palette contains only two colors: Black and White in this case.
    saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.CreateMonochrome();

    // For all monochrome images (including black-white ones) it is enough to allocate 1 bit per pixel.
    saveOptions.BitsPerPixel = 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.
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
    image.Save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds);

    // Save the entire image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions);
        System.Console.WriteLine("The size of the whole image in bytes: {0}", stream.Length);
    }

    // Save the central part of the image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions, bounds);
        System.Console.WriteLine("The size of the central part of the image in bytes: {0}", stream.Length);
    }
}
//The output may look like this:
//The size of the whole image in bytes: 24062
//The size of the central part of the image in bytes: 6046

See Also


Save(string, ImageOptionsBase, Rectangle)

Saves the object’s data to the specified file location in the specified file format according to save options.

public virtual void Save(string filePath, ImageOptionsBase options, Rectangle boundsRectangle)
ParameterTypeDescription
filePathStringThe file path.
optionsImageOptionsBaseThe options.
boundsRectangleRectangleThe destination image bounds rectangle. Set the empty rectangle for use sourse bounds.

Exceptions

exceptioncondition
ArgumentNullExceptionoptions
ImageSaveExceptionImage saving failed.

Examples

The following example loads a BMP image from a file, then saves a rectangular part of the image to a PNG file.

[C#]

string dir = "c:\\temp\\";

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    // Save the upper half of the image to a PNG file.
    Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(0, 0, image.Width, image.Height / 2);
    image.Save(dir + "output.png", saveOptions, bounds);
}

The following example shows how to save an entiree BMP image or part of it to a file or stream.

[C#]

string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
        
    // Convert to a black-white image
    bmpImage.BinarizeOtsu();

    // Save to the same location with default options.
    image.Save();

    Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

    // A palette contains only two colors: Black and White in this case.
    saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.CreateMonochrome();

    // For all monochrome images (including black-white ones) it is enough to allocate 1 bit per pixel.
    saveOptions.BitsPerPixel = 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.
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
    image.Save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds);

    // Save the entire image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions);
        System.Console.WriteLine("The size of the whole image in bytes: {0}", stream.Length);
    }

    // Save the central part of the image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions, bounds);
        System.Console.WriteLine("The size of the central part of the image in bytes: {0}", stream.Length);
    }
}
//The output may look like this:
//The size of the whole image in bytes: 24062
//The size of the central part of the image in bytes: 6046

See Also


Save(Stream, ImageOptionsBase)

Saves the image’s data to the specified stream in the specified file format according to save options.

public void Save(Stream stream, ImageOptionsBase optionsBase)
ParameterTypeDescription
streamStreamThe stream to save the image’s data to.
optionsBaseImageOptionsBaseThe save options.

Exceptions

exceptioncondition
ArgumentNullExceptionoptionsBase
ArgumentExceptionCannot save to the specified format as it is not supported at the moment.;optionsBase
ImageSaveExceptionImage export failed.

Examples

The following example loads an image from a file, then saves the image to a PNG file stream.

[C#]

string dir = "c:\\temp\\";

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
    using (System.IO.Stream outputStream = System.IO.File.Open(dir + "output.png", System.IO.FileMode.Create))
    {
        // Save the entire image to a file stream.
        image.Save(outputStream, saveOptions);
    }
}

This example shows the process of Saving an Image to MemoryStream. To demonstrate this operation, example loads an existing file from some disk location, performs Rotate operation on the image and Save the image in PSD format

[C#]

//Create an instance of MemoryStream
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
    //Create an instance of image class and initialize it with an existing file through File path
    using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"C:\temp\sample.bmp"))
    {
        //Rotate the image at 180 degree about X axis
        image.RotateFlip(Aspose.Imaging.RotateFlipType.Rotate180FlipX);

        //Save the Image as PSD to MemoryStream with default PsdOptions settings
        image.Save(stream, new Aspose.Imaging.ImageOptions.PsdOptions());
    }
}

The following example shows how to save an entiree BMP image or part of it to a file or stream.

[C#]

string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
        
    // Convert to a black-white image
    bmpImage.BinarizeOtsu();

    // Save to the same location with default options.
    image.Save();

    Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

    // A palette contains only two colors: Black and White in this case.
    saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.CreateMonochrome();

    // For all monochrome images (including black-white ones) it is enough to allocate 1 bit per pixel.
    saveOptions.BitsPerPixel = 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.
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
    image.Save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds);

    // Save the entire image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions);
        System.Console.WriteLine("The size of the whole image in bytes: {0}", stream.Length);
    }

    // Save the central part of the image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions, bounds);
        System.Console.WriteLine("The size of the central part of the image in bytes: {0}", stream.Length);
    }
}
//The output may look like this:
//The size of the whole image in bytes: 24062
//The size of the central part of the image in bytes: 6046

See Also


Save(Stream, ImageOptionsBase, Rectangle)

Saves the image’s data to the specified stream in the specified file format according to save options.

public virtual void Save(Stream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)
ParameterTypeDescription
streamStreamThe stream to save the image’s data to.
optionsBaseImageOptionsBaseThe save options.
boundsRectangleRectangleThe destination image bounds rectangle. Set the empty rectangle for use source bounds.

Exceptions

exceptioncondition
ArgumentNullExceptionoptionsBase
ArgumentExceptionCannot save to the specified format as it is not supported at the moment.;optionsBase
ImageSaveExceptionImage export failed.

Examples

The following example loads an image from a file, then saves a rectangular part of the image to a PNG file stream.

[C#]

string dir = "c:\\temp\\";

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(0, 0, image.Width, image.Height / 2);
    using (System.IO.Stream outputStream = System.IO.File.Open(dir + "sample.output.png", System.IO.FileMode.Create))
    {
        // Save the upper half of the image to a file stream.
        image.Save(outputStream, saveOptions, bounds);
    }
}

The following example shows how to save an entiree BMP image or part of it to a file or stream.

[C#]

string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
    Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
        
    // Convert to a black-white image
    bmpImage.BinarizeOtsu();

    // Save to the same location with default options.
    image.Save();

    Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

    // A palette contains only two colors: Black and White in this case.
    saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.CreateMonochrome();

    // For all monochrome images (including black-white ones) it is enough to allocate 1 bit per pixel.
    saveOptions.BitsPerPixel = 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.
    Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2);
    image.Save(dir + "sample.bw.palettized.part.bmp", saveOptions, bounds);

    // Save the entire image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions);
        System.Console.WriteLine("The size of the whole image in bytes: {0}", stream.Length);
    }

    // Save the central part of the image to a memory stream
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, saveOptions, bounds);
        System.Console.WriteLine("The size of the central part of the image in bytes: {0}", stream.Length);
    }
}
//The output may look like this:
//The size of the whole image in bytes: 24062
//The size of the central part of the image in bytes: 6046

See Also