This example uses a scan resolution of 200 dpi, but this can be changed. Also, the horizontal and vertical resolutions do not have to be set to the same value, use different values if necessary.
To scan and print, the steps are:
This example sets all parameters and automatically scans and saves. To invoke the user interface either set showUI to TRUE or use the TWscanimage function (commented out).
This is a stand alone function that will initialize the scanner each time it is called. If your app will scan more than one page then for faster scanning use
// Scan and print a document int scanTW_and_print(char far *fname) { imgdes image1; int rcode; int compression = 0; int showUI = FALSE; // Hide user interface TWAIN_CAP_DATA pixeltype; TWAIN_CAP_DATA brightness; TWAIN_CAP_DATA contrast; TWAIN_CAP_DATA resolution; TWAIN_CAP_DATA resunits; HWND hwnd; RECT scanrect; hwnd = GetActiveWindow(); if(rcode = TWdetecttwain(hwnd) != NO_ERROR) return (rcode); resunits.oneValue.val = TWUN_INCHES; resunits.conType = TWON_ONEVALUE; resolution.oneValue.val = 200; // 200 dpi resolution.conType = TWON_ONEVALUE; // For grayscale use: pixeltype.oneValue.val = TWPT_GRAY; pixeltype.oneValue.val = TWPT_BW; 1-bit b/w pixeltype.conType = TWON_ONEVALUE; brightness.oneValue.val = 0; // Range is usually -1000 to 1000 brightness.conType = TWON_ONEVALUE; contrast.oneValue.val = 0; // Range is usually -1000 to 1000 contrast.conType = TWON_ONEVALUE; // We're setting parameters here, but the scanner may choose to ignore our settings TWsetmeasureunit(hwnd, &resunits); // Set the device units TWsetxresolution(hwnd, &resolution); // Set the device resolution TWsetyresolution(hwnd, &resolution); // Set the device resolution TWsetbrightness(hwnd, &brightness); // Set brightness TWsetcontrast(hwnd, &contrast); // Set contrast TWsetpixeltype(hwnd, &pixeltype); // Set the pixel type scanrect.left = 250; // Scan bed coordinates in 1000th inch scanrect.top = 500; // 1/4-inch side, 1/2-inch top margins scanrect.right = 8000 + 250; // 8 inches wide scanrect.bottom = 10500 + 500; // 10 inches high // rcode = TWscanimage(hwnd, &image1); // For TWAIN user interface rcode = TWscanimageex(hwnd, &image1, &scanrect, showUI); // No user interface if(rcode == NO_ERROR) { // Successful scan, now print the image HDC hdcprn; hdcprn = GetPrinterDC(); // Function defined below if (hdcprn == NULL) rcode = PRT_ERR; else { RECT prtrect; prtrect.left = 0; prtrect.top = 0; prtrect.right = 7999; prtrect.bottom = 10499; // Simpliest form of the print function, no display progress dlg rcode = printimage(hwnd, hdcprn, 0, &image1, &prtrect, 0, 0); DeleteDC(hdcprn); } } freeimage(&image1); return(rcode); } // Get a Device Context for the printer HDC GetPrinterDC(void) { #define PRINTER_BUFFER_SIZE 80 char szPrinter[PRINTER_BUFFER_SIZE]; char *szDevice, *szDriver, *szOutput; GetProfileString("windows", "device", "", szPrinter, sizeof(szPrinter)); if((szDevice = strtok(szPrinter, ",")) != 0 && (szDriver = strtok(0, ",")) != 0 && (szOutput = strtok(0, ",")) != 0) return(CreateDC(szDriver, szDevice, szOutput, 0)); return(0); }
Victor Image Processing Library homepage | Victor Product Summary | more source code