C# Fax – Retrieve the Image of a Received Fax with GetImageChunk

25 Nov 2016 This code sample is deprecated and in process of being removed. Check out our new code libraries and usage documentation on InterFAX @ Github.
Download the C# fax API zip file.

This example shows you how to retrieve the image of a received (inbound) C# fax.

The project files demonstrate how to use the inbound C# fax Web service’s GetList and GetImageChunk methods, as shown in the following snippet:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GetImageChunk
{
    class Program
    {
        static void Main(string[] args)
        {
            string username = "";
            string password = "";
            int chunkSize = 1000;
            int messageID = 271112264;
            byte[] imageChunk = null;
            bool validImageChunk = true;
            interfax.Inbound interfaxWebServiceInbound = new interfax.Inbound();
            System.IO.FileStream createStreamFaxImage = new System.IO.FileStream(string.Format("C:\\Docs\\Interfax\\{0}.tif", messageID), System.IO.FileMode.Create);
            // the folders in the path above must exist
            do
            {
                long resultStatus = interfaxWebServiceInbound.GetImageChunk(username, password, messageID, false, chunkSize, createStreamFaxImage.Position, ref imageChunk);
                if (resultStatus == 0) // status 0 means no errors
                {
                    createStreamFaxImage.Write(imageChunk, 0, imageChunk.Length);
                    // writes chunk to file
                }
                else // any other status is an error code
                {
                    validImageChunk = false;
                    Console.WriteLine(string.Format("Error Code {0}.", resultStatus)); 
                    break;
                }
            } while (imageChunk.Length == chunkSize); 
            // if it's smaller than the defined chunk size it means it's the last chunk
            createStreamFaxImage.Close();
            if (validImageChunk)
            {
                Console.WriteLine(string.Format("Fax image was saved to C:\\Docs\\Interfax\\{0}.tif", messageID)); // verbose
            }
            Console.ReadLine(); // use this to keep console open (waiting for keypress) after it is finished
        }
    }
}