VintaSoft Imaging .NET SDK v8.7
Vintasoft.Imaging Namespace / ImageCollection Class / SaveAsync Method / SaveAsync(Stream,EncoderBase) Method
Stream where the image collection should be saved.
Encoder to the save images.
Example



In This Topic
    SaveAsync(Stream,EncoderBase) Method
    In This Topic
    Saves asynchronously all images from this collection to specified stream using the specified Vintasoft.Imaging.Codecs.Encoders.EncoderBase.
    Syntax
    'Declaration
     
    Public Overloads Sub SaveAsync( _
       ByVal stream As Stream, _
       ByVal encoder As EncoderBase _
    ) 
    public void SaveAsync( 
       Stream stream,
       EncoderBase encoder
    )
    public: void SaveAsync( 
       Stream* stream,
       EncoderBase* encoder
    ) 
    public:
    void SaveAsync( 
       Stream^ stream,
       EncoderBase^ encoder
    ) 

    Parameters

    stream
    Stream where the image collection should be saved.
    encoder
    Encoder to the save images.
    Exceptions
    ExceptionDescription
    Thrown if stream is null.
    Thrown if image collection does not contain images or another saving process is executing at this moment.
    Thrown if error occurs at saving the image.
    Remarks
    This method works asynchronously, i.e. separate thread is created for saving images.

    Supported image formats: JBIG2, PDF, TIFF.

    This method
    • saves images to the stream specified by stream using encoder
    • stream is overwritten if it is not empty
    • saving process can be canceled, saving of images can be suppressed
    • sources of images in the image collection is not changed, i.e. images[i].SourceInfo.Stream is not changed
    if
    • stream != images[0]..SourceInfo.Stream
    • SaveAndSwitchSource is false
    • encoder.CreateNewFile is true

    This method
    • adds images to existing stream specified by stream using encoder
    • saving process can be canceled, saving of images can be suppressed
    • sources of images in the image collection are not changed, i.e. images[i].SourceInfo.Stream is not changed
    if
    • stream != images[0]..SourceInfo.Stream
    • SaveAndSwitchSource is false
    • encoder.CreateNewFile is false

    This method
    • saves images to the new stream specified by stream using encoder
    • stream is overwritten if it is not empty
    • saving process cannot be canceled, saving of images cannot be suppressed
    • sources of images in the image collection are changed, i.e. images[i].SourceInfo.Stream is changed to stream
    if
    This method
    • changes images in the source stream if some images of source file are changed in the image collection
    • removes images from the source stream if some images of source file are removed from the image collection
    • adds images to the source stream if some images of NOT from the source file are added to the image collection
    • reindex images in the source stream if images are reindexed in the image collection
    • saving process cannot be canceled, saving of images cannot be suppressed
    • sources of images in the image collection is changed, i.e. images[i].SourceInfo.Stream is changed to stream
    if
    This method
    • throw an exception
    if
    ImageSaving event will occur before saving of each image. In this event you can get information about the image, suppress image saving or cancel image saving process.
    ImageSaved event will occur after saving of each image. In this event you can get information about progress of images saving process or cancel image saving process.
    Example
    This example illustrates how to save the image collection asynchronously.
    ''' <summary>
    ''' Indicates whether image collection saving process is finished.
    ''' </summary>
    Private _isImagesSavingProcessFinished As Boolean = False
                
                
                
    ''' <summary>
    ''' Example of ImageCollection.SaveAsync.
    ''' </summary>
    Public Sub ImageCollectionSaveAsyncExample()
    	' create image collection
    	Dim images As New Vintasoft.Imaging.ImageCollection()
                
    	' add several images into collection
    	' [ do not forget to set your image file paths here! ]
    	images.Add("testImage1.jpg")
    	images.Add("testImage2.bmp")
    	images.Add("testImage3.png")
                
    	' create FileStream for saving
    	Dim saveStream As New System.IO.FileStream("new-file-name.tif", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)
                
    	' create the TiffEncoder instance
    	Dim multipageEncoder As Vintasoft.Imaging.Codecs.Encoders.MultipageEncoderBase = New Vintasoft.Imaging.Codecs.Encoders.TiffEncoder()
                
    	' subscribe to the image collection events
    	AddHandler images.ImageCollectionSaving, New System.EventHandler(Of Vintasoft.Imaging.ImageCollectionSavingEventArgs)(AddressOf images_ImageCollectionSaving)
    	AddHandler images.ImageCollectionSaved, New System.EventHandler(AddressOf images_ImageCollectionSaved)
    	AddHandler images.ImageCollectionSavingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf images_ImageCollectionSavingProgress)
    	AddHandler images.ImageCollectionSavingFinished, New System.EventHandler(AddressOf images_ImageCollectionSavingFinished)
    	AddHandler images.ImageSavingException, New System.EventHandler(Of Vintasoft.Imaging.ExceptionEventArgs)(AddressOf images_ImageSavingException)
                
    	_isImagesSavingProcessFinished = False
    	' save images to file and switch source
    	multipageEncoder.SaveAndSwitchSource = True
                
    	' save image collection asynchronously
    	images.SaveAsync(saveStream, multipageEncoder)
                
    	' wait while image saving process is finished
    	While Not _isImagesSavingProcessFinished
    		System.Threading.Thread.Sleep(1)
    	End While
    	
                
                
    	' close FileStream
    	saveStream.Close()
    End Sub
                
    ''' <summary>
    ''' Image collection saving process is started.
    ''' </summary>
    Private Sub images_ImageCollectionSaving(sender As Object, e As Vintasoft.Imaging.ImageCollectionSavingEventArgs)
    	System.Console.Write("Progress: ")
    End Sub
                
    ''' <summary>
    ''' Image collection saving process is in progress.
    ''' </summary>
    Private Sub images_ImageCollectionSavingProgress(sender As Object, e As Vintasoft.Imaging.ProgressEventArgs)
    	System.Console.Write(String.Format("{0}% ", e.Progress))
    End Sub
                
    ''' <summary>
    ''' Image collection is saved successfully.
    ''' </summary>
    Private Sub images_ImageCollectionSaved(sender As Object, e As System.EventArgs)
    	System.Console.WriteLine("Images are saved successfully.")
    End Sub
                
    ''' <summary>
    ''' Image collection is NOT saved.
    ''' </summary>
    Private Sub images_ImageSavingException(sender As Object, e As Vintasoft.Imaging.ExceptionEventArgs)
    	System.Console.WriteLine(e.Exception.Message)
    	System.Console.WriteLine("Images are NOT saved.")
    End Sub
                
    ''' <summary>
    ''' Image collection saving process is finished.
    ''' </summary>
    Private Sub images_ImageCollectionSavingFinished(sender As Object, e As System.EventArgs)
    	Dim images As Vintasoft.Imaging.ImageCollection = DirectCast(sender, Vintasoft.Imaging.ImageCollection)
                
    	' unsubscribe from image collection events
    	RemoveHandler images.ImageCollectionSaving, AddressOf images_ImageCollectionSaving
    	RemoveHandler images.ImageCollectionSavingProgress, AddressOf images_ImageCollectionSavingProgress
    	RemoveHandler images.ImageCollectionSaved, AddressOf images_ImageCollectionSaved
    	RemoveHandler images.ImageSavingException, AddressOf images_ImageSavingException
    	RemoveHandler images.ImageCollectionSavingFinished, AddressOf images_ImageCollectionSavingFinished
                
    	' indicate that image collection saving process is finished
    	_isImagesSavingProcessFinished = True
    End Sub
    /// <summary>
    /// Indicates whether image collection saving process is finished.
    /// </summary>
    bool _isImagesSavingProcessFinished = false;
                
                
                
    /// <summary>
    /// Example of ImageCollection.SaveAsync.
    /// </summary>
    public void ImageCollectionSaveAsyncExample()
    {
        // create image collection
        Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection();
                
        // add several images into collection
        // [ do not forget to set your image file paths here! ]
        images.Add("testImage1.jpg");
        images.Add("testImage2.bmp");
        images.Add("testImage3.png");
                
        // create FileStream for saving
        System.IO.FileStream saveStream = new System.IO.FileStream(
            "new-file-name.tif", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
                
        // create the TiffEncoder instance
        Vintasoft.Imaging.Codecs.Encoders.MultipageEncoderBase multipageEncoder =
            new Vintasoft.Imaging.Codecs.Encoders.TiffEncoder();
                
        // subscribe to the image collection events
        images.ImageCollectionSaving +=
            new System.EventHandler<Vintasoft.Imaging.ImageCollectionSavingEventArgs>(images_ImageCollectionSaving);
        images.ImageCollectionSaved += new System.EventHandler(images_ImageCollectionSaved);
        images.ImageCollectionSavingProgress += new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(
            images_ImageCollectionSavingProgress);
        images.ImageCollectionSavingFinished += new System.EventHandler(images_ImageCollectionSavingFinished);
        images.ImageSavingException += new System.EventHandler<Vintasoft.Imaging.ExceptionEventArgs>(
            images_ImageSavingException);
                
        _isImagesSavingProcessFinished = false;
        // save images to file and switch source
        multipageEncoder.SaveAndSwitchSource = true;
                
        // save image collection asynchronously
        images.SaveAsync(saveStream, multipageEncoder);
                
        // wait while image saving process is finished
        while (!_isImagesSavingProcessFinished)
        {
            System.Threading.Thread.Sleep(1);
        };
                
        // close FileStream
        saveStream.Close();
    }
                
    /// <summary>
    /// Image collection saving process is started.
    /// </summary>
    private void images_ImageCollectionSaving(object sender, Vintasoft.Imaging.ImageCollectionSavingEventArgs e)
    {
        System.Console.Write("Progress: ");
    }
                
    /// <summary>
    /// Image collection saving process is in progress.
    /// </summary>
    private void images_ImageCollectionSavingProgress(object sender, Vintasoft.Imaging.ProgressEventArgs e)
    {
        System.Console.Write(string.Format("{0}% ", e.Progress));
    }
                
    /// <summary>
    /// Image collection is saved successfully.
    /// </summary>
    private void images_ImageCollectionSaved(object sender, System.EventArgs e)
    {
        System.Console.WriteLine("Images are saved successfully.");
    }
                
    /// <summary>
    /// Image collection is NOT saved.
    /// </summary>
    private void images_ImageSavingException(object sender, Vintasoft.Imaging.ExceptionEventArgs e)
    {
        System.Console.WriteLine(e.Exception.Message);
        System.Console.WriteLine("Images are NOT saved.");
    }
                
    /// <summary>
    /// Image collection saving process is finished.
    /// </summary>
    private void images_ImageCollectionSavingFinished(object sender, System.EventArgs e)
    {
        Vintasoft.Imaging.ImageCollection images = (Vintasoft.Imaging.ImageCollection)sender;
                
        // unsubscribe from image collection events
        images.ImageCollectionSaving -= images_ImageCollectionSaving;
        images.ImageCollectionSavingProgress -= images_ImageCollectionSavingProgress;
        images.ImageCollectionSaved -= images_ImageCollectionSaved;
        images.ImageSavingException -= images_ImageSavingException;
        images.ImageCollectionSavingFinished -= images_ImageCollectionSavingFinished;
                
        // indicate that image collection saving process is finished
        _isImagesSavingProcessFinished = true;
    }
    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also