←Select platform

CodecsSaveImageEventArgs Class

Summary
Provides information for the RasterCodecs.SaveImage event.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public class CodecsSaveImageEventArgs : EventArgs 
@interface LTCodecsSaveImageEventArgs : NSObject 
public class CodecsSaveImageEvent extends LeadEvent 
public ref class CodecsSaveImageEventArgs : public System.EventArgs  
class CodecsSaveImageEventArgs(EventArgs): 
Remarks

The Buffer property works as the input and output buffer containing the image data to save. If the value of RetrieveDataFromImage is set to false (the default), then the user is always responsible for providing the image data by setting in Buffer. If the value of RetrieveDataFromImage is set to true, then the RasterCodecs object will populate the Buffer prior to raising this event. The user can then inspect or modify the scanlines data or simple ignore it to save the original image data as is.

Notice that on either case, the user must provide the scanline data in the source image original format (stored in the Image property. The RasterCodecs object will then convert this data to the appropriate output format if needed, for example, if the user instructed the RasterCodecs object to save the image in a different file format than the original image.

This event can also be used to provide feedback to the user on the save operation progression and a chance for cancellation. To understand the various page and percentages, consider the following example.

File Ocr.tif is a multipage TIF file containing 4 pages. We want to save pages 2 through 4 (a total of 3 pages). We make the following call:

C#
private void Test(RasterCodecs rasterCodecsObject) 
{ 
   // Load the image  
   RasterImage image = rasterCodecsObject.Load(@"C:\LEADTOOLS23\Resources\Images\Ocr.tif"); 
    
   // Tell the RasterCodecs object to automatically set the image data to save from the source image 
   rasterCodecsObject.Options.Save.RetrieveDataFromImage = true; 
 
   // Subscribe to the SaveImage event  
   rasterCodecsObject.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(rasterCodecsObject_SaveImage); 
    
   // Save pages 2 through 4 (total of 3 pages) 
   int firstImagePage = 2; 
   int lastImagePage = 4; 
   int firstFilePage = 1; 
    
   // Save  
   rasterCodecsObject.Save( 
      image,  
      @"C:\LEADTOOLS23\Resources\Images\Ocr_pages2through4.tif", 
      RasterImageFormat.Tif, 
      1, // Bits per pixel 
      firstImagePage, 
      lastImagePage, 
      firstFilePage, 
      CodecsSavePageMode.Overwrite); 
    
   // Unsubscribe to the LoadImage  
   event rasterCodecsObject.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(rasterCodecsObject_SaveImage);  
   image.Dispose(); 
}  
 
private void rasterCodecsObject_SaveImage(object sender, CodecsSaveImageEventArgs e) 
{ 
} 

Inside the event handler, we will have the following values:

Member Values
FirstPage Will always be 1 since we specified 1 as the first file page number.
Page Will go from 1 through 3 since we are saving 3 pages total starting as destination (file) page number 1.
LastPage Will always be 3 since we are saving 3 pages from 1 to 3.
ImagePage Will go from 2 through 4 since we are saving existing pages 2, 3, and 4.
PagePercent Will go from 0 to 100 three times since we are saving three pages. This value will reset back to zero whenever Page and ImagePage changes.
TotalPercent Will go from 0 to 100 during the whole saving operation. When this value is 100, all the pages are saved and the method returns.
Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
public void SaveImageExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_SaveImage.cmp"); 
 
   // Load the source file (make sure to load as 24 bits/pixel) 
   RasterImage image = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1); 
 
   // Instruct RasterCodecs to generate the saved scanlines data for us to manipulate 
   codecs.Options.Save.RetrieveDataFromImage = true; 
 
   // Add a handler to the SaveImage event 
   codecs.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage); 
 
   // Save the image 
   codecs.Save(image, destFileName, RasterImageFormat.Cmp, 24); 
 
   codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage); 
 
   image.Dispose(); 
 
   // Clean up 
   codecs.Dispose(); 
} 
 
