Victor Image Processing Library How-to Tips

Scan a Single Image

The Victor Library TWAIN Support Module is very easy to use. The simplest implementation is to scan a single image using the device's user interface . . .

     vicwin.imgdes cimage = new vicwin.imgdes(); // Image descriptor
     int rcode;                                  // Return code
        rcode = vicwin.TWscanimage(hWnd, ref cimage);

And to save the image in a TIFF file . . .

        rcode = vicwin.savetif("newimage.tif", ref cimage, 0)

In addition to being easy, the Victor Library control of the TWAIN device is also powerful. You can hide the user interface, control the scan conditions, and capture multiple images with an automatic document feeder.

Control a TWAIN Automatic Document Feed Scanner

This is a straight forward use of the scanner and file functions. The procedure is to scan each page of the document into an image buffer, then save it in a TIFF multipage image file. We hide the user interface and set the scanner conditions with our application:

      pixel data type: grayscale
      scanner resolution: 150 dpi horizontal and vertical
      scan area: 2 x 2 inch area

This example sets a scan resolution of 150 dpi, but this can be changed and the horizontal and vertical resolutions may use different values if necessary.

To scan and save, the steps are:

This example sets all parameters and automatically scans and saves.

This is a stand alone function that will initialize the scanner each time it is called. If your app will scan more than one time then for faster scanning use

at the top of the app and TWclose at the bottom. Calling the open/close functions only once can really speed up an app that invokes scanning multiple times.

C# Source Code

Scan, View, and Save - the C# Source Code

Requires Victor Image Processing Library for 32-bit Windows v 6 or higher.
       static PictureBox picbox;
       public static int scan_show_count_images(PictureBox pb)
           int swidth = 0, slength = 0, bpp = 0;
           int rcode;
           int hwnd;
           vicwin.imgdes cimage = new vicwin.imgdes();
           vicwin.RECT srect;
           vicwin.TWAIN_CAP_DATA pixel;
           vicwin.TWAIN_CAP_DATA res;
        // Save the value of pb for display of the pages below
           picbox = pb;
           pixel = new vicwin.TWAIN_CAP_DATA();
           res = new vicwin.TWAIN_CAP_DATA();
           hwnd = GetActiveWindow();
        // Set up automatic document feeder
           rcode = vicwin.TWsetfeeder(hwnd, 1);
           if (rcode == vicwin.NO_ERROR)
               srect.left = 0;
      = 0;
               srect.right = 2000;  // 8000 = 8 inches 
               srect.bottom = 2000; //10500 = 10.5 inches 
               pixel.conType = vicwin.TWON_ONEVALUE;
               pixel.oneValue.val = vicwin.TWPT_GRAY;    // Grayscale
               res.conType = vicwin.TWON_ONEVALUE;
               res.oneValue.val = 150;                   // 150 dpi
               rcode = vicwin.TWsetpixeltype(hwnd, ref pixel);
               rcode = vicwin.TWsetxresolution(hwnd, ref res);
               rcode = vicwin.TWsetyresolution(hwnd, ref res);
        Begin the scanning, each page scanned will be sent to the save_scannedimage function defined below
               rcode = vicwin.TWscancountimages(hwnd, ref  cimage, ref srect, 0, 100, save_scannedimage);
           return (rcode);
        // Function to call for every page scanned    
       static int pg;
       public static int save_scannedimage(ref vicwin.imgdes tempimg)
           int rcode = vicwin.NO_ERROR;
           string filename = "C:\\tifpages.tif";
           if (tempimg.bmh != 0)
               System.IntPtr bmhandle;
               int swidth = 0, slength = 0, bpp = 0;
        // Helper functions defined below
               vicwinex.calc_image_dimensions(ref tempimg, ref swidth, ref slength);
               bpp = vicwinex.getbpp(ref tempimg);
               if (tempimg.hBitmap == 0)
               { //  If it's a packed dib, use dibtoimage so we can display in picturebox
                   vicwin.imgdes tempimg2 = new vicwin.imgdes();
                   rcode = vicwin.allocimage(ref  tempimg2, swidth, slength, bpp);
                   if (rcode == vicwin.NO_ERROR)
                       rcode = vicwin.dibtoimage(tempimg.bmh, ref  tempimg2);
                       if (rcode == vicwin.NO_ERROR)
                           vicwin.freeimage(ref tempimg); // Release original scanned image
                           vicwin.copyimgdes(ref tempimg2, ref tempimg);  // Replace with new image
               if (tempimg.hBitmap != 0) //View the image in a picture box
                   bmhandle = new IntPtr(tempimg.hBitmap);
                   picbox.Image = Image.FromHbitmap(bmhandle);
                   picbox.Width = swidth;
                   picbox.Height = slength;
        // Save the image in a multipage TIFF file with LZW compression
               rcode = vicwin.savetifpage(ref filename, ref tempimg, vicwin.TIFLZW, pg++);
        // Free the image buffer
               vicwin.freeimage(ref tempimg);
           return (rcode);
//-------------------- C# helper functions from vic_comparator, functions.cs -------------------
      [DllImport("kernel32.dll",EntryPoint="RtlMoveMemory")] public static extern void RtlMoveMemory(int des, int src, int count);
      [DllImport("kernel32.dll",EntryPoint="RtlMoveMemory")] unsafe public static extern void Copybmh(vicwin.BITMAPINFOHEADER *des, int src, int count);
      unsafe public static int getbpp(ref vicwin.imgdes timage )
         vicwin.BITMAPINFOHEADER bmh;
         Copybmh(&bmh, timage.bmh, 40);
      public static void calc_area_dimensions(ref vicwin.imgdes timage, ref int width, ref int length)
         width = timage.endx - timage.stx + 1;
         length = timage.endy - timage.sty + 1;
      unsafe public static void calc_image_dimensions(ref vicwin.imgdes timage, ref int width, ref int length)
         vicwin.BITMAPINFOHEADER bmh;
         Copybmh(&bmh, timage.bmh, 40);
         width = bmh.biWidth;
         length = bmh.biHeight;
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code

Copyright © 2013 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.