예제 #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)
                    {
                        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, this._extTag);
                            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);
            }
        }
예제 #3
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);
            }
        }
        protected void TransferPictures()
        {
            // see http://www.twain.org/wp-content/uploads/2017/03/TWAIN-2.4-Specification.pdf
            // page (3-20)

            if (DataSource.SourceId.Id == 0)
            {
                return;
            }

            PendingXfers pendingTransfer = new PendingXfers();
            TwainResult  result;

            try
            {
                do
                {
                    pendingTransfer.Count = 0;     // the Twain source will fill this in during DsPendingTransfer
                    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;
                    }
                    Bitmap bitmap;
                    try {
                        // 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;
                        }
                        if (hbitmap == IntPtr.Zero)
                        {
                            throw new TwainException("Transfer complete, but bitmap pointer is still null.");
                        }

                        bitmap = BitmapRenderer.NewBitmapFromHBitmap(hbitmap);
                    } finally {
                        if (hbitmap != IntPtr.Zero)
                        {
                            Kernel32Native.GlobalFree(hbitmap);
                            hbitmap = IntPtr.Zero;
                        }
                    }

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

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

                    // fire the transfer event...
                    TransferImageEventArgs args = new TransferImageEventArgs(bitmap, pendingTransfer.Count != 0, 1.0f);
                    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);
            }
        }
        protected void TransferPicturesIncremental()
        {
            // see http://www.twain.org/wp-content/uploads/2017/03/TWAIN-2.4-Specification.pdf
            // page 4-20
            Console.WriteLine("TransferPicturesIncremental...");
            Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, "TransferPicturesIncremental...");

            if (DataSource.SourceId.Id == 0)
            {
                return;
            }

            PendingXfers pendingTransfer = new PendingXfers();
            TwainResult  result;

            try
            {
                int recievedBlockCount = 1;
                do
                {
                    pendingTransfer.Count = 0;     // the Twain source will fill this in during DsPendingTransfer

                    Console.WriteLine("Get the image info...");
                    // 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;
                    }

                    /*Console.WriteLine("Get the image layout...");
                     * ImageLayout imageLayout = new ImageLayout();
                     * result = Twain32Native.DsImageLayout(
                     *  ApplicationId,
                     *  DataSource.SourceId,
                     *  DataGroup.Image,
                     *  DataArgumentType.ImageLayout,
                     *  Message.GetCurrent,
                     *  imageLayout);
                     *
                     * if (result != TwainResult.Success)
                     * {
                     *  DataSource.Close();
                     *  break;
                     * }*/

                    // Setup Destination Bitmap
                    Bitmap bitmap = BitmapRenderer.NewBitmapForImageInfo(imageInfo);

                    Console.WriteLine("Setup incremental Memory XFer...");
                    // Setup incremental Memory XFer
                    SetupMemXfer setupMemXfer = new SetupMemXfer();
                    result = Twain32Native.DsSetupMemXfer(
                        ApplicationId,
                        DataSource.SourceId,
                        DataGroup.Control,
                        DataArgumentType.SetupMemXfer,
                        Message.Get,
                        setupMemXfer
                        );

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

                    Console.WriteLine("allocate the preferred buffer size...");
                    // allocate the preferred buffer size
                    // see twain spec pdf, page 4-21
                    ImageMemXfer imageMemXfer = new ImageMemXfer();
                    try
                    {
                        imageMemXfer.Memory.Flags  = MemoryFlags.AppOwns | MemoryFlags.Pointer;
                        imageMemXfer.Memory.Length = setupMemXfer.MinBufSize;                                                                 // 對於A8 scanner,Preferred = MaxBufSize,太大了,所以我們選小一點的
                        imageMemXfer.Memory.TheMem = Kernel32Native.GlobalAlloc(GlobalAllocFlags.MemFixed, (int)setupMemXfer.MinBufSize * 2); // 不知道為什麼原本她size寫要*2倍
                        imageMemXfer.Compression   = Compression.None;

                        if (imageMemXfer.Memory.TheMem == IntPtr.Zero)
                        {
                            Logger.WriteLog(LOG_LEVEL.LL_SERIOUS_ERROR, "error allocating buffer for memory transfer");
                            throw new TwainException("error allocating buffer for memory transfer");
                        }

                        long pixels_written = 0;
                        long total_pixels   = imageInfo.ImageWidth * imageInfo.ImageLength;

                        do
                        {
                            // perform a transfer
                            result = Twain32Native.DsImageMemXfer(
                                ApplicationId,
                                DataSource.SourceId,
                                DataGroup.Image,
                                DataArgumentType.ImageMemXfer,
                                Message.Get,
                                imageMemXfer
                                );

                            //string savePath = @"C:\Users\Tenny\Pictures\TwainTest\tempBitmap_";
                            //savePath += i.ToString() + @".bmp";
                            if (result == TwainResult.Success || result == TwainResult.XferDone)
                            {
                                // dibArray是這次Buffer的RGB陣列
                                byte[] dibArray = ShiftPixels(ref imageMemXfer, imageInfo.BitsPerPixel / 8);

                                BitmapRenderer.TransferPixels(bitmap, imageInfo, imageMemXfer);
                                pixels_written += (imageMemXfer.BytesWritten * 8) / imageInfo.BitsPerPixel;
                                double percent_complete = (double)pixels_written / (double)total_pixels;

                                if (result == TwainResult.XferDone)
                                {
                                    percent_complete = 1.0;
                                    // 算出空白區域的高度,裁切尾端部分
                                    int blankHeight = GetCropHeight(bitmap);
                                    if (blankHeight > 0 && blankHeight < imageInfo.ImageLength)
                                    {
                                        bitmap = cropImage(bitmap, blankHeight);
                                    }
                                }

                                // fire the transfer event
                                TransferImageEventArgs args = new TransferImageEventArgs(bitmap, result != TwainResult.XferDone, (float)percent_complete);
                                TransferImage(this, args);
                                if (!args.ContinueScanning)
                                {
                                    result = TwainResult.XferDone;
                                }
                            }
                            recievedBlockCount++;
                        } while (result == TwainResult.Success);
                    }
                    finally
                    {
                        if (imageMemXfer.Memory.TheMem != IntPtr.Zero)
                        {
                            Kernel32Native.GlobalFree(imageMemXfer.Memory.TheMem);
                            imageMemXfer.Memory.TheMem = IntPtr.Zero;
                        }
                    }

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

                    if (result != TwainResult.Success)
                    {
                        DataSource.Close();
                        break;
                    }
                }while (pendingTransfer.Count != 0);
            }
            finally
            {
                // Reset any pending transfers
                result = Twain32Native.DsPendingTransfer(
                    ApplicationId,
                    DataSource.SourceId,
                    DataGroup.Control,
                    DataArgumentType.PendingXfers,
                    Message.Reset,
                    pendingTransfer);
                Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, "TransferPicturesIncremental...done.");
            }
        }
