LEADTOOLS画像ファイルサポート(Leadtools.Codecsアセンブリ)

フレームを表示

SaveImageイベント








ファイルのセーブプロセスの間に発生し、出力画像データの手動処理または進捗ステータスの監視のための機能を提供します。
構文
'Declaration
 
Public Event SaveImage As EventHandler(Of CodecsSaveImageEventArgs)
'Usage
 
Dim instance As RasterCodecs
Dim handler As EventHandler(Of CodecsSaveImageEventArgs)
 
AddHandler instance.SaveImage, handler
synchronized public void addSaveImageListener(CodecsSaveImageListener listener)
synchronized public void removeSaveImageListener(CodecsSaveImageListener listener)
            
add_SaveImage(function(sender, e))
remove_SaveImage(function(sender, e))

イベントデータ

イベントハンドラは、このイベントに関連するデータを格納するタイプCodecsSaveImageEventArgsの引数を受け取ります。以下のCodecsSaveImageEventArgsプロパティは、このイベントに特有の情報を提供します。

プロパティ説明
Buffer ユーザーが提供しなければならない1行以上の出力画像データを格納するメモリバッファを取得します。
Cancel ユーザーが保存プロセスを中止できる値を取得または設定します。
FileName 現時点では保存されているファイルの名前を取得します。
FirstPage 保存されている最初のページのインデックスを取得します。
Image 保存されているRasterImageオブジェクトを取得します。
ImagePage 現時点では保存されているRasterImage.Pageのインデックスを取得します。
LastPage 保存されている最後のページのインデックスを取得します。
Lines バッファからコピーする行数を取得します。
Offset 画像データの先頭のオフセットを取得します。
OffsetValid Offsetプロパティが有効かどうかについて決定する値を取得します。
Page 現時点では保存されているページ番号を取得します。
PagePercent 現在のページのセーブ完了パーセンテージを取得します。
Row バッファ内の最初の行の現在画像の行番号を取得します。
Stream 現時点では保存されているストリームオブジェクトを取得します。
Stream 現時点では保存されているストリームオブジェクトを取得します。
TotalPercent セーブ操作の全体完了パーセントを取得します。
解説

このイベントは、保存メソッドでイメージを保存することの間、発生します。このイベントを使うならば、画像データを提供されたイベントバッファへコピーしなければなりません。

保存されているimageに関する情報を取得するためにこのイベントを使うこともできて、進捗にステータスを提供することもできて、および/または、セーブ操作を中止することもできます。進捗ステータスモニターとしてこのイベントを使う方法に関する情報については、「CodecsSaveOptions.RetrieveDataFromImageとCodecsSaveImageEventArgs」を参照してください。

サンプル

それが保存されていて、このサンプルはimageの密度を二倍にします。

Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Drawing
Imports Leadtools.Svg

      
Public Sub SaveImageExample()
   Dim codecs As New RasterCodecs()
   Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
   Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "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)

   ' Instruct RasterCodecs to generate the saved scanlines data for us to manipulate
   codecs.Options.Save.RetrieveDataFromImage = True

   ' 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()
End Sub

Private Sub codecs_SaveImage(ByVal sender As Object, ByVal e As CodecsSaveImageEventArgs)
   ' This example works with images saved as 24-bit per pixel only
   Debug.Assert(e.Image.BitsPerPixel = 24)

   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.LastPage - e.FirstPage + 1)
      Console.WriteLine("Page percent: {0}, Total percent", e.PagePercent, e.TotalPercent)
   End If

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

   ' Get the scanlines from the image
   Dim scanlineLength As Integer = e.Image.BytesPerLine
   Dim scanline(scanlineLength - 1) As Byte

   ' Loop through all the scanlines in the data
   For y As Integer = 0 To e.Lines - 1
      ' 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 x As Integer = 0 To scanlineLength
         scanline(x) = CType(scanline(x) * 2, Byte)
      Next

      ' Copy it back to the event buffer
      e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength)
   Next
