←Select platform

Save(RasterImage,string,RasterImageFormat,int,int,int,int,CodecsSavePageMode) Method

Summary

Saves one or more pages of a RasterImage to a file in any of the supported compressed or uncompressed formats.

Syntax
C#
VB
Objective-C
C++
Public Overloads Sub Save( _ 
   ByVal image As RasterImage, _ 
   ByVal fileName As String, _ 
   ByVal format As RasterImageFormat, _ 
   ByVal bitsPerPixel As Integer, _ 
   ByVal firstPage As Integer, _ 
   ByVal lastPage As Integer, _ 
   ByVal firstSavePageNumber As Integer, _ 
   ByVal pageMode As CodecsSavePageMode _ 
)  
- (BOOL)       save:(LTRasterImage *)image  
               file:(NSString *)file  
             format:(LTRasterImageFormat)format  
       bitsPerPixel:(NSInteger)bitsPerPixel  
          firstPage:(NSInteger)firstPage  
           lastPage:(NSInteger)lastPage  
firstSavePageNumber:(NSInteger)firstSavePageNumber  
           pageMode:(LTCodecsSavePageMode)pageMode  
              error:(NSError **)error 

Parameters

image
The RasterImage object that holds the image data.

fileName
A String containing the output file name.

format
The output file format. For valid values, 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 closet 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
1-based index of the first page in image to save.

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

firstSavePageNumber
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. firstSavePageNumber is not used.
CodecsSavePageMode.Insert Insert the new page(s) at the index specified by firstSavePageNumber.
CodecsSavePageMode.Replace Replace the page(s) starting at the index specified by firstSavePageNumber.
CodecsSavePageMode.Overwrite Overwrite the page(s) starting at the index specified by firstSavePageNumber.
|

Remarks

If the image is 8 bits per pixel or greater, use the LEAD CMP format or one of the JPEG (JTIF or JFIF) formats 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 options 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 the 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 
// Setup 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

This example will create a multipage image and save it to a file before using the Save method to insert and replace pages directly in the file.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
public void SaveFile2Example() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_SaveFile2.tif"); 
 
   // Use GDI+ to create a RasterImage with 4 pages containing text showing the page number 
   RasterImage image = null; 
   const int pageCount = 4; 
 
   using (System.Drawing.Font f = new System.Drawing.Font("Arial", 36, System.Drawing.FontStyle.Bold)) 
   using (System.Drawing.Bitmap btmp = new System.Drawing.Bitmap(320, 200)) 
   using (System.Drawing.StringFormat sf = new System.Drawing.StringFormat()) 
   { 
      System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, btmp.Width, btmp.Height); 
      sf.Alignment = System.Drawing.StringAlignment.Center; 
      sf.LineAlignment = System.Drawing.StringAlignment.Center; 
 
      for (int i = 1; i <= pageCount; i++) 
      { 
         // Create a GDI+ bitmap with the text 
         string text = "Page " + i; 
 
         using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(btmp)) 
         { 
            g.FillRectangle(System.Drawing.Brushes.White, rc); 
            g.DrawString(text, f, System.Drawing.Brushes.Black, rc, sf); 
         } 
 
         RasterImage tempImage = Leadtools.Drawing.RasterImageConverter.ConvertFromImage(btmp, Leadtools.Drawing.ConvertFromImageOptions.None); 
 
         if (image == null) 
            image = tempImage; 
         else 
            image.AddPage(tempImage); 
      } 
   } 
 
   // Save all the pages to the file 
   // The file should have 4 pages now: 1, 2, 3, 4 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite); 
   image.Dispose(); 
 
   // Load the 3rd page and insert it as the second 
   // The file should have 5 pages now: 1, 3, 2, 3, 4 
   image = codecs.Load(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, 3, 3); 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, 1, 2, CodecsSavePageMode.Insert); 
   image.Dispose(); 
 
   // Load the last page, and insert it as the first 
   // The file should have 5 pages now: 4, 1, 3, 2, 3, 4 
   image = codecs.Load(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, 5, 5); 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, 1, 1, CodecsSavePageMode.Insert); 
   image.Dispose(); 
 
   // Replace the 5th page with the 2nd 
   // The file should have 5 pages now: 4, 1, 3, 2, 1, 4 
   image = codecs.Load(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, 2, 2); 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, 1, 5, CodecsSavePageMode.Replace); 
   image.Dispose(); 
 
   // Delete the 2nd and 6th pages 
   // The file should have 5 pages now: 4, 3, 2, 1 
   codecs.DeletePage(destFileName, 2); 
   // Notice, -1 because we already deleted a page, so 6th page is not 5th 
   codecs.DeletePage(destFileName, 6 - 1); 
 
   // Clean up 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
