设置 TWAIN 扫描的分辨率

Setting resolution for TWAIN scanning

我在 WinForms 中创建了一个应用程序,它可以列出所有的 twain 和 wia 扫描仪,并使用该列表中选择的设备进行扫描。

每当我在不使用 TWAIN 用户界面的情况下使用 TWAIN 进行扫描时,我得到的图像质量很差。

我的代码基于此处找到的包装器:https://www.codeproject.com/Articles/1376/NET-TWAIN-image-scanner?msg=1097187#xx1097187xx and the specifications at http://www.twain.org/wp-content/uploads/2016/03/TWAIN-2.2-Spec.pdf

我尝试以 dpi 为单位设置分辨率,但没有任何区别。设置上限后 rc 总是说 "failure":

TwFix32 f32 = new TwFix32();
f32.FromFloat(300);//value of DPI 

// Set X resolution.
TwCapability capX = new TwCapability(TwCap.XResolution, f32.Whole);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capX);

// Set Y resolution.
TwCapability capY = new TwCapability(TwCap.YResolution, f32.Whole);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capY);

它通过创建一个新的构造函数起作用:

public TwCapability(TwCap cap, short sval, TwType twtype)
{
    Cap = (short)cap;
    ConType = (short)TwOn.One;
    Handle = Twain.GlobalAlloc(0x42, 6);
    IntPtr pv = Twain.GlobalLock(Handle);
    Marshal.WriteInt16(pv, 0, (short)TwType.Int16);
    Marshal.WriteInt32(pv, 2, (int)sval);
    Marshal.WriteInt16(pv, 0, (short)twtype);
    Twain.GlobalUnlock(Handle);
}


TwCapability capResx = new TwCapability(TwCap.ICAP_XRESOLUTION, 300, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResx);


TwCapability capResy = new TwCapability(TwCap.ICAP_YRESOLUTION, 300, TwType.Fix32);
rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capResy);