|
|
I have the code below for scanning all papers. It works but the problem is when all papers will finish, it shows the error:
Exception User-Unhandeled System.runtime.InterpService.COMEException: Exception From HRESULT:0*80210003
which belongs to this line:
var imgFile = (ImageFile)ScanerItem.Transfer();
Does anyone know how to fix it?
Thanks
How can i fix it? Here is the code:
bool continueScanning2 = true;
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
while (continueScanning2)
{
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect();
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
string name2 = Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
var Path = @"C:\Users\...\Desktop\test\" + name2;
pictureBox1.ImageLocation = Path;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
|
Thanks for the reply. yes but i don't want to see this error code after the scanning process will be finished.
i want to see this warning whenever i click on the "scan button" and there is no paper in scanner device.
|
|
|
|
|
Then you will have to probe the device first to see if there is paper loaded. The scanner can only report its current status, it is up to you to check the status at the correct time.
|
|
|
|
|
Could you please write this condition that how should i check it?
Thanks
|
|
|
|
|
What do you mean? All you need is a simple if clause.
|
|
|
|
|
i wrote a while loop to check it until the device is loaded by it's not working.
Here are my code:
try
{
object[] items = await Task.Run<object[]>(() =>
{
var deviceManager = new DeviceManager();
List<object> result = new List<object>();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
result.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
return result.ToArray();
});
foreach (var item in items)
{
lstListOfScanner.Items.Add(item);
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
int n = 1;
bool continueRunning2 = true;
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect();
while (AvailableScanner != null)
{
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
What does "not working" mean?
|
|
|
|
|
It works still like before.
it scanns all pages and when they finished, it shows me the error of loading paper
|
|
|
|
|
Yes, because the scanner is telling you that there is no more paper in the feeder. Why do you think this is an error?
|
|
|
|
|
Becuase i need it to works like it:
1) if there is no paper on the device and i click on "Scan" button, it shows me the warnning that i should load paper
but
2) if the scanner was already loaded, and i click on "Scan" button, it starts to scan all pages and at the end nothing happens! (or i added a meesagebox("Finish") but not this warning)
|
|
|
|
|
When you catch the exception check to see which error code has been returned. If it is "No paper loaded" then post a message and try again.
|
|
|
|
|
if i write the code below, it shows me the message box when ever the device is empty!
catch (COMException ex)
{
uint errorCode = (uint)ex.ErrorCode;
if (errorCode == 0x80210003)
{
MessageBox.Show("Load the scaner device!");
}
else
{
MessageBox.Show(ex.Message);
}
but i do NOT want to see this message when the scan proccess will be finished.
(i want to see nothing when all papers scanned. just see a message if at the beginning i click and the device was empty)
Could you pls correct it?
|
|
|
|
|
How does the application know that all the pages have been scanned?
|
|
|
|
|
According to the Windows Imaging Acquisition codes, that means the paper is empty. If you reach this point, it suggests that you have finished scanning. Something you need to consider; your while loop will never end until you hit an exception because you aren't setting it to false anywhere. Also, your logic looks very odd. You're evaluating the scanners each time through the while loop, and connecting again and again. You should move the for loop and the AvailableScanner.Connect outside the while loop; something like this perhaps:
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner == null)
{
return;
}
var device = AvailableScanner.Connect();
while (continueRunning2)
{
var ScanerItem = device.Items[1];
...
}
modified 19-Nov-19 8:08am.
|
|
|
|
|
Thanks but it didn't work. now the problem is with if statement which you added.
according to your suggetion, i changed it like below:
could you please say where is the problem?
int n = 1;
bool continueRunning2 = true;
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner != null)
{
return;
}
var device = AvailableScanner.Connect();
while (continueRunning2)
{
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Your version doesn't match mine. Check what's in my answer, I edited it as soon as I'd posted it because I noticed the problem with the if condition. The if statement says:
if (AvailableScanner == null)
|
|
|
|
|
A point to note. You still have the problem that you aren't actually setting your boolean condition to false anywhere so you're still going to throw the exception when the scanner finishes scanning documents.
|
|
|
|
|
i changed it but it still has the same problem as before (it shows the error of loading paper).
Below is my complete code, could you please check/correct it?
try
{
object[] items = await Task.Run<object[]>(() =>
{
var deviceManager = new DeviceManager();
List<object> result = new List<object>();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
result.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
return result.ToArray();
});
foreach (var item in items)
{
lstListOfScanner.Items.Add(item);
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
int n = 1;
bool continueRunning2 = true;
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner == null)
{
return;
}
var device = AvailableScanner.Connect();
while (continueRunning2)
{
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Your problem is that you are making the scope of your try/catch for the COMException too broad. As you only want to show the message if you haven't managed to successfully scan a single page, then all you really need to do is track whether or not you have scanned a page and show the message if you haven't. This could be done simply by using a boolean to track whether or not the page has loaded.
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
if (AvailableScanner == null)
{
return;
}
var device = AvailableScanner.Connect();
bool hasScannedPage = false;
while (continueRunning2)
{
var ScanerItem = device.Items[1];
ImageFile imgFile = null;
try
{
imgFile = (ImageFile)ScanerItem.Transfer();
}
catch (COMException ex)
{
uint errorCode = (uint)ex.ErrorCode;
if (errorCode == 0x80210003 && !hasScannedPage)
{
MessageBox.Show("You cannot scan anything without loading the device");
}
break;
}
hasScannedPage = true;
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\Users\...\Desktop\test\" + n + ".jpg";
n++;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
});
|
|
|
|
|
Perfect! Thanks!
It was what i need!
|
|
|
|
|
You're welcome. I'm glad it's working now.
|
|
|
|
|
Dear Pete,
I have also another short question. i will appreciate it if you tell me how to fix this:
1)I have a button to load a photo into a picturebox and extract the code into a text field.(It works well)
2)There is also another button for deleting that image which i mentioned. But when i click on delete button it couldnt delete the image because it said: "Its being used by another process"
I dont know how to delete this image from directory after i saw the qrcode in textbox.
Here are my code:
Thanks
private void button2_Click(object sender, EventArgs e)
{
var Path = @"D:\1.jpg";
pictureBox1.Image = Image.FromFile(Path);
MessagingToolkit.QRCode.Codec.QRCodeDecoder decoder = new MessagingToolkit.QRCode.Codec.QRCodeDecoder();
textBox2.Text = decoder.Decode(new QRCodeBitmapImage(pictureBox1.Image as Bitmap));
}
private void button3_Click(object sender, EventArgs e)
{
if (File.Exists(@"D:\1.jpg"))
{
System.IO.File.Delete(@"D:\1.jpg");
}
}
|
|
|
|
|
The thing that is locking the image is the picturebox (specifically, the Image). You'll have to unload that first.
|
|
|
|