GetImageStream

GetImageStream()

Returns the image stream.

public Stream GetImageStream()

Return Value

A stream with the image.

Examples

The following example shows how to save images to files:

// Create an instance of Parser class
using (Parser parser = new Parser(filePath))
{
    // Extract images from document
    IEnumerable<PageImageArea> images = parser.GetImages();
    
    // Check if images extraction is supported
    if (images == null)
    {
        Console.WriteLine("Page images extraction isn't supported");
        return;
    }

    // Iterate over images
    foreach (PageImageArea image in images)
    {
        // Open the image stream
        using (Stream imageStream = image.GetImageStream())
        {
            // Create the file to save image
            using (Stream destStream = File.Create(Guid.NewGuid().ToString() + image.FileType.Extension))
            {
                byte[] buffer = new byte[4096];
                int readed = 0;

                do
                {
                    // Read data from the image stream
                    readed = imageStream.Read(buffer, 0, buffer.Length);

                    if (readed > 0)
                    {
                        // Write data to the file stream
                        destStream.Write(buffer, 0, readed);
                    }
                }
                while (readed > 0);
            }
        }
    }
}

See Also


GetImageStream(ImageOptions)

Returns the image stream in a different format.

public Stream GetImageStream(ImageOptions options)
Parameter Type Description
options ImageOptions The options which are used to extract the image.

Return Value

A stream with the image.

Examples

The following example shows how to save images to files in a different format:

// Create an instance of Parser class
using (Parser parser = new Parser(filePath))
{
    // Extract images from document
    IEnumerable<PageImageArea> images = parser.GetImages();
    
    // Check if images extraction is supported
    if (images == null)
    {
        Console.WriteLine("Page images extraction isn't supported");
        return;
    }

    // Create the options to save images in PNG format
    ImageOptions options = new ImageOptions(ImageFormat.Png);
    
    // Iterate over images
    foreach (PageImageArea image in images)
    {
        // Open the image stream
        using (Stream imageStream = image.GetImageStream(options))
        {
            // Create the file to save image
            using (Stream destStream = File.Create(Guid.NewGuid().ToString() + ".png"))
            {
                byte[] buffer = new byte[4096];
                int readed = 0;

                do
                {
                    // Read data from the image stream
                    readed = imageStream.Read(buffer, 0, buffer.Length);

                    if (readed > 0)
                    {
                        // Write data to the file stream
                        destStream.Write(buffer, 0, readed);
                    }
                }
                while (readed > 0);
            }
        }
    }
}

See Also