Пример #1
0
        public override List<Image> Scan()
        {
            Console.WriteLine("Scan Twain");
            lesImagesNum.Clear();
            // can use the utility method to create appId or make one yourself
            var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly());

            // new it up and handle events
            session = new TwainSession(appId);

            session.TransferReady += session_TransferReady;
            session.DataTransferred += session_DataTransferred;
            session.SourceDisabled += session_SourceDisable;

            // finally open it
            session.Open();
            var t = session.GetEnumerator();

            Console.WriteLine(t.ToString());
            while (t.MoveNext())
            {
                Console.WriteLine(t.ConvertToString());
            }

            // choose and open the first source found
            // note that TwainSession implements IEnumerable<DataSource> so we can use this extension method.
            IEnumerable<DataSource> lesSources = session.GetSources();
            Console.WriteLine("Nb Source : " + session.Count());
            myDS = session.FirstOrDefault();
            myDS.Open();

            // All low-level triplet operations are defined through these properties.
            // If the operation you want is not available, that most likely means
            // it's not for consumer use or it's been abstracted away with an equivalent API in this lib.
            //myDS.DGControl;
            //myDS.DGImage.;

            // The wrapper has many methods that corresponds to the TWAIN capability triplet msgs like
            // GetValues(), GetCurrent(), GetDefault(), SetValue(), etc.
            // (see TWAIN spec for reference)

            // This example sets pixel type of scanned image to BW and
            // IPixelType is the wrapper property on the data source.
            // The name of the wrapper property is the same as the CapabilityId enum.
            PixelType typeCouleur = PixelType.Gray;

            if (myDS.Capabilities.ICapPixelType.CanSet &&
                myDS.Capabilities.ICapPixelType.GetValues().Contains(typeCouleur))
            {
                myDS.Capabilities.ICapPixelType.SetValue(typeCouleur);
            }

            //Même chose avec le DPI
            TWFix32 DPI = MainWindow.numerisationDPI;

            if(myDS.Capabilities.ICapXResolution.CanSet &&
                myDS.Capabilities.ICapXResolution.GetValues().Contains(DPI))
            {
                myDS.Capabilities.ICapXResolution.SetValue(DPI);
            }

            if (myDS.Capabilities.ICapYResolution.CanSet &&
                myDS.Capabilities.ICapYResolution.GetValues().Contains(DPI))
            {
                myDS.Capabilities.ICapYResolution.SetValue(DPI);
            }

            myDS.Enable(SourceEnableMode.ShowUI, false, System.IntPtr.Zero);

            EventWaitHandle session_SourceDisable_Wait = new EventWaitHandle(false, EventResetMode.AutoReset);
            session_SourceDisable_Wait.WaitOne();
            return lesImagesNum;
        }