Click here to Skip to main content
15,888,461 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to scan all pages with one click as below: Pin
Member 1094325619-Nov-19 4:30
Member 1094325619-Nov-19 4:30 
GeneralRe: How to scan all pages with one click as below: Pin
Richard MacCutchan19-Nov-19 4:45
mveRichard MacCutchan19-Nov-19 4:45 
AnswerRe: How to scan all pages with one click as below: Pin
Pete O'Hanlon19-Nov-19 2:02
mvePete O'Hanlon19-Nov-19 2:02 
GeneralRe: How to scan all pages with one click as below: Pin
Member 1094325619-Nov-19 2:23
Member 1094325619-Nov-19 2:23 
GeneralRe: How to scan all pages with one click as below: Pin
Pete O'Hanlon19-Nov-19 2:37
mvePete O'Hanlon19-Nov-19 2:37 
GeneralRe: How to scan all pages with one click as below: Pin
Pete O'Hanlon19-Nov-19 2:38
mvePete O'Hanlon19-Nov-19 2:38 
GeneralRe: How to scan all pages with one click as below: Pin
Member 1094325619-Nov-19 2:50
Member 1094325619-Nov-19 2:50 
GeneralRe: How to scan all pages with one click as below: Pin
Pete O'Hanlon19-Nov-19 22:40
mvePete O'Hanlon19-Nov-19 22:40 
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.
C#
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) // Skip device If it is not a scanner
     {
         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)
		 {
		   // At this point, you know that you have not managed to scan
		   // any pages and you are receiving a paper out message, so
		   // you can inform the user that they need to load the device
		   MessageBox.Show("You cannot scan anything without loading the device");
		 }
		 // You cannot go any further so break out of the loop
		 break;
	  }
	  // By setting hasScannedPage to true, you won't show the message to the user
	  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"; // save the image in some path with filename.
	  n++;
	  bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg);
   }
 });


GeneralRe: How to scan all pages with one click as below: Pin
Member 1094325620-Nov-19 0:35
Member 1094325620-Nov-19 0:35 
GeneralRe: How to scan all pages with one click as below: Pin
Pete O'Hanlon20-Nov-19 0:49
mvePete O'Hanlon20-Nov-19 0:49 
GeneralRe: How to scan all pages with one click as below: Pin
Member 1094325620-Nov-19 4:17
Member 1094325620-Nov-19 4:17 
GeneralRe: How to scan all pages with one click as below: Pin
Pete O'Hanlon20-Nov-19 5:15
mvePete O'Hanlon20-Nov-19 5:15 
GeneralRe: How to scan all pages with one click as below: Pin
Member 1094325619-Nov-19 3:04
Member 1094325619-Nov-19 3:04 
QuestionInsert voice data down SQLServer Pin
Hoang Luc18-Nov-19 4:45
Hoang Luc18-Nov-19 4:45 
AnswerRe: Insert voice data down SQLServer Pin
OriginalGriff18-Nov-19 4:46
mveOriginalGriff18-Nov-19 4:46 
Questionadd code text to speech voices windows 7 ? Pin
Member 245846717-Nov-19 21:14
Member 245846717-Nov-19 21:14 
AnswerRe: add code text to speech voices windows 7 ? Pin
Pete O'Hanlon17-Nov-19 22:07
mvePete O'Hanlon17-Nov-19 22:07 
GeneralRe: add code text to speech voices windows 7 ? Pin
Member 245846720-Nov-19 15:45
Member 245846720-Nov-19 15:45 
GeneralRe: add code text to speech voices windows 7 ? Pin
Pete O'Hanlon20-Nov-19 21:47
mvePete O'Hanlon20-Nov-19 21:47 
GeneralRe: add code text to speech voices windows 7 ? Pin
Member 245846724-Nov-19 15:06
Member 245846724-Nov-19 15:06 
QuestionC# How to show my List of data in Pivot format Pin
Mou_kol16-Nov-19 8:17
Mou_kol16-Nov-19 8:17 
QuestionRe: C# How to show my List of data in Pivot format Pin
Eddy Vluggen16-Nov-19 9:30
professionalEddy Vluggen16-Nov-19 9:30 
AnswerRe: C# How to show my List of data in Pivot format Pin
Richard Deeming17-Nov-19 22:57
mveRichard Deeming17-Nov-19 22:57 
Questionprint statements within a method is not displaying on Rich text box in c# windows form Pin
Member 1465244915-Nov-19 18:35
Member 1465244915-Nov-19 18:35 
AnswerRe: print statements within a method is not displaying on Rich text box in c# windows form Pin
OriginalGriff15-Nov-19 19:57
mveOriginalGriff15-Nov-19 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.