我对以下代码有问题。我想通过单击Winforms C#应用程序中的按钮来扫描文档。
我使用Wia,Visual Studio和扫描仪Fujitsu N7100A与Windows 8一起工作。
但是该程序无法按预期运行。它似乎是在转移方法上分解的。
// Create a DeviceManager instance
var deviceManager = new DeviceManager();
// Create an empty variable to store the scanner instance
DeviceInfo firstScannerAvailable = null;
// Loop through the list of devices to choose the first available
AddLogs(deviceManager.DeviceInfos.Count.ToString(), filename);
foreach (DeviceInfo d in deviceManager.DeviceInfos)
{
if (d.Type == WiaDeviceType.ScannerDeviceType)
{
firstScannerAvailable = d;
}
}
// Connect to the first available scanner
var device = firstScannerAvailable.Connect();
// Select the scanner
var scannerItem = device.Items[0];
// Retrieve a image in JPEG format and store it into a variable
var imageFile = (ImageFile)scannerItem.Transfer(FormatID.wiaFormatPNG);
//Save the image in some path with filename
var path = @"C:Documentsscan.png";
if (File.Exists(path))
{
File.Delete(path);
}
// Save image !
imageFile.SaveFile(path);
我只需要删除日志文件中的行添加。
这更像是一个解决方法,因为我不知道您的扫描仪。
我会假设所有扫描仪都有一个驱动器,它们像我这样的扫描文档,所以我建议您阅读所有可用的驱动器循环循环,请访问驱动器和卷,然后读取文件,然后复制文档你想要
类似的东西:
foreach (var item in DriveInfo.GetDrives())
{
//VolumeLabel differs from a scanner to another
if (item.VolumeLabel == "Photo scan" && item.DriveType == DriveType.Removable)
{
foreach (var obj in Directory.GetFiles(item.Name))
{
File.Copy(obj, "[YOUR NEW PATH]");
break;
}
break;
}
}
最终与此扫描仪一起工作。我将与之合作。我不是说为什么这与吐温合作而不是与WIA一起使用,而是与现实一起使用。抱歉,浪费时间。谢谢您的答案。祝你有美好的一天。
我目前正在解决这个问题。N7100A驱动程序似乎将device
的Pages
属性设置为0
,这应该意味着连续扫描,但是转移方法无法处理此值。您必须将该属性设置为1
:
var pages = 1;
// Not all devices have this property, but Fujitsu N7100A has.
device.Properties["Pages"]?.set_Value(ref pages);
我认为问题在这里
var scannerItem = device.Items[0];
由于WIA索引不是基于零的,因此应该为1代替
var scannerItem = device.Items[1];