EZTwain Pro - Printing Examples (C#)

Overview  Download  Licensing  Features  Code Wizard  Stories  User GuidePDF file

p>The printing functions either print from a file (EZTwain 3.20b69) or print an image represented in memory as a DIB. For example if you are using a function like TWAIN_Acquire, it returns the scanned image as a DIB. In C#, DIBs are represented as System.IntPtr's.

Given a single image as a DIB, you can print it with one of the single-image print functions:

    System.IntPtr hdib;
    ... <code that leaves a Dib in the variable 'hdib'>
    // Print hdib, prompting user with standard Print dialog:
    EZTwain.DIB_Print(hdib, "1-page Print Job");    
    // if you are now done using that image, release it:    
    EZTwain.DIB_Free(hdib);

The DIB_PrintNoPrompt function is similar, but immediately prints to the default printer with no prompting.

Normally the printing functions try to print each image at its implied physical size. For example, if you have an image 1700 pixels wide x 2200 pixels tall, marked as resolution=200 DPI, then its implied size is 8.5" by 11". If an image has resolution set to 0 DPI (which happens with a few scanners, most digital cameras, and some image files, particularly GIFs) then EZTwain pretends the image resolution is 72 DPI. You can tell EZTwain to scale down printed images so they always fit onto the printer page:

    EZTwain.DIB_SetPrintToFit(true);

To print the contents of a file, use TWAIN_PrintFile. Example:

    // print the file, jobname="File Copy", prompt the user with
    // a standard print dialog:
    EZTwain.PrintFile("c:\\docs\\multipage.tif", "File Copy", false);
    // If an error needs to be reported, report it:
    EZTwain.ReportLastError("Printing file copy");

If you have a set of images in memory, or need to do something else more complex than DIB_Print or TWAIN_PrintFile, you can create a print job one page at a time using DIB_PrintJobBegin, DIB_PrintPage and DIB_PrintJobEnd. Here's the basic code that's inside TWAIN_PrintFile:

    // count the pages in the file:
    int npages = EZTwain.PagesInFile("c:\\docs\\multipage.tif");
    // begin a print job, prompting for the printer:
    EZTwain.DIB_PrintJobBegin("File Copy", false);
    // loop through each image in file
    for (int i = 0; i < npages; i++) {
        // Load the ith page from the file into memory as a Dib:
        System.IntPtr hdib = EZTwain.LoadPage("c:\\docs\\multipage.tif", i);
        // In production code, please add error checking...
        if (hdib == 0) {
            break;
        }
        EZTwain.DIB_PrintPage(hdib);	// output page to print job
        EZTwain.DIB_Free(hdib);			// free image when done
    }
    // Close the print job:
    EZTwain.DIB_PrintJobEnd();