Search code examples
c++twainimage-scanner

How do I set the DPI of a scan using TWAIN in C++


I am using TWAIN in C++ and I am trying to set the DPI manually so that a user is not displayed with the scan dialog but instead the page just scans with set defaults and is stored for them. I need to set the DPI manually but I can not seem to get it to work. I have tried setting the capability using the ICAP_XRESOLUTION and the ICAP_YRESOLUTION. When I look at the image's info though it always shows the same resolution no matter what I set it to using the ICAPs. Is there another way to set the resolution of a scanned in image or is there just an additional step that needs to be done that I can not find in the documentation anywhere?

Thanks


Solution

  • I use ICAP_XRESOLUTION and the ICAP_YRESOLUTION to set the scan resolution for a scanner, and it works at least for a number of HP scanners.

    Code snipset:

    float x_res = 1200;
    cap.Cap = ICAP_XRESOLUTION;
    cap.ConType = TWON_ONEVALUE;
    cap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE));
    if(cap.hContainer)
    {
        val_p = (pTW_ONEVALUE)GlobalLock(cap.hContainer);
        val_p->ItemType = TWTY_FIX32;
        TW_FIX32 fix32_val = FloatToFIX32(x_res);
        val_p->Item = *((pTW_INT32) &fix32_val);
        GlobalUnlock(cap.hContainer);
        ret_code = SetCapability(cap);
        GlobalFree(cap.hContainer);
    }
    
    TW_FIX32 FloatToFIX32(float i_float)
    {
        TW_FIX32 Fix32_value;
        TW_INT32 value = (TW_INT32) (i_float * 65536.0 + 0.5);
        Fix32_value.Whole = LOWORD(value >> 16);
        Fix32_value.Frac = LOWORD(value & 0x0000ffffL);
        return Fix32_value;
    }
    

    The value should be of type TW_FIX32 which is a floating point format defined by twain (strange but true).

    I hope it works for you!