问题
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?
回答1:
Solved.
It turns out that WIA actually stores captured images as temporary files in the Users profile temp folder, so:
Path.GetTempPath()
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.
回答2:
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.
Marshal.ReleaseComObject(Image1);
I found this on CodePrjoct, link
来源:https://stackoverflow.com/questions/6121151/c-sharp-wia-image-scanning-fails-with-hresult-0x80070050