Public Sub SaveFile2Example() 
   Dim codecs As RasterCodecs = New RasterCodecs() 
 
   Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_SaveFile2.tif") 
 
   ' Create a RasterImage with 4 pages containing text showing the page number 
   Dim image As RasterImage = Nothing 
   Const pageCount As Integer = 4 
 
   Using f As New System.Drawing.Font("Arial", 36, System.Drawing.FontStyle.Bold) 
      Using btmp As New System.Drawing.Bitmap(320, 200) 
         Using sf As New System.Drawing.StringFormat() 
            Dim rc As New System.Drawing.Rectangle(0, 0, btmp.Width, btmp.Height) 
            sf.Alignment = System.Drawing.StringAlignment.Center 
            sf.LineAlignment = System.Drawing.StringAlignment.Center 
 
            For i As Integer = 1 To pageCount 
               ' Create a GDI+ bitmap with the text 
               Dim text As String = "Page " + i.ToString() 
 
               Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(btmp) 
                  g.FillRectangle(System.Drawing.Brushes.White, rc) 
                  g.DrawString(text, f, System.Drawing.Brushes.Black, rc, sf) 
               End Using 
 
               Dim tempImage As RasterImage = RasterImageConverter.ConvertFromImage(btmp, ConvertFromImageOptions.None) 
 
               If image Is Nothing Then 
                  image = tempImage 
               Else 
                  image.AddPage(tempImage) 
               End If 
            Next 
         End Using 
      End Using 
   End Using 
 
   ' Save all the pages to the file 
   ' The file should have 4 pages now: 1, 2, 3, 4 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite) 
   image.Dispose() 
 
   ' Load the 3rd page and insert it as the second 
   ' The file should have 5 pages now: 1, 3, 2, 3, 4 
   image = codecs.Load(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, 3, 3) 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, 1, 2, CodecsSavePageMode.Insert) 
   image.Dispose() 
 
   ' Load the last page, and insert it as the first 
   ' The file should have 5 pages now: 4, 1, 3, 2, 3, 4 
   image = codecs.Load(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, 5, 5) 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, 1, 1, CodecsSavePageMode.Insert) 
   image.Dispose() 
 
   ' Replace the 5th page with the 2nd 
   ' The file should have 5 pages now: 4, 1, 3, 2, 1, 4 
   image = codecs.Load(destFileName, 0, CodecsLoadByteOrder.BgrOrGray, 2, 2) 
   codecs.Save(image, destFileName, RasterImageFormat.Tif, 1, 1, 1, 5, CodecsSavePageMode.Replace) 
   image.Dispose() 
 
   ' Delete the 2nd and 6th pages 
   ' The file should have 5 pages now: 4, 3, 2, 1 
   codecs.DeletePage(destFileName, 2) 
   ' Notice, -1 because we already deleted a page, so 6th page is not 5th 
   codecs.DeletePage(destFileName, 6 - 1) 
 
   ' Clean up 
   codecs.Dispose() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 
c#[Silverlight C# Example] 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Examples; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Windows.Media; 
 
public void SaveStream2Example(int width, int height, int pageCount, Stream outStreamTif) 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // Create a RasterImage with 4 pages containing text showing the page number 
   RasterImage image = null; 
 
   // The white background canvas 
   Canvas canvas = new Canvas(); 
   canvas.Width = width; 
   canvas.Height = height; 
   canvas.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)); 
 
   // The black text block 
   TextBlock textBlock = new TextBlock(); 
   textBlock.FontFamily = new FontFamily("Arial"); 
   textBlock.FontSize = 36; 
   textBlock.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)); 
   canvas.Children.Add(textBlock); 
 
   for (int i = 1; i <= pageCount; i++) 
   { 
      // The text we want on the page 
      string text = "Page " + i; 
      textBlock.Text = text; 
 
      // Center the text 
      double left = (canvas.Width - textBlock.ActualWidth) / 2; 
      double top = (canvas.Height - textBlock.ActualHeight) / 2; 
      textBlock.SetValue(Canvas.LeftProperty, left); 
      textBlock.SetValue(Canvas.TopProperty, top); 
 
      // Create a Writeable bitmap with the text 
      WriteableBitmap wb = new WriteableBitmap(width, height); 
 
      wb.Render(canvas, null); 
      wb.Invalidate(); 
 
      // Convert it to a RasterImage 
      RasterImage pageImage = RasterImageConverter.ConvertFromSource(wb, ConvertFromSourceOptions.None); 
 
      // Add it as a page to our main raster image 
      if (image == null) 
         image = pageImage; 
      else 
         image.AddPage(pageImage); 
   } 
 
   // Save all the pages to the file 
   // The file should have 4 pages now: 1, 2, 3, 4 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite); 
   image.Dispose(); 
 
   // Load the 3rd page and insert it as the second 
   // The file should have 5 pages now: 1, 3, 2, 3, 4 
   image = codecs.Load(outStreamTif, 0, CodecsLoadByteOrder.BgrOrGray, 3, 3); 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, 1, 2, CodecsSavePageMode.Insert); 
   image.Dispose(); 
 
   // Load the last page, and insert it as the first 
   // The file should have 5 pages now: 4, 1, 3, 2, 3, 4 
   image = codecs.Load(outStreamTif, 0, CodecsLoadByteOrder.BgrOrGray, 5, 5); 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, 1, 1, CodecsSavePageMode.Insert); 
   image.Dispose(); 
 
   // Replace the 5th page with the 2nd 
   // The file should have 5 pages now: 4, 1, 3, 2, 1, 4 
   image = codecs.Load(outStreamTif, 0, CodecsLoadByteOrder.BgrOrGray, 2, 2); 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, 1, 5, CodecsSavePageMode.Replace); 
   image.Dispose(); 
 
   // Delete the 2nd and 6th pages 
   // The file should have 5 pages now: 4, 3, 2, 1 
   codecs.DeletePage(outStreamTif, 2); 
   // Notice, -1 because we already deleted a page, so 6th page is not 5th 
   codecs.DeletePage(outStreamTif, 6 - 1); 
} 
vb[Silverlight VB Example] 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Windows.Media 
 