End Sub

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
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);

   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.LastPage - e.FirstPage + 1);
      Console.WriteLine("Page percent: {0}, Total percent", e.PagePercent, e.TotalPercent);
   }

   Console.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:\Users\Public\Documents\LEADTOOLS Images";
}
RasterCodecsExamples.prototype.SaveImageExample = function ( )
{
   Tools.SetLicense ( ) ;
   with ( Leadtools ) { with ( Leadtools.Codecs ) { 
      var codecs = new RasterCodecs();
      //required to fire events in JS
      Leadtools.RasterSupport.initialize();
      codecs.eventsDispatchMode = Leadtools.LeadEventsDispatchMode.useCoreDispatcher;

      var image ;
      var srcFileName = "Assets\\Image1.cmp";
      var destFileName = "Image1_SaveImage.cmp";

      // Load the source file (make sure to load as 24 bits/pixel)
      return Tools.AppInstallFolder().getFileAsync(srcFileName).then ( function ( loadFile ) {
         return codecs.loadAsync(LeadStreamFactory.create(loadFile), 24, CodecsLoadByteOrder.bgr, 1, 1)})
         .then ( function ( img ) {
            image = img ;

            // 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.addEventListener ( "saveimage", codecs_SaveImage1);

            // Save the image
            return Tools.AppLocalFolder().createFileAsync(destFileName)}).then ( function ( saveFile ) {
               return codecs.saveAsync(image, LeadStreamFactory.create(saveFile), RasterImageFormat.cmp, 24)})
         .then ( function ( ) {
            codecs.removeEventListener ("saveimage", codecs_SaveImage1);

            image.close();
            // Clean up
            codecs.close();
         });
   }
   }
}


function codecs_SaveImage1(e) {
   // This example works with images saved as 24-bit per pixel only
   console.assert(e.image.bitsPerPixel == 24, "e.image.bitsPerPixel == 24");

   if (e.row == 0) {
      // Show information about the image being saved
      console.info("Saving an image with ", e.image.bitsPerPixel, " bpp.");
      console.info("Page: ", e.page, " of ", e.lastPage - e.firstPage + 1);
      console.info("Page percent: ", e.pagePercent, "%, Total percent: ", e.totalPercent, "%");
   }

   console.info("Row: ", e.row, ", Lines ", e.lines);

   // Get the scanlines from the image
   var scanlineLength = e.image.bytesPerLine;
   var scanline = new Array(scanlineLength);

   // Loop through all the scanlines in the data
   for (var 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 (var x = 0; x < scanlineLength; x++) {
         scanline[x] *= 2;
      }

      // Copy it back to the event buffer
      e.buffer.setData(y * scanlineLength, scanline, 0, scanlineLength);
   }
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;

      
public async Task SaveImageExample()
{
   RasterCodecs codecs = new RasterCodecs();
   string srcFileName = @"Assets\Image1.cmp";
   string destFileName = @"Image1_SaveImage.cmp";

   // Load the source file (make sure to load as 24 bits/pixel)
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 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
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName);
   await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), 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
   Assert.IsTrue(e.Image.BitsPerPixel == 24);

   if (e.Row == 0)
   {
      // Show information about the image being saved
      Debug.WriteLine("Saving an image with {0} bpp.", e.Image.BitsPerPixel);
      Debug.WriteLine("Page: {0} of {1}", e.Page, e.LastPage - e.FirstPage + 1);
      Debug.WriteLine("Page percent: {0}, Total percent", e.PagePercent, e.TotalPercent);
   }

   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);
   }
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Examples;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;
using Leadtools.Windows.Media;

public void SaveImageExample(Stream inStreamCmp, Stream outStreamCmp)
{
   RasterCodecs codecs = new RasterCodecs();
   // Load the source file (make sure to load as 24 bits/pixel)
   RasterImage image = codecs.Load(inStreamCmp, 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, outStreamCmp, RasterImageFormat.Cmp, 24);

   codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);

   image.Dispose();
}

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;
   RasterNativeBuffer buffer = e.Buffer;

   // See if we need to re-allocate the managed buffer
   if (_saveImageScanLine == null || _saveImageScanLine.Length < bytesPerLine)
      _saveImageScanLine = new byte[bytesPerLine];

   long offset = 0;
   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
      buffer.SetData(offset, _saveImageScanLine, 0, bytesPerLine);

      // Move the pointer to the position in the buffer for the next row
      offset += bytesPerLine;
   }
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Windows.Media

Public Sub SaveImageExample(ByVal inStreamCmp As Stream, ByVal outStreamCmp As Stream)
   Dim codecs As RasterCodecs = New RasterCodecs()
   ' Load the source file (make sure to load as 24 bits/pixel)
   Dim image As RasterImage = codecs.Load(inStreamCmp, 24, CodecsLoadByteOrder.Bgr, 1, 1)

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

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

   RemoveHandler codecs.SaveImage, AddressOf codecs_SaveImage

   image.Dispose()
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 RasterNativeBuffer = 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 offset As Long = 0
   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) *= 2
         x += 1
      Loop

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

      ' Move the pointer to the position in the buffer for the next row
      offset += bytesPerLine
      y += 1
   Loop
End Sub
必要条件

ターゲットプラットホーム

参照

参照

RasterCodecsクラス
RasterCodecsメンバ