private void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e) 
{ 
   // This example works with images saved as 24-bit per pixel only 
   Debug.Assert(e.Image.BitsPerPixel == 24); 
   e.Cancel = false; 
 
   if (e.Row == 0) 
   { 
      // Show information about the image being saved 
      Debug.WriteLine("Saving an image with {0} bpp to {1}", e.Image.BitsPerPixel, e.FileName); 
      Debug.WriteLine("Offset: {0}, OffsetValid: {1}", e.Offset, e.OffsetValid); 
      Debug.WriteLine("Page: {0} of {1}", e.Page, e.LastPage - e.FirstPage + 1); 
      Debug.WriteLine("Page percent: {0}%, Total percent: {1}%, Image Page: {2}", e.PagePercent, e.TotalPercent, e.ImagePage); 
 
      if (e.Stream != null) 
      { 
         Debug.WriteLine("Stream: {0}", e.Stream); 
      } 
   } 
 
   Debug.WriteLine("Row: {0}, Lines {1}", e.Row, e.Lines); 
 
   // Get the scanlines from the image 
   int scanlineLength = e.Image.BytesPerLine; 
   byte[] scanline = new byte[scanlineLength]; 
 
   // Loop through all the scanlines in the data 
   for (int y = 0; y < e.Lines; y++) 
   { 
      // Get this row 
      e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength); 
 
      // We got the data, now double the intensity 
      // Remember, this is 24-bits/pixel 
      for (int x = 0; x < scanlineLength; x++) 
      { 
         scanline[x] *= 2; 
      } 
 
      // Copy it back to the event buffer 
      e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength); 
   } 
} 
 
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 saveImageExample() { 
   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 file (make sure to load as 24 bits/pixel) 
   RasterImage image = codecs.load(srcFileName, 24, CodecsLoadByteOrder.BGR, 1, 1); 
 
   // Instruct RasterCodecs to generate the saved scanlines data for us to 
   // manipulate 
   codecs.getOptions().getSave().setRetrieveDataFromImage(true); 
 
   // Add a handler to the SaveImage event 
   codecs.addSaveImageListener(codecsSaveImage); 
   // Save the image 
   codecs.save(image, destFileName, RasterImageFormat.CMP, 24); 
 
   assertTrue("File unsuccessfully saved to" + destFileName, (new File(destFileName)).exists()); 
   System.out.println("File is saved to" + destFileName); 
 
   // Clean up 
   codecs.removeSaveImageListener(codecsSaveImage); 
   image.dispose(); 
   codecs.dispose(); 
} 
 
CodecsSaveImageListener codecsSaveImage = new CodecsSaveImageListener() { 
 
   @Override 
   public void onSaveImage(CodecsSaveImageEvent e) { 
      // This example works with images saved as 24-bit per pixel only 
      assertTrue(e.getImage().getBitsPerPixel() == 24); 
      e.setCancel(false); 
 
      if (e.getRow() == 0) { 
         // Show information about the image being saved 
         System.out.println("Saving an image with " + e.getImage().getBitsPerPixel() + " bpp"); 
         System.out.println("Page: " + e.getPage() + " of " + (e.getLastPage() - e.getFirstPage() + 1)); 
         System.out.println("Page percent: " + e.getPagePercent() + "%, Total percent: " + e.getTotalPercent() 
               + "%, Image Page: " + e.getImagePage()); 
 
         if (e.getStream() != null) { 
            System.out.println("Stream: " + e.getStream()); 
         } 
 
      } 
 
      System.out.println("Row: " + e.getRow() + ", Lines " + e.getLines()); 
 
      // Get the scanlines from the image 
      int scanlineLength = e.getImage().getBytesPerLine(); 
      byte[] scanline = new byte[scanlineLength]; 
 
      // Loop through all the scanlines in the data 
      for (int y = 0; y < e.getLines(); y++) { 
         // Get this row 
         e.getBuffer().getData(y * scanlineLength, scanline, 0, scanlineLength); 
 
         // We got the data, now double the intensity 
         // Remember, this is 24-bits/pixel 
         for (int x = 0; x < scanlineLength; x++) { 
            scanline[x] *= 2; 
         } 
 
         // Copy it back to the event buffer 
         e.getBuffer().setData(y * scanlineLength, scanline, 0, scanlineLength); 
      } 
   } 
 
}; 
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.