Public Sub SaveStream2Example(ByVal width As Integer, ByVal height As Integer, ByVal pageCount As Integer, ByVal outStreamTif As Stream) 
   Dim codecs As RasterCodecs = New RasterCodecs() 
 
   ' Create a RasterImage with 4 pages containing text showing the page number 
   Dim image As RasterImage = Nothing 
 
   ' The white background canvas 
   Dim canvas As Canvas = New Canvas() 
   canvas.Width = width 
   canvas.Height = height 
   canvas.Background = New SolidColorBrush(Color.FromArgb(255, 255, 255, 255)) 
 
   ' The black text block 
   Dim textBlock As TextBlock = New TextBlock() 
   textBlock.FontFamily = New FontFamily("Arial") 
   textBlock.FontSize = 36 
   textBlock.Foreground = New SolidColorBrush(Color.FromArgb(255, 0, 0, 0)) 
   canvas.Children.Add(textBlock) 
 
   Dim i As Integer = 1 
   Do While i <= pageCount 
      ' The text we want on the page 
      Dim text As String = "Page " & i 
      textBlock.Text = text 
 
      ' Center the text 
      Dim left As Double = (canvas.Width - textBlock.ActualWidth) / 2 
      Dim top As Double = (canvas.Height - textBlock.ActualHeight) / 2 
      textBlock.SetValue(canvas.LeftProperty, left) 
      textBlock.SetValue(canvas.TopProperty, top) 
 
      ' Create a Writeable bitmap with the text 
      Dim wb As WriteableBitmap = New WriteableBitmap(width, height) 
 
      wb.Render(canvas, Nothing) 
      wb.Invalidate() 
 
      ' Convert it to a RasterImage 
      Dim pageImage As RasterImage = RasterImageConverter.ConvertFromSource(wb, ConvertFromSourceOptions.None) 
 
      ' Add it as a page to our main raster image 
      If image Is Nothing Then 
         image = pageImage 
      Else 
         image.AddPage(pageImage) 
      End If 
      i += 1 
   Loop 
 
   ' Save all the pages to the file 
   ' The file should have 4 pages now: 1, 2, 3, 4 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite) 
   image.Dispose() 
 
   ' Load the 3rd page and insert it as the second 
   ' The file should have 5 pages now: 1, 3, 2, 3, 4 
   image = codecs.Load(outStreamTif, 0, CodecsLoadByteOrder.BgrOrGray, 3, 3) 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, 1, 2, CodecsSavePageMode.Insert) 
   image.Dispose() 
 
   ' Load the last page, and insert it as the first 
   ' The file should have 5 pages now: 4, 1, 3, 2, 3, 4 
   image = codecs.Load(outStreamTif, 0, CodecsLoadByteOrder.BgrOrGray, 5, 5) 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, 1, 1, CodecsSavePageMode.Insert) 
   image.Dispose() 
 
   ' Replace the 5th page with the 2nd 
   ' The file should have 5 pages now: 4, 1, 3, 2, 1, 4 
   image = codecs.Load(outStreamTif, 0, CodecsLoadByteOrder.BgrOrGray, 2, 2) 
   codecs.Save(image, outStreamTif, RasterImageFormat.Tif, 1, 1, 1, 5, CodecsSavePageMode.Replace) 
   image.Dispose() 
 
   ' Delete the 2nd and 6th pages 
   ' The file should have 5 pages now: 4, 3, 2, 1 
   codecs.DeletePage(outStreamTif, 2) 
   ' Notice, -1 because we already deleted a page, so 6th page is not 5th 
   codecs.DeletePage(outStreamTif, 6 - 1) 
End Sub 

Requirements

Target Platforms

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

Leadtools.Codecs Assembly