←Select platform

Save(RasterImage,Stream,long,RasterImageFormat,int,int,int,int,CodecsSavePageMode) Method

Summary
Saves the specified image to a stream using an offset within to begin saving.
Syntax
C#
C++/CLI
Python
def Save(self,pageMode): 

Parameters

image
The RasterImage object that holds the image data.

stream
A stream where the image data will be saved. When saving to a stream, LEADTOOLS will not truncate the stream (if it already had a size/data). LEADTOOLS will reset the stream position to the original position before the save.

offset
The offset within the specified stream to embed the saved image file. For example, if you specify 5, then 5 bytes of other data will precede the embedded file.

format
The output file format. For valid values, refer to Summary of All Supported File Formats.

bitsPerPixel
Resulting file's pixel depth. Note that not all bits per pixel are available to all file formats. For valid values, refer to Summary of All Supported File Formats. If  bitsPerPixel is 0, the image will be stored using the closest bits/pixel value supported by that format. For example, if a file format supports 1, 4, and 24 bits/pixel, and RasterImage.BitsPerPixel is 5, the file will be stored as 24 bit. Likewise, if RasterImage.BitsPerPixel is 2, the file will be stored as 4 bit.

firstPage
A 1-based index of the first page in the  image to save.

lastPage
A 1-based index of the last page in the  image to save. Pass -1 to save all pages from the  firstPage to the last page in the  image.

firstSavePageNumber
A 1-based index of the first output page. If the output file already exists, then this parameter lets you control which pages to overwrite and/or where to append the new pages.

pageMode
Determines how to handle the page when saving to multipage formats. This can be one of the following:

Value Meaning
CodecsSavePageMode.Append Append the new page(s) to the end of the file. If the file does not exist, this option will create the file and add the pages to it. The firstSavePageNumber parameter is not used.
CodecsSavePageMode.Insert Insert the new page(s) at the index specified by the firstSavePageNumber.
CodecsSavePageMode.Replace Replace the page(s) starting at the index specified by the firstSavePageNumber.
CodecsSavePageMode.Overwrite Overwrite the entire file.
|

Return Value

The size of the embedded image file, in bytes.

Remarks

Use this method to embed an image file in another file.

If the output file format supports multipage files, all the pages in the  image will be saved to the file.

If the image is 8 bits per pixel or greater, use a LEAD CMP or JPEG compressed format to save disk space.

If the image is 1-bit per pixel, use the LEAD 1-bit format or a CCITT Group 3 or 4 format to save disk space.

If the image has a region, the region stored in the image will be saved if the image is saved as one of the TIFF file formats. For more information, refer to Saving A Region. Note, however, that the ability to save a region inside a TIFF file must be unlocked. This requires a Document Imaging or Document Imaging toolkit.

Only TIFF and DICOM file formats are capable of saving images that have been window-leveled. Images can be window-leveled by calling RasterImage.WindowLevel and specifying RasterWindowLevelMode.PaintAndProcessing, by using the WindowLevelCommand, or by loading an image from a file format that supports window-leveling. If a window-leveled image is saved as any other file format, the image data will be converted before being saved. For more information, refer to Saving Window-Leveled Images.

Use the CodecsSaveOptions class to set up other save option parameters before calling this method.

Use FormatSupportsMultipageSave to determine if the format supports saving into a multipage file.

Use the SaveImage event to provide progress feedback or to set or modify the saved image data.

This method supports signed data images, but only DICOM and TIFF formats support signed data. This method will throw an exception if you attempt to save a signed image to a format other than DICOM or TIFF.

In LEADTOOLS version 17 and up, when saving a colored image (such as a 24-bits per pixel image) to bitonal (1-bit per pixel), the RasterCodecs object will not use any dithering when converting the image data. This is done because dithering is not recommended when converting colored images containing text for document processing such as OCR and Barcode. The resulting text will be fuzzy and hard for a recognition engine to process. To save a colored image as bitonal with Floyd-Stein dithering (the behavior of LEADTOOLS 16.5 and earlier) use CodecsSaveOptions.UseImageDitheringMethod along with RasterImage.DitheringMethod as illustrated below:

// 'codecs' is the RasterCodecs to use when saving 
// 'image' is a colored RasterImage object 
// Set up FloydStein dithering: 
codecs.Options.Save.UseImageDitheringMethod = true; 
image.DitheringMethod = RasterDitheringMethod.FloydStein; 
// Save the image as 1-bpp with auto-dithering: 
codecs.Save(image, fileName, RasterImageFormat.Tif, 1); 

For information about quality factors, refer to Compression Quality Factors.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
 
public void SaveStream3Example() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_SaveStream3.bin"); 
 
   // Load the source image 
   RasterImage image = codecs.Load(srcFileName); 
 
   // Create a memory stream 
   MemoryStream ms = new MemoryStream(); 
 
   // Save this image to the stream after the header 
   Debug.WriteLine("Saving the image"); 
   long position = codecs.Save(image, ms, 0, RasterImageFormat.Cmp, 24); 
   image.Dispose(); 
 
   Debug.WriteLine("{0} bytes saved to the stream"); 
 
   // Save the stream to a file 
   using (FileStream fs = File.Create(destFileName)) 
      ms.WriteTo(fs); 
 
   ms.Close(); 
 
   // Make sure the saved file works 
 
   // Save the image into disk 
   Debug.WriteLine("Loading the file back"); 
   image = codecs.Load(destFileName); 
   image.Dispose(); 
 
   // Clean up 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
public void saveStream3Example() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1.cmp"); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "Image2.cmp"); 
 
   // Load the source image 
   RasterImage image = codecs.load(srcFileName); 
 
   // Create a memory stream 
   ILeadStream ms = LeadStreamFactory.create(); 
 
   // Save this image to the stream after the header 
   System.out.println("Saving the image"); 
   codecs.save(image, ms, 0, RasterImageFormat.CMP, 24); 
   image.dispose(); 
 
   System.out.printf("%s bytes saved to the stream%n", image.getBytesPerLine()); 
 
   // Save the stream to a file 
   ILeadStream fs = LeadStreamFactory.create(destFileName); 
   fs.write(ms.toInputStream().readAllBytes(), 0); 
 
   ms.close(); 
 
   // Save the image into disk 
   System.out.println("Loading the file back"); 
   image = codecs.load(destFileName); 
   codecs.save(image, destFileName, RasterImageFormat.CMP, 0); 
 
   assertTrue("Image unsuccessfully saved", (new File(destFileName)).exists()); 
   System.out.println("The image saves to " + destFileName); 
 
   // Clean up 
   codecs.dispose(); 
   image.dispose(); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.