Printing images

Developers can print images by using a small series of functions.  First, the PrepareToPrint function must be called to initialize the print settings.  Next, for each image that should be printed, the developer must load the image and then call the PrintDIBPage function.  After calling the PrintDIBPage function, the developer must call the Microsft API’s GlobalUnlock and GlobalFree functions for the image if they no longer need the image.  Once all of the images have been printed, the developer must call the EndPrint function.  The psuedocode below demonstrates how several images can be printed:

public static bool PrintTiffFile(string sourceFile, string printer, int copies = 1)

{

    long devmode;

    long hDib;

    int res;

    int[] sRect;

    sRect = new int[4];

    sRect[0] = 1;

    sRect[1] = 1;

    sRect[2] = 1;

    sRect[3] = 1;

    short count;

 

    try

    {

        res = -1;

        count = BiTiff.GetNumberOfImagesInTiffFile(sourceFile);

        devmode = BiPrint.BiPrintSetup(printer, false);

        int rv = BiPrint.PrepareToPrintEx(devmode, printer, 0, sourceFile, count,

            (short)copies, false, false, false, true, false, false, false, true, false);

 

        if (rv != 0)

            throw new Exception("Tiff printing failed. Error: " + rv.ToString());

 

        for (short i = 0; i < count; i++)

        {

            hDib = BiDIB.LoadImageIntoDIB(sourceFile, i);

            res = BiPrint.PrintDIBPage(hDib, ref sRect[0], i, 0, 0);

            BiTiff.DropDIB(hDib);

 

            if (res != 0)

            {

                BiPrint.EndPrint();

                throw new Exception("Tiff printing failed. Error: " + res.ToString());

            }

        }

        BiPrint.EndPrint();

 

        return true;

    }

    catch (Exception ex)

    {

        return false;

    }

}