C# WIA 图像扫描失败,并显示 HRESULT: 0x80070050
我希望有人遇到过这个问题 - 我正在尝试使用 WIA 从文档扫描仪捕获图像,但是在随机机器上尝试传输图像结果时 - WIA 报告“文件存在。 - HRESULT:0x80070050)”。在所有出现此问题的计算机上,该软件的初始使用都是成功的。
我能够成功连接到扫描仪,查询名称、制造商等。
我确定,如果我在备用用户帐户下运行代码(使用右键单击以管理员身份运行),我可以成功扫描图像。但是,在具有提升权限的同一用户帐户下运行代码会导致相同的错误。
注意:Item1.Transfer 上发生异常 - 所以到目前为止我还没有向 WIA 提供文件路径,因此这不可能是它所引用的文件。
WIA.DeviceManager DeviceManager1 = new WIA.DeviceManagerClass();
WIA.Device Scanner = DeviceManager1.DeviceInfos[i].Connect();
WIA.Item Item1 = null;
foreach (WIA.Item CurrentItem in Scanner.Items) {
Item1 = CurrentItem;
break;
}
WIA.ImageFile Image1 = new WIA.ImageFile();
//Attempt To Capture Scan
Image1 = (WIA.ImageFile)Item1.Transfer(WIA.FormatID.wiaFormatJPEG);
//Save To File
Image1.SaveFile(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + @"\scan" + DateTime.Now.Ticks + ".jpg");
最合乎逻辑的答案是 WIA 在图像捕获期间存储临时文件 - 我不知道 - 并且它无法覆盖以前的扫描。有谁知道这可能是哪里?
I'm hoping someone has come across this - I'm trying to capture images from a document scanner using WIA, however on random machines when attempting to transfer the image result - WIA reports "The file exists. - HRESULT: 0x80070050)". On All machines with this issue, initial use of the software was successful.
I am able to connect successfully to the scanner, query for Name, Manufacturer,etc.
I've determined that i can successfully scan an image, if i run the code under an alternative user account (Using right-click run as administrator). However, running the code under the same user account with elevated privledges results in the same error.
NOTE: Exception is happening on Item1.Transfer - so up until this point i haven't yet provided WIA with a file path, so this can't be the file it's referring to.
WIA.DeviceManager DeviceManager1 = new WIA.DeviceManagerClass();
WIA.Device Scanner = DeviceManager1.DeviceInfos[i].Connect();
WIA.Item Item1 = null;
foreach (WIA.Item CurrentItem in Scanner.Items) {
Item1 = CurrentItem;
break;
}
WIA.ImageFile Image1 = new WIA.ImageFile();
//Attempt To Capture Scan
Image1 = (WIA.ImageFile)Item1.Transfer(WIA.FormatID.wiaFormatJPEG);
//Save To File
Image1.SaveFile(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + @"\scan" + DateTime.Now.Ticks + ".jpg");
The most logical answer is that WIA is storing a temporary file during image capture - that i'm not aware of - and it is unable to overwrite a previous scan. Does anyone know where this might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了。
事实证明,WIA 实际上将捕获的图像作为临时文件存储在用户配置文件临时文件夹中,因此:
或 C:\Users\USER_PROFILE\AppData\Local\Temp\
文件以 imgXXXX.tmp 格式存储
在我们的例子中 - 原因这导致了一个问题,这个问题似乎在网络上的任何地方都没有记录,那就是我们每隔几秒钟轮询一次扫描仪 - 创建一个临时文件,因为只有 4x,在 WIA 之前最多可以有 65K 个临时文件会出错。
设置一个例程来清除此临时文件夹中的旧图像文件可以立即解决该问题。
Solved.
It turns out that WIA actually stores captured images as temporary files in the Users profile temp folder, so:
or C:\Users\USER_PROFILE\AppData\Local\Temp\
Files are stored in the format imgXXXX.tmp
In our case - the reason this caused an issue, which doesn't seem to be documented anywhere on the net, is that we polled the scanner every few seconds - creating a temp file, as there are only 4x's, there can be a max of 65K temp files before WIA will bug out.
Setting up a routine to clear out old image files from this temp folder immediately resolved the issue.
当我尝试使用 WIA 从相机设备读取图像时,我遇到了同样的问题。正确的解决方案是正确处理 WIA.ImageFile。这会清理 tmp 文件。
我在 CodePrjoct 上找到了这个,链接
I came across this same problem when trying to use WIA to read images off of a camera device. The proper solution is to dispose of the WIA.ImageFile properly. This cleans up the tmp file.
I found this on CodePrjoct, link