2

in my UWP application i am working on scan functionality. in this application user can scan the document through scanner by selecting flatbed or auto feeder.Now problem is when i am trying to scan it gives the en exception a Task was canceled.

please help..

thanks in advance. :)

have a great day... :)

private async void Btnscan_Click(object sender, RoutedEventArgs e)
    {

        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        folderPicker.FileTypeFilter.Add("*");

        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
//set the destination folder name for scan images.


        DeviceInformationDisplay selectedScanner = CmbScannerList.SelectedItem as DeviceInformationDisplay; // here i got the selected scanner.

        // scanner id is := "\\\\?\\ROOT#IMAGE#0000#{6bdd1fc6-810f-11d0-bec7-08002be2092f}"

        ScanToFolder(selectedScanner.id, folder);


    }

function Scan To folder

 public async void ScanToFolder(string deviceId, StorageFolder folder)
    {
        try
        {
 cancellationToken = new CancellationTokenSource();

            ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

            if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Flatbed))
            {
                var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Flatbed, folder).AsTask(cancellationToken.Token); // through an exception(A Task Was Canceled):(
Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);

            }

        }

        catch (Exception ex)
        {
            // here i got the exception.
        }

    }

Updated :

Now i am set the DeviceClass to ALL.

   private void StartWatcher()
    {
        resultCollection.Clear();
        DeviceWatcher deviceWatcher;

        deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.All); // set Image scanner to all. 
       deviceWatcherHelper.StartWatcher(deviceWatcher);
  }

After run the project in scanner list i got the all connected devices in which i got my scanner name This : when i am trying to pass this name it gives error in imagescanner System.Exception: 'Exception from HRESULT: 0x80210015' means device not found. Now i am chnage all to ImageScanner i got nothing in scanner list.

enter image description here

and in scanner HP application i got this name. and IT Scan Well :( in scanner list i don't got this name in my application. :( enter image description here

on my pc setting -> devices -> scanner and printers i got those name.

enter image description here

  • Where is the `cancellationToken` declared? Is it not actually canceled? – Martin Zikmund Apr 15 '19 at 09:23
  • please check the code now its updated :) –  Apr 15 '19 at 09:26
  • And how is it initialized? It cannot be `null` – Martin Zikmund Apr 15 '19 at 09:33
  • sorry i don't have any idea would please suggest some code :( –  Apr 15 '19 at 09:38
  • [link](https://code.msdn.microsoft.com/windowsapps/Scan-Runtime-API-Sample-5703b7a2/sourcecode?fileId=86722&pathId=1849918096) i am referring this site. and i am remove the unwanted code.(scenarios) –  Apr 15 '19 at 09:57
  • Aren't you calling the `CancelScanning` somewhere? What happens if you remove the `AsTask` part completely? – Martin Zikmund Apr 15 '19 at 11:11
  • it gives same error :( A task Was canceled . –  Apr 15 '19 at 11:22
  • Very weird, could you create a simple GitHub repo so I could try out your code directly? – Martin Zikmund Apr 15 '19 at 11:55
  • git hub project sample: [link](https://github.com/patilketan888/Scan-Image-in-UWP ) –  Apr 15 '19 at 13:30
  • have you seen the code ? is it work ? –  Apr 16 '19 at 04:53
  • 1
    I have tested your project on my machine and it does seem to work as expected and I am getting no `TaskCanceledException`. Maybe there is some problem with your scanner drivers? Are you able to test on some other machine or with some other scanner? – Martin Zikmund Apr 16 '19 at 07:44
  • Thank you for your valuable replay. :) i will check on another machine . i want to ask you the one question is their any TWAIN dll in nuGet package.which support scanning in uwp(xamarin Forms) ? or any third party library which is free :) if yes please share the link . and again thank you :) –  Apr 16 '19 at 07:58
  • @Martin : still i am getting the TaskCanceledException. i am also check the scan image from scanner application which is provided by scanner company and it works as expected. which scanner do you use ? –  Apr 17 '19 at 06:43
  • 1
    Hi, sorry for late reply, TWAIN.dll cannot be used directly in Xamarin.Forms and UWP, but you could use Desktop Extension (https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-2/) to launch full trust assembly and communicate with it. – Martin Zikmund Apr 17 '19 at 07:56
  • 1
    I have a Samsung printer which has a scanner – Martin Zikmund Apr 17 '19 at 07:56
  • @Martin : thank you for your reply. scanner is connected to your machine or network. because i am trying to access the scanner through network. please check this scenario on your machine. :) is their any access permission issue(Folder which we select for scan purpose) happen ? –  Apr 17 '19 at 09:42
  • @MartinZikmund Have you tried this scenario ? –  Apr 17 '19 at 10:51
  • Unfortunately I am unable to tell, my scanner does not support network connection. Maybe someone else could check. I would expect a different exception in case of permission issue – Martin Zikmund Apr 17 '19 at 17:32
  • @MartinZikmund : thank you for your reply. please check the updated question. :( my scanner is connected in network. it detect and access by HP scan application. not my application. –  Apr 18 '19 at 10:35
  • @MartinZikmund Finally it solved the issue. :) Thank you for your time and interest. :) actually its driver issue. after driver reinstall it solve the issue :) Once again thank you :) Have a great day :) –  Apr 18 '19 at 13:07
  • 1
    Great! I have rewritten the solution as an answer, please consider accepting it so that the question is resolved. Happy coding! – Martin Zikmund Apr 18 '19 at 14:09
  • @MartinZikmund sorry i am posting the question link do you have any idea about this in UWP [link](https://stackoverflow.com/q/56034495/11362349) –  May 16 '19 at 08:51

1 Answers1

0

Rewriting the resolution of the problem as an answer. I tested the code on my machine where it behaved correctly and argued the problem is most likely a driver issue. This has been confirmed by the OP and reinstall of the driver helped make the scanning work again.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91