Leadtools.Codecs Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
SaveImage Event
See Also  Example
Leadtools.Codecs Namespace > RasterCodecs Class : SaveImage Event



Occurs during the file save process to provide functionality for manually handling the output image data or monitoring a progress status.

Syntax

Visual Basic (Declaration) 
Public Event SaveImage() As EventHandler(Of CodecsSaveImageEventArgs)
Visual Basic (Usage)Copy Code
Dim instance As RasterCodecs
Dim handler As EventHandler(Of CodecsSaveImageEventArgs)
 
AddHandler instance.SaveImage, handler
C# 
public event EventHandler<CodecsSaveImageEventArgs> SaveImage()
C++/CLI 
public:
event EventHandler<CodecsSaveImageEventArgs>^ SaveImage();

Example

This example will double the density of an image as it is being saved.

Visual BasicCopy Code
Public Sub SaveImageExample()
   RasterCodecs.Startup()
   Dim codecs As RasterCodecs = New RasterCodecs()

   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
   Dim destFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_SaveImage.cmp"

   ' Load the source file (make sure to load as 24 bits/pixel)
   Dim image As RasterImage = codecs.Load(srcFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1)

   ' Add a handler to the SaveImage event
   AddHandler codecs.SaveImage, AddressOf codecs_SaveImage

   ' Save the image
   codecs.Save(image, destFileName, RasterImageFormat.Cmp, 24)

   RemoveHandler codecs.SaveImage, AddressOf codecs_SaveImage

   image.Dispose()

   ' Clean up
   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub

Private _saveImageScanLine As Byte()

Private Sub codecs_SaveImage(ByVal sender As Object, ByVal e As CodecsSaveImageEventArgs)
   If e.Row = 0 Then
      ' Show information about the image being saved
      Console.WriteLine("Saving an image with {0} bpp to {1}", e.Image.BitsPerPixel, e.FileName)
      Console.WriteLine("Offset: {0}, OffsetValid: {1}", e.Offset, e.OffsetValid)
      Console.WriteLine("Page: {0} of {1}", e.Page, e.PageCount)
   End If

   Console.WriteLine("Row: {0}, Lines {1}", e.Row, e.Lines)

   ' Get the scanlines from the image
   Dim lastRow As Integer = e.Row + e.Lines
   Dim bytesPerLine As Integer = e.Image.BytesPerLine
   Dim buffer As IntPtr = e.Buffer

   ' See if we need to re-allocate the managed buffer
   If _saveImageScanLine Is Nothing OrElse _saveImageScanLine.Length < bytesPerLine Then
      _saveImageScanLine = New Byte(bytesPerLine - 1) {}
   End If

   Dim y As Integer = e.Row
   Do While y < lastRow
      ' Get the row into the managed buffer
      e.Image.Access()
      e.Image.GetRow(y, _saveImageScanLine, 0, bytesPerLine)
      e.Image.Release()

      ' We got the data, now double the intensity
      ' Remember, this is 24-bits/pixel
      Dim x As Integer = 0
      Do While x < bytesPerLine
         _saveImageScanLine(x) = CType(_saveImageScanLine(x) * 2, Byte)
         x += 1
      Loop

      ' Copy the data into the buffer
      Marshal.Copy(_saveImageScanLine, 0, buffer, bytesPerLine)

      ' Move the pointer to the position in the buffer for the next row
      buffer = New IntPtr(buffer.ToInt64() + bytesPerLine)
      y += 1
   Loop
End Sub
C#Copy Code
public void SaveImageExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
   string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "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); 
 
   // 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(); 
   RasterCodecs.Shutdown(); 

 
byte[] _saveImageScanLine; 
 
void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e) 

   if(e.Row == 0) 
   { 
      // Show information about the image being saved 
      Console.WriteLine("Saving an image with {0} bpp to {1}", e.Image.BitsPerPixel, e.FileName); 
      Console.WriteLine("Offset: {0}, OffsetValid: {1}", e.Offset, e.OffsetValid); 
      Console.WriteLine("Page: {0} of {1}", e.Page, e.PageCount); 
   } 
 
   Console.WriteLine("Row: {0}, Lines {1}", e.Row, e.Lines); 
 
   // Get the scanlines from the image 
   int lastRow = e.Row + e.Lines; 
   int bytesPerLine = e.Image.BytesPerLine; 
   IntPtr buffer = e.Buffer; 
 
   // See if we need to re-allocate the managed buffer 
   if(_saveImageScanLine == null || _saveImageScanLine.Length < bytesPerLine) 
      _saveImageScanLine = new byte[bytesPerLine]; 
 
   for(int y = e.Row; y < lastRow; y++) 
   { 
      // Get the row into the managed buffer 
      e.Image.Access(); 
      e.Image.GetRow(y, _saveImageScanLine, 0, bytesPerLine); 
      e.Image.Release(); 
 
      // We got the data, now double the intensity 
      // Remember, this is 24-bits/pixel 
      for(int x = 0; x < bytesPerLine; x++) 
         _saveImageScanLine[x] *= 2; 
 
      // Copy the data into the buffer 
      Marshal.Copy(_saveImageScanLine, 0, buffer, bytesPerLine); 
 
      // Move the pointer to the position in the buffer for the next row 
      buffer = new IntPtr(buffer.ToInt64() + bytesPerLine); 
   } 
}

Remarks

This event will fire during saving images with the Save methods. You can use this event to get information about the image being saved, manually provide the image scanline data being saved data or provide a progress status as well as to abort the save operation.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also