예제 #6
0
 void TransferImage(object sender, TransferImageEventArgs e)
 {
     if (e.Image != null)
     {
         if (this.scanTarget == ScanTarget.APP)
         {
             int x = Properties.Settings.Default.x;
             int y = Properties.Settings.Default.y;
             int w = Properties.Settings.Default.w;
             int h = Properties.Settings.Default.h;
             Rectangle r = MainForm.getRectangle(e.Image, x, y, w, h);
             if (!r.IsEmpty)
             {
                 this.pictureBox.Image = e.Image.Clone(MainForm.getRectangle(e.Image, x, y, w, h), e.Image.PixelFormat);
             }
             else
             {
                 this.pictureBox.Image = e.Image;
             }
         }
         else if (this.scanTarget == ScanTarget.WEB)
         {
             this.scanData.Add(e.Image);
         }
     }
 }
예제 #7
0
파일: Scaner.cs 프로젝트: ondister/Recog
 void _twain_TransferImage(object sender, TransferImageEventArgs e)
 {
     if (e.Image != null)
     {
         _image = e.Image;
     }
 }
예제 #8
0
        private void TwainTransferImage(object sender, TransferImageEventArgs e)
        {
            if (MainScanImage == null) throw new ApplicationException("Není nastavený objekt pro ulození naskenovného obrázku.");

            try
            {
                if (e.Image != null)
                {
                    if (Settings.Default.ScannerSaveToFile)
                    {
                        Logger.Log(String.Format("TwainTransferImage-FILE"));
                        //System.Drawing.Bitmap bmp = BitmapFunctions.AutoColorCorrections(e.Image);
                        //Logger.Log(String.Format("TwainTransferImage-AutoColorCorrections"));
                        e.Image.Save(System.IO.Path.Combine(Settings.Default.ScanFolderPath, TWAIN_SCAN_FILENAME), System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                    else
                    {
                        Logger.Log(String.Format("TwainTransferImage-MEMORY"));
                        IntPtr hbitmap = new System.Drawing.Bitmap(e.Image).GetHbitmap();
                        Logger.Log(String.Format("TwainTransferImage-GetHbitmap"));
                        ScanBitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                        Logger.Log(String.Format("TwainTransferImage-CreateBitmapSource"));
                        Gdi32Native.DeleteObject(hbitmap);
                    }
                }
                else
                {
                    string message = "Nepodařilo se naskenovat obrázek.";
                    MessageBox.Show(message, "TwainTransferImage", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    Logger.Log(String.Format("ERROR: {0}", message));
                }
            }
            catch (TwainException tx)
            {
                string message = String.Format("{0}\nConditionCode={1}\nReturnCode={2}.", tx.Message, tx.ConditionCode, tx.ReturnCode);
                MessageBox.Show(message, tx.Source, MessageBoxButton.OK, MessageBoxImage.Error);
                Logger.Log(String.Format("TWAIN: {0}", message));
                SetMenuButtonsEnabled();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Source, MessageBoxButton.OK, MessageBoxImage.Error);
                Logger.Log(String.Format("ERROR: {0}", ex.Message));
                SetMenuButtonsEnabled();
            }
            finally
            {
                GetAvailableMemory();
                Logger.Log("TwainTransferImage-Finally");
            }
        }
예제 #9
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);
                }
            }
        }
예제 #10
0
		private void Twain_TransferImage(object sender, TransferImageEventArgs e)
		{
			if (e.Image != null)
			{
				Log("Image transfered");
				var img = e.Image;
				if (_images == null) _images = new List<Image>();

				_images.Add((Image)img.Clone());
				img.Dispose();
			}
		}
예제 #11
0
 /// <summary>
 /// FrontTwainTransferImage is the delegate to send the front-side scanned image to the form control 'imgVwrFront'
 /// </summary>
 /// <param name="sender">Object</param>
 /// <param name="e">TransferImageEventArgs</param>
 void FrontTwainTransferImage(Object sender, TransferImageEventArgs e)
 {
     if (e.Image != null)
         this.imgVwrFront.EditableImage = e.Image;
 }
예제 #12
0
 /// <summary>
 /// BackTwain_TransferImage is the delegate to send the back-side scanned image to the form control 'imgVwrBack'
 /// </summary>
 /// <param name="sender">Object</param>
 /// <param name="e">TransferImageEventArgs</param>
 void BackTwain_TransferImage(Object sender, TransferImageEventArgs e)
 {
     if (e.Image != null)
         this.imgVwrBack.EditableImage = e.Image;
 }