コード例 #1
0
        protected void TransferPictures()
        {
            if (DataSource.SourceId.Id == 0)
            {
                return;
            }

            var pendingTransfer = new PendingXfers();
            TwainResult result;
	        try
	        {
		        do
		        {
			        pendingTransfer.Count = 0;
			        var hbitmap = IntPtr.Zero;

			        // Get the image info
			        var imageInfo = new ImageInfo();
			        result = Twain32Native.DsImageInfo(
				        ApplicationId,
				        DataSource.SourceId,
				        DataGroup.Image,
				        DataArgumentType.ImageInfo,
				        Message.Get,
				        imageInfo);

					_log.Debug(string.Format("Get(ImageInfo), result: {0}", result));
			        if (result != TwainResult.Success)
			        {
				        CloseDataSource();
				        break;
			        }

			        // Transfer the image from the device
			        result = Twain32Native.DsImageTransfer(
				        ApplicationId,
				        DataSource.SourceId,
				        DataGroup.Image,
				        DataArgumentType.ImageNativeXfer,
				        Message.Get,
				        ref hbitmap);

					_log.Debug(string.Format("Get(ImageNativeXfer), result: {0}", result));

			        if (result != TwainResult.XferDone)
			        {
						_log.ErrorFormat("Transfer the image from the device failed. Result: {0}", result);
				        CloseDataSource();
				        break;
			        }

			        DataSource.SourceState = TwainState.Transfering;

			        // End pending transfers
			        result = Twain32Native.DsPendingTransfer(
				        ApplicationId,
				        DataSource.SourceId,
				        DataGroup.Control,
				        DataArgumentType.PendingXfers,
				        Message.EndXfer,
				        pendingTransfer);

					_log.Debug(string.Format("EndXfer(PendingXfers), result: {0}", result));
			        if (result != TwainResult.Success)
			        {
				        CloseDataSource();
				        break;
			        }

			        DataSource.SourceState = TwainState.TransferReady;

			        if (hbitmap == IntPtr.Zero)
			        {
				        _log.Warn("Transfer complete but bitmap pointer is still null.");
			        }
			        else
			        {
				        using (var renderer = new BitmapRenderer(hbitmap))
				        {
					        TransferImageEventArgs args = new TransferImageEventArgs(renderer.RenderToBitmap(),
						        pendingTransfer.Count != 0);
					        TransferImage(this, args);
					        if (!args.ContinueScanning)
						        break;
				        }
			        }
		        } while (pendingTransfer.Count != 0);
	        }
	        catch (Exception e)
	        {
		        
	        }
            finally
            {
                // Reset any pending transfers
                result = Twain32Native.DsPendingTransfer(
                    ApplicationId,
                    DataSource.SourceId,
                    DataGroup.Control,
                    DataArgumentType.PendingXfers,
                    Message.Reset,
                    pendingTransfer);

				
	            if (result == TwainResult.Success)
	            {
		            _twainState = TwainState.SourceEnabled;
					_log.Debug(string.Format("Reset(PendingXfers), result: {0}", result));
	            }
	            else
	            {
					var conditionCode = GetConditionCode(ApplicationId, DataSource.SourceId);
					_log.ErrorFormat("Reset(PendingXfers), result: {0}, condition code: {1}", result, conditionCode);		
	            }
            }
        }
コード例 #2
0
        protected void TransferPictures()
        {
            if (DataSource.SourceId.Id == 0)
            {
                return;
            }

            PendingXfers pendingTransfer = new PendingXfers();
            TwainResult result;
            try
            {
                do
                {
                    pendingTransfer.Count = 0;
                    IntPtr hbitmap = IntPtr.Zero;

                    // Get the image info
                    ImageInfo imageInfo = new ImageInfo();
                    result = Twain32Native.DsImageInfo(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Image,
                        DataArgumentType.ImageInfo,
                        Message.Get,
                        imageInfo);

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }

                    // Transfer the image from the device
                    result = Twain32Native.DsImageTransfer(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Image,
                        DataArgumentType.ImageNativeXfer,
                        Message.Get,
                        ref hbitmap);

                    if (result != TwainResult.XferDone)
                    {
                        DataSource.Close();
                        break;
                    }

                    // End pending transfers
                    result = Twain32Native.DsPendingTransfer(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Control,
                        DataArgumentType.PendingXfers,
                        Message.EndXfer,
                        pendingTransfer);

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }

                    if (hbitmap != IntPtr.Zero)
                    {
                        using (var renderer = new BitmapRenderer(hbitmap))
                        {
                            TransferImageEventArgs args = new TransferImageEventArgs(renderer.RenderToBitmap(), pendingTransfer.Count != 0);
                            TransferImage(this, args);
                            if (!args.ContinueScanning)
                                break;
                        }
                    }
                }
                while (pendingTransfer.Count != 0);
            }
            finally
            {
                // Reset any pending transfers
                result = Twain32Native.DsPendingTransfer(
                    ApplicationId,
                    DataSource.SourceId,
                    DataGroup.Control,
                    DataArgumentType.PendingXfers,
                    Message.Reset,
                    pendingTransfer);
            }
        }