我有一个支持使用WIA扫描的C#(WinForm)程序。我正在尝试在扫描一个或多个文档之前设置设备属性。主要是我想设置扫描仪的纸张尺寸。以下是代码片段:
foreach (Property property in selectedDevice.Properties)
{
//WiaProperties.WiaDpsHorizontalBedSize is my constant
if (property.PropertyID == WiaProperties.WiaDpsHorizontalBedSize)
{
//Set property value here...
}
}
我找到了水平床尺寸属性,但问题是如何将其设置为值?有一个属性的set_Value属性,但似乎需要引用一个结果对象。所以我不知道如何在设备上设置属性?
答案 0 :(得分:10)
我正在开展扫描项目,WIA的例子也很少。此代码是您要设置的床大小,DPI等。请查看SetProperty方法,并提供有关如何处理set_Value的示例。
class Scan
{
// Scanner only device properties (DPS)
public const int WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const int WIA_DIP_FIRST = 2;
public const int WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const int WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const int WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const int WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
public const int WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
public const int FEEDER = 1;
public const int FLATBED = 2;
public const int DUPLEX = 4;
public const int FEED_READY = 1;
WIA.CommonDialog _dialog = new WIA.CommonDialog();
WIA.Device _scanner;
public void ADFScan()
{
_dialog = new CommonDialogClass();
_scanner = _dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);
foreach (Property item in _scanner.Items[1].Properties)
{
switch (item.PropertyID)
{
case 6146: //4 is Black-white,gray is 2, color 1
SetProperty(item, 2);
break;
case 6147: //dots per inch/horizontal
SetProperty(item, 100);
break;
case 6148: //dots per inch/vertical
SetProperty(item, 100);
break;
case 6149: //x point where to start scan
SetProperty(item, 0);
break;
case 6150: //y-point where to start scan
SetProperty(item, 0);
break;
case 6151: //horizontal exent
SetProperty(item, (int)(8.5 * 100));
break;
case 6152: //vertical extent
SetProperty(item, 11 * 100);
break;
}
}
ImageFile image = (ImageFile)_scanner.Items[1].Transfer(FormatID.wiaFormatPNG);
System.IO.File.Delete("tmp.png");
image.SaveFile("tmp.png");
}
private void SetProperty(Property property, int value)
{
IProperty x = (IProperty)property;
Object val = value;
x.set_Value(ref val);
}
public void test()
{
bool WantsToScan = true;
while (WantsToScan) ScanAndSaveOnePage();
}
static void Main(string[] args)
{
new Scan().test();
}
}