Exemplo n.º 1
0
        protected void PreFilterMessage(System.Windows.Forms.Message m, ref bool handled)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not || cmd == TwainCommand.Null)
            {
                return; // do not change handled
            }
            switch (cmd)
            {
            case TwainCommand.CloseRequest: {
                OnTwainCloseRequest();
                break;
            }

            case TwainCommand.CloseOk: {
                OnTwainCloseOk();
                break;
            }

            case TwainCommand.DeviceEvent: {
                OnTwainDeviceEvent();
                break;
            }

            case TwainCommand.TransferReady: {
                OnTwainTransferReady();
                break;
            }
            }

            handled = true;
        }
Exemplo n.º 2
0
        //Métodos auxiliares para escanear
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand orden = escaner.RecibirMensaje(ref m);

            if (orden == TwainCommand.Not)
            {
                return(false);
            }

            switch (orden)
            {
            case TwainCommand.CloseRequest:
            {
                Terminar();
                escaner.CerrarConexion();
                break;
            }

            case TwainCommand.CloseOk:
            {
                Terminar();
                escaner.CerrarConexion();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList imagenes = escaner.TransferirImagenes();
                Terminar();
                escaner.CerrarConexion();

                for (int i = 0; i < imagenes.Count; i++)
                {
                    //byte []nuevaImagen;
                    IntPtr imagen = (IntPtr)imagenes[i];

                    //Arreglar
                    textoActual = new TextoManejado(GlobalLock(imagen));

                    textoActual = textoActual.Copia();

                    GlobalFree(imagen);

                    estadoImagen = EstadoImagen.nula;

                    CargarImagen();
                }
                break;
            }
            }

            return(true);
        }
Exemplo n.º 3
0
        bool IMessageFilter.PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            TwainCommand Tc = Tw.PassMessage(ref m);

            if (Tc == TwainCommand.Not)
            {
                return(false);
            }
            if (Tc == TwainCommand.Null)//by dh
            {
                return(false);
            }
            switch (Tc)
            {
            case TwainCommand.CloseRequest:
            case TwainCommand.CloseOk:
                EndScanning();
                Tw.CloseSrc();
                break;

            case TwainCommand.DeviceEvent:
                break;

            case TwainCommand.TransferReady:
                ArrayList LstPic = Tw.TransferPictures();
                EndScanning();
                Tw.CloseSrc();
                string inhosTimes = txtTimes.Text;
                if (inhosTimes.ToString().Length == 1)
                {
                    inhosTimes = "0" + inhosTimes;
                }
                for (int i = 0; i < LstPic.Count; ++i)
                {
                    IntPtr Img = (IntPtr)LstPic[i];
                    Page++;
                    string page     = SetPage(Page);
                    string fileName = LURecordNO.DisplayText + "_" + inhosTimes + "_" + page + "_00";
                    bmpptr = GlobalLock(Img);
                    pixptr = GetPixelInfo(bmpptr);
                    Gdip.SaveDIBAs(txtFolder.Text, fileName, bmpptr, pixptr);    //保存图片
                }
                DataTable data = CreatePictureDate(txtFolder.Text);
                pictureGrid.DataSource = data;
                if (data != null && data.Rows.Count > 0)
                {
                    pictureView.FocusedRowHandle = data.Rows.Count - 1;
                }
                txtPage.Text = SetPage(Page + 1);
                break;
                //case TwainCommand.Null://by dh
                //    EndScanning();
                //    Tw.CloseSrc();
                //    break;
            }
            return(true);
        }
Exemplo n.º 4
0
        bool IMessageFilter.PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            TwainCommand Tc = Tw.PassMessage(ref m);

            if (Tc == TwainCommand.Not)
            {
                return(false);
            }
            if (Tc == TwainCommand.Null)//by dh
            {
                return(false);
            }
            switch (Tc)
            {
            case TwainCommand.CloseRequest:
                timer1.Stop();
                break;

            case TwainCommand.CloseOk:
                EndScanning();
                Tw.CloseSrc();
                break;

            case TwainCommand.DeviceEvent:
                timer1.Stop();
                break;

            case TwainCommand.TransferReady:
                timer1.Enabled  = true;
                timer1.Interval = 10;
                timer1.Start();
                ArrayList LstPic = Tw.TransferPictures();    //点击扫描按钮
                EndScanning();
                Tw.CloseSrc();
                string inhosTimes = txtTimes.Text;
                if (inhosTimes.ToString().Length == 1)
                {
                    inhosTimes = "0" + inhosTimes;
                }
                for (int i = 0; i < LstPic.Count; ++i)
                {
                    IntPtr Img = (IntPtr)LstPic[i];
                    Page++;
                    string page     = SetPage(Page);
                    string fileName = txtID.Text + "_" + inhosTimes + "_" + page + "_00";
                    bmpptr = GlobalLock(Img);
                    pixptr = GetPixelInfo(bmpptr);
                    Gdip.SaveDIBAs(txtFolder.Text, fileName, bmpptr, pixptr); //保存图片
                }
                isSuccessEnd = true;                                          //已完成
                OnRefresh(null, null);                                        //刷新图片采集主窗体
                timer1.Stop();
                this.Close();
                break;
            }
            return(true);
        }
Exemplo n.º 5
0
        public bool PreFilterMessage(ref Message m)
        {
            try
            {
                if (tw == null)
                {
                    return(true);
                }

                TwainCommand cmd = tw.PassMessage(ref m);

                if (cmd == TwainCommand.Not)
                {
                    return(false);
                }

                if (cmd == TwainCommand.Null)
                {
                    return(false);
                }
                switch (cmd)
                {
                case TwainCommand.CloseRequest:
                {
                    //  EndingScan();
                    break;
                }

                case TwainCommand.CloseOk:
                {
                    EndingScan();
                    break;
                }

                case TwainCommand.DeviceEvent:
                {
                    break;
                }

                case TwainCommand.TransferReady:
                {
                    tw.TransferPictures();
                    EndingScan();
                    break;
                }
                }

                return(true);
            }
            catch (Exception a)
            {
                ACSLog.InsertLog(MessageBoxIcon.Error, a);
                return(true);
            }
        }
Exemplo n.º 6
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            ArrayList arrayList = this.tw.TransferPictures();

            this._DibHands = new ArrayList();
            bool result;

            if (arrayList.Count != 0)
            {
                this.EndingScan();
                this.tw.Finish();
                this.picnumber++;
                for (int i = 0; i < arrayList.Count; i++)
                {
                }
                result = true;
            }
            else
            {
                TwainCommand twainCommand = this.tw.PassMessage(ref m);
                if (twainCommand == TwainCommand.Null || twainCommand == TwainCommand.Not)
                {
                    result = false;
                }
                else
                {
                    switch (twainCommand)
                    {
                    case TwainCommand.TransferReady:
                        this.EndingScan();
                        this.tw.CloseSrc();
                        this.picnumber++;
                        for (int i = 0; i < arrayList.Count; i++)
                        {
                            IntPtr intPtr = (IntPtr)arrayList[i];
                        }
                        break;

                    case TwainCommand.CloseRequest:
                        this.EndingScan();
                        this.tw.CloseSrc();
                        break;

                    case TwainCommand.CloseOk:
                        this.EndingScan();
                        this.tw.CloseSrc();
                        break;
                    }
                    result = true;
                }
            }
            return(result);
        }
Exemplo n.º 7
0
            public bool PreFilterMessage(ref Message m)
            {
                TwainCommand cmd = tw.PassMessage(ref m);

                if (cmd == TwainCommand.Not)
                {
                    return(false);
                }

                switch (cmd)
                {
                case TwainCommand.CloseRequest:
                {
                    EndingScan();
                    tw.CloseSrc();
                    form.Close();
                    break;
                }

                case TwainCommand.CloseOk:
                {
                    EndingScan();
                    tw.CloseSrc();
                    break;
                }

                case TwainCommand.DeviceEvent:
                {
                    break;
                }

                case TwainCommand.TransferReady:
                {
                    ArrayList pics = tw.TransferPictures();
                    EndingScan();
                    tw.CloseSrc();
                    foreach (IntPtr img in pics)
                    {
                        int bitcount = 0;

                        using (Bitmap bmp = DibUtils.BitmapFromDib(img, out bitcount))
                        {
                            Bitmaps.Add(new ScannedImage(bmp, bitcount == 1 ? ScanBitDepth.BlackWhite : ScanBitDepth.C24Bit, settings.MaxQuality, settings.Quality));
                        }
                    }
                    form.Close();
                    break;
                }
                }

                return(true);
            }
Exemplo n.º 8
0
        public bool PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = m_twain.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.TransferReady:
            {
                bool status = m_twain.TransferPictures(VoucherData);
                if (status)
                {
                    AcquireVoucher(true);
                }
                else
                {
                    m_twain.CloseDS();
                    m_twain.CloseDSM();
                    Close();
                }
                break;
            }

            case TwainCommand.CloseRequest:
            {
                m_twain.CloseDS();
                m_twain.CloseDSM();
                Close();
                break;
            }

            case TwainCommand.CloseOk:
            {
                WinMsg.SendText(m_MainFormHandler, Strings.VScan_ScanIsDoneEvent);
                Close();
                break;
            }

            case TwainCommand.DeviceEvent:
                break;

            default:
                break;
            }

            return(true);
        }
Exemplo n.º 9
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;
                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr  img    = (IntPtr)pics[i];
                    PicForm newpic = new PicForm(img);
                    newpic.MdiParent = this;
                    int picnum = i + 1;
                    newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString();
                    newpic.Show();
                }
                break;
            }
            }

            return(true);
        }
Exemplo n.º 10
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                this.Close();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr img      = (IntPtr)pics[i];
                    int    bitcount = 0;
                    Bitmap bmp      = CDIBUtils.BitmapFromDIB(img, out bitcount);
                    bitmaps.Add(new CScannedImage(bmp, bitcount == 1 ? CScanSettings.BitDepth.BLACKWHITE : CScanSettings.BitDepth.C24BIT));
                }
                this.Close();
                break;
            }
            }

            return(true);
        }
Exemplo n.º 11
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }
            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;
                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr img = (IntPtr)pics[i];
                    bmpptr = GlobalLock(img);
                    pixptr = GetPixelInfo(bmpptr);
                    tw.SaveDIBAs(bmpptr, pixptr, i + 1);
                }
                scannedcount          = 0;
                scannedcount          = this.NoofScannedfileCount();
                this.HeaderLabel.Text = scannedcount + " page scanned.";
                break;
            }
            }
            return(true);
        }
Exemplo n.º 12
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                this.SaveImages();
                break;
            }

            default:
            {
                return(false);
            }
            }

            return(true);
        }
Exemplo n.º 13
0
        public bool PreFilterMessage(ref Message message)
        {
            TwainCommand cmd = m_Twain.PassMessage(ref message);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                m_Twain.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                m_Twain.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                TransferPictures();
                break;
            }
            }
            return(true);
        }
Exemplo n.º 14
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;
                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr  img    = (IntPtr)pics[i];
                    PicForm newpic = new PicForm(img);
                    newpic.MdiParent = this;
                    int picnum = i + 1;
                    newpic.Text = "Изображение " + picnumber.ToString();
                    if (newpic.SaveAs_() != "")
                    {
                        newpic.Show();
                    }
                }
                tbcntImage.Text = picnumber.ToString();
                foreach (Form f in MdiChildren)
                {
                    if (f is PicForm)
                    {
                        //MessageBox.Show(f.Text);
                    }
                }

                break;
            }
            }

            return(true);
        }
Exemplo n.º 15
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            string       sFileImage = "";
            TwainCommand cmd        = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Null)
            {
                //EndingScan();
                return(false);
            }
            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
                EndingScan();
                tw.CloseSrc();
                break;

            case TwainCommand.CloseOk:
                EndingScan();
                tw.CloseSrc();
                break;

            case TwainCommand.DeviceEvent:
                break;

            case TwainCommand.TransferReady:
                IntPtr    img;
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                for (int i = 0; i < pics.Count; i++)
                {
                    img = (IntPtr)pics[i];
                    PicForm newpic = new PicForm(img);
                    int     picnum = i + 1;
                }
                if (pics.Count > 0)
                {
                    sFileImage = getFileImage().Trim();

                    try
                    {
                        if (!Directory.Exists(ImagePath))
                        {
                            DirectoryInfo di = Directory.CreateDirectory(ImagePath);
                        }

                        string filepath = ImagePath + @"\" + sFileImage.Trim();

                        Gdip.SaveDIBAsFile(filepath, PicForm.bmpptr, PicForm.pixptr);

                        Image picForCheck = Image.FromFile(filepath);

                        if (picForCheck != null)
                        {
                            System.IO.FileInfo file = new System.IO.FileInfo(filepath);
                            CheckAndSaveImage(picForCheck, file);
                        }
                    }
                    catch { }
                }
                else
                {
                    MessageBox.Show("Ошибка сканирования!");
                }
                break;
            }

            return(true);
        }
Exemplo n.º 16
0
        bool IMessageFilter.PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            TwainCommand Tc = Tw.PassMessage(ref m);

            if (Tc == TwainCommand.Not)
            {
                return(false);
            }
            //if (Tc == TwainCommand.Null)//by dh
            //{
            //    return false;
            //}
            switch (Tc)
            {
            case TwainCommand.CloseRequest:
            case TwainCommand.CloseOk:
                EndScanning();
                Tw.CloseSrc();
                break;

            case TwainCommand.DeviceEvent:
                break;

            case TwainCommand.TransferReady:
                ArrayList LstPic = Tw.TransferPictures();
                EndScanning();
                Tw.CloseSrc();
                string inhosTimes = txtTimes.Text;
                if (inhosTimes.ToString().Length == 1)
                {
                    inhosTimes = "0" + inhosTimes;
                }
                for (int i = 0; i < LstPic.Count; ++i)
                {
                    IntPtr Img = (IntPtr)LstPic[i];
                    Page++;
                    string page     = SetPage(Page);
                    string fileName = LURecordNO.DisplayText + "_" + inhosTimes + "_" + page + "_00";
                    bmpptr = GlobalLock(Img);
                    pixptr = GetPixelInfo(bmpptr);
                    Gdip.SaveDIBAs(txtFolder.Text, fileName, bmpptr, pixptr); //保存图片
                }
                for (int i = 0; i < LstPic.Count; ++i)                        //回收内存
                {
                    IntPtr Img = (IntPtr)LstPic[i];
                    Marshal.FreeHGlobal(Img);
                }
                DataTable data = CreatePictureDate(txtFolder.Text);
                pictureGrid.DataSource = data;
                if (data != null && data.Rows.Count > 0)
                {
                    pictureView.FocusedRowHandle = data.Rows.Count - 1;
                }
                txtStartPage.Text = SetPage(Page + 1);
                LstPic.Clear();
                //GC.SuppressFinalize(this);
                GC.Collect();    //回收内存
                //dibhand = IntPtr.Zero;
                //bmpptr = IntPtr.Zero;
                //pixptr = IntPtr.Zero;
                //Marshal.FreeCoTaskMem(dibhand);
                //Marshal.FreeCoTaskMem(bmpptr);
                //Marshal.FreeCoTaskMem(pixptr);
                //Marshal.FreeHGlobal(dibhand);
                //Marshal.FreeHGlobal(bmpptr);
                //Marshal.FreeHGlobal(pixptr);
                break;
            }
            return(true);
        }
Exemplo n.º 17
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                logger.Error("BaseScan.(DeviceEvent)裝置錯誤");
                //Console.WriteLine("DeviceEvent");
                break;
            }

            case TwainCommand.Null:
            {
                //logger.Error("BaseScan.(null) 正常等待");

                break;
            }

            case TwainCommand.Failure:
            {
                MessageBox.Show("設置錯誤");
                logger.Error("BaseScan.設置錯誤");
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.TransferReady:
            {
                #region 立即存檔
                logger.Info("BaseScan.1.已取得圖檔,存入指定路徑");
                DJScanCompletList = tw.TransferePicturesSaveImmediate(DJScanPath, DJScanTitle);
                EndingScan();
                tw.CloseSrc();
                #endregion

                switch (DJImageFormat)
                {
                case "jpg":
                    logger.Info("BaseScan.2.jpg 轉換");
                    DJScanCompletList = DJImageProcess.TransBmpToJpg(DJScanCompletList, DJJpgEncodeLevel, DJImgEncrypt);
                    logger.Info("BaseScan.2.jpg 轉換 ok");
                    break;

                default:
                    break;
                }

                logger.Info("BaseScan.3.21.送出完成委派");
                OnScanComplete(this);
                logger.Info("BaseScan.3.3.送完完成委派");
                break;
            }

            default:
                Console.WriteLine(cmd.ToString());
                break;
            }

            return(true);
        }
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                int ImgNumber = 0;

                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;

                if (oBatch.Find(_CompanyID, _CustomerID, _Teacher))
                {
                    //ImgNumber = oBatch._ImageFinal;
                }

                //    oBatch._ImageInitial = 1;
                oBatch.CompanyID = _CompanyID;
                //    oBatch.CustomerID = _CustomerID;


                int i;
                for (i = 0; i < pics.Count; i++)
                {
                    IntPtr img = (IntPtr)pics[i];

                    {
                        bmprect = new Rectangle(0, 0, 0, 0);
                        bmpptr  = GlobalLock(img);
                        pixptr  = GetPixelInfo(bmpptr);
                        int picnum = ImgNumber + i + 1;

                        //Gdip.SavePicToFile("ScanPass" + picnum.ToString() + ".tiff", bmpptr, pixptr);
                        Gdip.SavePicToFile("Images/Order-" + _CompanyID.PadLeft(2, '0') + _CustomerID.PadLeft(4, '0') + _Batch.PadLeft(3, '0') + picnum.ToString().PadLeft(4, '0') + ".tif", bmpptr, pixptr);
                    }
                }

                //Save Batch Here

                //oBatch._ImageFinal = ImgNumber + i;
                //oBatch._NumberImages += i;
                //oBatch.Teacher = _Teacher;
                oBatch.Save();



                break;
            }

            default:
            {
                return(false);
            }
            }

            return(true);
        }
Exemplo n.º 19
0
        public bool PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();

                int picCount = pics.Count;
                if (picCount > 0)
                {
                    string fileName = "";
                    for (int i = 0; i < picCount; i++)
                    {
                        IntPtr      img = (IntPtr)pics[i];
                        GetGraphics get = new GetGraphics(img);
                        Bitmap      b   = get.GetImage();

                        StringBuilder sb = new StringBuilder();
                        sb.Append("IMG");
                        sb.Append((i + 1).ToString("0000"));
                        sb.Append(DateTime.UtcNow.ToString("_yyyyMMddHHmmss"));
                        fileName = string.Format(SavePath, sb);

                        b.Save(fileName);
                        listPicture.Items.Add(fileName);

                        if (i == picCount - 1)
                        {
                            pictureBox1.Image = b;
                        }
                    }
                }
                break;
            }
            }

            return(true);
        }
Exemplo n.º 20
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = twainDevice.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan(true);
                twainDevice.CloseSrc();
                this.btnOk_Click(this, EventArgs.Empty);
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan(false);
                twainDevice.CloseSrc();
                this.btnOk_Click(this, EventArgs.Empty);
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = twainDevice.TransferPictures();

                EndingScan(false);
                twainDevice.CloseSrc();

                if (pics.Count > 0)
                {
                    int i, j = 1;

                    String DocumentID = String.Empty;
                    this.Cursor = Cursors.WaitCursor;

                    for (i = 0; i < pics.Count; i++)
                    {
                        IntPtr img = (IntPtr)pics[i];
                        Bitmap bmp = GetBitmap(img);

                        if (img != IntPtr.Zero)
                        {
                            imageBoxCtrl.pictureStrip1.AddPicture(bmp);
                            SaveToFile(i, bmp);

                            imageBoxCtrl.imageBox.Image = bmp;
                            imageBoxCtrl.imageBox.Size  = new Size(bmp.Width, bmp.Height);
                        }
                    }

                    j++;
                }
                this.Cursor = Cursors.Arrow;
                this.btnOk_Click(this, EventArgs.Empty);
                break;
            }
            }

            return(true);
        }
Exemplo n.º 21
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            if (m.HWnd != this.Handle)
            {
                return(false);
            }
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.Failure:
            case TwainCommand.CloseRequest:
            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                OnDialogClose(currentScanType, callback);
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                if (pics != null)
                {
                    List <Bitmap> bitmaps = new List <Bitmap>();
                    for (int n = 0; n < pics.Count; n++)
                    {
                        IntPtr img = (IntPtr)pics[n];
                        try
                        {
                            IntPtr bmpptr = Twain.GlobalLock(img);
                            Bitmap b      = null;
                            try
                            {
                                b = DibToImage.WithScan0(bmpptr);
                            }
                            catch { }
                            if (b != null)
                            {
                                bitmaps.Add(b);
                            }
                        }
                        catch (Exception ex)
                        {
                            Tiff.LibTiffHelper.WriteToLog(ex);
                        }
                        finally
                        {
                            Twain.GlobalFree(img);
                        }
                        pics[n] = null;
                    }
                    if (bitmaps.Count > 0)
                    {
                        OnImagesReceived(bitmaps, currentScanType, callback);
                    }
                }
                break;
            }
            }
            return(true);
        }
Exemplo n.º 22
0
        // receives messages and handles TWAIN device messages
        public bool PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            TwainCommand twainCommand = _twain.PassMessage(ref m);

            if (twainCommand == TwainCommand.Not)
            {
                return(false);
            }

            switch (twainCommand)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                _twain.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                _twain.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                if (TransferStarted != null)
                {
                    TransferStarted(this, EventArgs.Empty);
                }

                ArrayList pics = _twain.TransferPictures();

                for (int i = 0; i < pics.Count; i++)
                {
                    if (SaveTo == SaveTo.ImageObject)                             // save to Image object
                    {
                        Bitmap bmp = bitmapFromDIB((IntPtr)pics[i]);


                        ScannedImages.Add(bmp);
                    }
                    else                             // save to file
                    {
                        Guid   clsid;
                        String ext;

                        Bitmap bmp = bitmapFromDIB((IntPtr)pics[i]);

                        GetCodecClsid(this.ImageFileFormat, out clsid, out ext);

                        String fileName = this.OutputFolder;

                        if (String.IsNullOrEmpty(fileName))
                        {
                            fileName = Path.GetTempPath();
                        }

                        if (fileName.Length > 0 && !fileName.EndsWith("\\"))
                        {
                            fileName += "\\";
                        }

                        String namingTemplate = "Image_{0:D2}";

                        if (!String.IsNullOrEmpty(FileNamingTemplate))
                        {
                            namingTemplate = FileNamingTemplate;
                        }

                        fileName += String.Format(namingTemplate, i) + ext;

                        try
                        {
                            File.Delete(fileName);
                        }
                        catch
                        {
                        }

                        try
                        {
                            if (ImageFileFormat == ImageFormat.Jpeg)
                            {
                                ImageCodecInfo    ici = GetEncoderInfo("image/jpeg");
                                EncoderParameters eps = new EncoderParameters(1);
                                eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, this.JpegQuality);

                                bmp.Save(fileName, ici, eps);
                            }
                            else if (ImageFileFormat == ImageFormat.Tiff)
                            {
                                ImageCodecInfo    ici         = GetEncoderInfo("image/tiff");
                                long              compression = this.TiffCompression;
                                EncoderParameters eps         = new EncoderParameters(1);
                                eps.Param[0] = new EncoderParameter(Encoder.Compression, compression);

                                bmp.Save(fileName, ici, eps);
                            }
                            else
                            {
                                bmp.Save(fileName, ImageFileFormat);
                            }

                            ScannedImages.Add(fileName);
                        }
                        catch (System.Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }
                        finally
                        {
                            bmp.Dispose();
                        }
                    }
                }

                EndingScan();
                _twain.CloseSrc();
                break;
            }

            case TwainCommand.Null:
            {
                _twainCommandNullCount++;

                if (_twainCommandNullCount > 25)
                {
                    _twainCommandNullCount = 0;
                    EndingScan();
                    _twain.CloseSrc();
                }
                break;
            }
            }

            return(true);
        }
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                int ImgNumber = 0;

                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;

                if (!this.mnShowImages.Checked)
                {
                    if (oImages.Find(_CompanyID, _CustomerID, _Teacher))
                    {
                        //  ImgNumber = oImages._ImageFinal;
                    }

                    // oImages._ImageInitial = 1;
                    oImages.CompanyID  = _CompanyID;
                    oImages.CustomerID = _CustomerID;
                }



                int i;
                for (i = 0; i < pics.Count; i++)
                {
                    IntPtr img = (IntPtr)pics[i];

                    if (this.mnShowImages.Checked)
                    {
                        PicForm newpic = new PicForm(img);
                        newpic.MdiParent = this;
                        //Panel.Controls.Add((Control)newpic);

                        int picnum = ImgNumber + i + 1;
                        //newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString();
                        newpic.Text = "Order" + picnumber.ToString() + "_Pic" + picnum.ToString();
                        newpic.Show();
                    }
                    else
                    {
                        bmprect = new Rectangle(0, 0, 0, 0);
                        bmpptr  = GlobalLock(img);
                        pixptr  = GetPixelInfo(bmpptr);
                        int picnum = ImgNumber + i + 1;

                        //Gdip.SavePicToFile("ScanPass" + picnum.ToString() + ".tiff", bmpptr, pixptr);
                        Gdip.SavePicToFile("Images/Order-" + _CompanyID.PadLeft(2, '0') + _CustomerID.PadLeft(4, '0') + _Batch.PadLeft(3, '0') + picnum.ToString().PadLeft(4, '0') + ".tif", bmpptr, pixptr);
                    }
                }
                if (!this.mnShowImages.Checked)
                {
                    //Save Batch Here

                    //   oImages._ImageFinal = ImgNumber + i;
                    //oImages._NumberImages += i;
                    oImages.Teacher = _Teacher;
                    oImages.Save();
                }

                break;
            }

            default:
            {
                return(false);
            }
            }

            return(true);
        }
Exemplo n.º 24
0
        ///////////////////////////////////////////////////////////////////////////////
        // Public Functions, these are the essentials...
        ///////////////////////////////////////////////////////////////////////////////
        #region Public Functions...

        /// <summary>
        /// Our constructor...
        /// </summary>
        /// <param name="a_szManufacturer">Application manufacturer</param>
        /// <param name="a_szProductFamily">Application product family</param>
        /// <param name="a_szProductName">Name of the application</param>
        /// <param name="a_u16ProtocolMajor">TWAIN protocol major (doesn't have to match TWAINH.CS)</param>
        /// <param name="a_u16ProtocolMinor">TWAIN protocol minor (doesn't have to match TWAINH.CS)</param>
        /// <param name="a_u32SupportedGroups">Bitmask of DG_ flags</param>
        /// <param name="a_twcy">Country code for the application</param>
        /// <param name="a_szInfo">Info about the application</param>
        /// <param name="a_twlg">Language code for the application</param>
        /// <param name="a_u16MajorNum">Application's major version</param>
        /// <param name="a_u16MinorNum">Application's minor version</param>
        /// <param name="a_blUseLegacyDSM">Use the legacy DSM (like TWAIN_32.DLL)</param>
        /// <param name="a_blUseCallbacks">Use callbacks instead of Windows post message</param>
        /// <param name="a_deviceeventback">Function to receive device events</param>
        /// <param name="a_scancallback">Function to handle scanning</param>
        /// <param name="a_runinuithreaddelegate">Help us run in the GUI thread on Windows</param>
        /// <param name="a_intptrHwnd">window handle</param>
        public TWAIN
        (
            string a_szManufacturer,
            string a_szProductFamily,
            string a_szProductName,
            ushort a_u16ProtocolMajor,
            ushort a_u16ProtocolMinor,
            uint a_u32SupportedGroups,
            TWCY a_twcy,
            string a_szInfo,
            TWLG a_twlg,
            ushort a_u16MajorNum,
            ushort a_u16MinorNum,
            bool a_blUseLegacyDSM,
            bool a_blUseCallbacks,
            DeviceEventCallback a_deviceeventback,
            ScanCallback a_scancallback,
            RunInUiThreadDelegate a_runinuithreaddelegate,
            IntPtr a_intptrHwnd
        )
        {
            TW_IDENTITY twidentity;

            // Since we're using P/Invoke in this sample, the DLL
            // is implicitly loaded as we access it, so we can
            // never go lower than state 2...
            m_state = STATE.S2;

            // Register the caller's info...
            twidentity = default(TW_IDENTITY);
            twidentity.Manufacturer.Set(a_szManufacturer);
            twidentity.ProductFamily.Set(a_szProductFamily);
            twidentity.ProductName.Set(a_szProductName);
            twidentity.ProtocolMajor = a_u16ProtocolMajor;
            twidentity.ProtocolMinor = a_u16ProtocolMinor;
            twidentity.SupportedGroups = a_u32SupportedGroups;
            twidentity.Version.Country = a_twcy;
            twidentity.Version.Info.Set(a_szInfo);
            twidentity.Version.Language = a_twlg;
            twidentity.Version.MajorNum = a_u16MajorNum;
            twidentity.Version.MinorNum = a_u16MinorNum;
            m_twidentityApp = twidentity;
            m_twidentitylegacyApp = TwidentityToTwidentitylegacy(twidentity);
            m_twidentitymacosxApp = TwidentityToTwidentitymacosx(twidentity);
            m_deviceeventcallback = a_deviceeventback;
            m_scancallback = a_scancallback;
            m_runinuithreaddelegate = a_runinuithreaddelegate;
            m_intptrHwnd = a_intptrHwnd;

            // Placeholder for our DS identity...
            m_twidentityDs = default(TW_IDENTITY);
            m_twidentitylegacyDs = default(TW_IDENTITY_LEGACY);
            m_twidentitymacosxDs = default(TW_IDENTITY_MACOSX);

            // We'll normally do an automatic get of DAT.STATUS, but if we'd
            // like to turn it off, this is the variable to hit...
            m_blAutoDatStatus = true;

            // Our helper functions from the DSM...
            m_twentrypointdelegates = default(TW_ENTRYPOINT_DELEGATES);

            // Our events...
            m_autoreseteventCaller = new AutoResetEvent(false);
            m_autoreseteventThread = new AutoResetEvent(false);
            m_autoreseteventRollback = new AutoResetEvent(false);
            m_autoreseteventThreadStarted = new AutoResetEvent(false);
            m_lockTwain = new Object();

            // Windows only...
            if (ms_platform == Platform.WINDOWS)
            {
                m_blUseLegacyDSM = a_blUseLegacyDSM;
                m_blUseCallbacks = a_blUseCallbacks;
                m_windowsdsmentrycontrolcallbackdelegate = WindowsDsmEntryCallbackProxy;
            }

            // Linux only...
            else if (ms_platform == Platform.LINUX)
            {
                m_blUseLegacyDSM = false;
                m_blUseCallbacks = true;
                m_linuxdsmentrycontrolcallbackdelegate = LinuxDsmEntryCallbackProxy;
            }

            // Mac OS X only...
            else if (ms_platform == Platform.MACOSX)
            {
                m_blUseLegacyDSM = false;
                m_blUseCallbacks = true;
                m_macosxdsmentrycontrolcallbackdelegate = MacosxDsmEntryCallbackProxy;
            }

            // Uh-oh, Log will throw an exception for us...
            else
            {
                TWAINWorkingGroup.Log.Assert("Unsupported platform..." + ms_platform);
            }

            // Activate our thread...
            if (m_threadTwain == null)
            {
                m_twaincommand = new TwainCommand();
                m_threadTwain = new Thread(Main);
                m_threadTwain.Start();
                if (!m_autoreseteventThreadStarted.WaitOne(5000))
                {
                    try
                    {
                        m_threadTwain.Abort();
                        m_threadTwain = null;
                    }
                    catch
                    {
                        // Log will throw an exception for us...
                        TWAINWorkingGroup.Log.Assert("Failed to start the TWAIN background thread...");
                    }
                }
            }
        }
Exemplo n.º 25
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Null)
            {
                EndingScan();
                tw.CloseSrc();
                return(false);
            }
            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;
                if (pics.Count > 0)
                {
                }
                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr img = (IntPtr)pics[i];


                    dibhand = img;
                    bmpptr  = GlobalLock(dibhand);
                    pixptr  = GetPixelInfo(bmpptr);
                    //Bitmap map = new Bitmap(130, 170);
                    Bitmap   map = new Bitmap(bmprect.Width, bmprect.Height);
                    Graphics scannedImageGraphics = Graphics.FromImage(map);

                    IntPtr hdc = scannedImageGraphics.GetHdc();
                    // IntPtr hdc = map.GetHbitmap();
                    SetDIBitsToDevice(hdc, 0, 0, bmprect.Width, bmprect.Height,
                                      0, 0, 0, bmprect.Height, pixptr, bmpptr, 0);
                    scannedImageGraphics.ReleaseHdc(hdc);

                    // map.Save("c:\\tempgetimageformdevice.bmp");
                    // this.pictureBox1.Image = map;
                    if (set.CapWidth == set.DevWidth && set.CapHeight == set.DevHeight)
                    {
                        this.picPic.Image = map;
                        this.SavePic();
                    }
                    else
                    {
                        CaptureImage form = new CaptureImage(map, this.picPic, set);
                        form.ShowDialog();
                        this.SavePic();
                    }



                    /*
                     * //333,266
                     * Rectangle cltrect = ClientRectangle;
                     * Point loc = this.pictureBox1.Location;
                     * //100 128
                     * Size size=this.pictureBox1.Size;
                     * //Point scrol = AutoScrollPosition;
                     *
                     * // Rectangle realrect = this.pictureBox1.Location;
                     * // realrect.X -= scrol.X;
                     * //  realrect.Y -= scrol.Y;
                     *
                     * //240
                     * int bot = bmprect.Height;
                     *
                     *
                     * dibhand = img;
                     * bmpptr = GlobalLock(dibhand);
                     * pixptr = GetPixelInfo(bmpptr);
                     * IntPtr hdc = this.pictureBox1.CreateGraphics().GetHdc();
                     * SetDIBitsToDevice(hdc, 0, 0, bmprect.Width, bmprect.Height,
                     *      0, 0, 0, bmprect.Height, pixptr, bmpptr, 0);
                     * //this.pictureBox1.CreateGraphics().ReleaseHdc(hdc);
                     * // Image img = (Image)pics[i];
                     * // PicForm newpic = new PicForm(img);
                     * // newpic.MdiParent = this;
                     * //this.pictureBox1.Image = img;
                     * int picnum = i + 1;
                     *
                     *
                     * // newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString();
                     * //  newpic.Show();
                     */
                }
                break;
            }
            }

            return(true);
        }
Exemplo n.º 26
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();
                picnumber++;
                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr  img    = (IntPtr)pics[i];
                    PicForm newpic = new PicForm(img);
                    newpic.SetLanguage(menuItemEnglish.Checked ? 0 : 1);
                    newpic.MdiParent = this;
                    int picnum = i + 1;
                    //newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString();

                    if (m_TargetPath == "פעיל")
                    {
                        newpic.Text = m_ScanType + m_FileName;
                        newpic.SetPath(m_LicenceAndInsurancePath + m_TargetPath + "\\");
                    }
                    else
                    {
                        newpic.Text = m_FileName;
                        newpic.SetPath(m_ScannerPath + m_TargetPath + "\\");
                    }

                    if (menuItemAutoScanNext.Checked)
                    {
                        newpic.AutoScanNext();
                    }

                    newpic.Show();
                    toolStripButtonSave.Enabled = true;

                    if (menuItemAutoScanNext.Checked)
                    {
                        while (newpic.Visible)
                        {
                            Application.DoEvents();
                        }

                        Scan(m_TargetPath, m_ShowSnif, m_TargetNumeric);
                    }
                }
                break;
            }
            }

            return(true);
        }
Exemplo n.º 27
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Null)
            {
                return(false);
            }

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            case TwainCommand.TransferReady:
            {
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();

                for (int i = 0; i < pics.Count; i++)
                {
                    dibhand = (IntPtr)pics[i];
                    bmpptr  = GlobalLock(dibhand);
                    pixptr  = GetPixelInfo(bmpptr);

                    IntPtr img = IntPtr.Zero;
                    Guid   clsid;
                    String filename = "temp" + i + ".png";

                    if (!Gdip.GetCodecClsid(filename, out clsid))
                    {
                        return(false);
                    }

                    int st = Gdip.GdipCreateBitmapFromGdiDib(bmpptr, pixptr, ref img);

                    if ((st != 0) || (img == IntPtr.Zero))
                    {
                        return(false);
                    }

                    st = Gdip.GdipSaveImageToFile(img, filename, ref clsid, IntPtr.Zero);
                    Gdip.GdipDisposeImage(img);

                    addImageToPicturebox(filename);
                    actSaveToDb(null, null);
                    btnSaveToDb.Enabled = true;
                }
                break;
            }
            }
            return(true);
        }
Exemplo n.º 28
0
        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            TwainCommand cmd = tw.PassMessage(ref m);

            if (cmd == TwainCommand.Not)
            {
                return(false);
            }

            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                // _Mensaje = "CloseRequest";
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.CloseOk:
            {
                //    _Mensaje = "CloseOk";
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                //     _Mensaje = "DeviceEvent";
                break;
            }

            case TwainCommand.Null:
            {
                //   _Mensaje = "Null";
                EndingScan();
                tw.CloseSrc();
                break;
            }

            case TwainCommand.TransferReady:
            {
                _Mensaje = "TransferReady";
                ArrayList pics = tw.TransferPictures();
                EndingScan();
                tw.CloseSrc();

                if (pics.Count == 0)
                {
                    _Mensaje = "NoHojas";
                    return(true);
                }



                // picnumber++; quitar                        //inicializacion de clase para guardar imagen
                Escaner.Escaner objEscaner = new Escaner.Escaner();
                //crea el documento pdf donde se le agregaran hojas
                PdfDocument documento = new PdfDocument();

                for (int i = 0; i < pics.Count; i++)
                {
                    IntPtr img    = (IntPtr)pics[i];      //var que contiene la imagen actual
                    int    picnum = i + 1;
                    objEscaner.GuardaImagen(img, _dir, _nombre + picnum.ToString());

                    //codigo para crear pdf
                    PdfPage   hoja = documento.AddPage();
                    XGraphics gfx  = XGraphics.FromPdfPage(hoja);
                    XImage    jpg  = XImage.FromFile(_dir + _nombre + picnum.ToString() + ".jpg");
                    gfx.DrawImage(jpg, 0, 0, hoja.Width, hoja.Height);

                    jpg.Dispose();            //para poder eliminar la foto
                    File.Delete(_dir + _nombre + picnum.ToString() + ".jpg");
                    //fin codigo para grear PDF
                }
                //ya con todas las paginas agregadas cuarda el archivo pdf;
                if (documento.PageCount > 0)
                {
                    documento.Save(_dir + _nombre + ".pdf");
                }
                documento.Dispose();

                if (_destino == @"\")
                {
                    _destino = _dir;
                }

                File.Move(_dir + _nombre + ".pdf", _destino + _nombre + ".pdf");
                _Mensaje = "TransferReadyFin";
                return(true);
            }
            }

            return(true);
        }
Exemplo n.º 29
0
        /// <summary>
        /// 系统消息处理函数
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="handled"></param>
        private void MessageFilter(ref System.Windows.Interop.MSG msg, ref bool handled)
        {
            //1:让TWAIN进行消息处理
            TwainCommand cmd = twSession.PassMessageWPF(ref msg);

            messageLogger.Debug("TWAIN 信息 = " + cmd + ":" + DateTime.Now);

            if (cmd == TwainCommand.Not || cmd == TwainCommand.Null) // 非数据源消息
            {
                handled = false;                                     // 消息未处理,由窗口过程处理
                return;
            }

            //2:进行消息处理
            switch (cmd)
            {
            case TwainCommand.CloseRequest:
            {
                configFlag = false;
                EndingScan();
                twSession.DisableDS();
                break;
            }

            case TwainCommand.CloseOk:
            {
                configFlag = true;
                EndingScan();
                twSession.DisableDS();
                break;
            }

            case TwainCommand.DeviceEvent:
            {
                break;
            }

            //扫描仪传输就绪函数
            case TwainCommand.TransferReady:
            {
                loadAndUnloadLogger.Debug("TWAIN 信息 = TransferReady " + DateTime.Now);
                ScanImageViewer scanViewer = null;
                try
                {
                    if (ShowProgress)
                    {
                        //方式一:启动文件传输,弹出进度提示对话框
                        //获得文件格式
                        CapInfo capInfo;
                        capInfo = twSession.GetScannerCap(TwCap.ICAP_IMAGEFILEFORMAT);
                        string       intStr     = capInfo.CurrentIntStr;
                        string       enumStr    = twSession.ConvertIntStringToEnumString(TwCap.ICAP_IMAGEFILEFORMAT, intStr);
                        TwFileFormat fileFormat = (TwFileFormat)Enum.Parse(typeof(TwFileFormat), enumStr);

                        //获取分辨率
                        capInfo = twSession.GetScannerCap(TwCap.ICAP_XRESOLUTION);
                        Int32 resolution = Int32.Parse(capInfo.CurrentIntStr);

                        // 获得空白页检测阈值
                        capInfo = twSession.GetScannerCap(TwCap.ICAP_AUTODISCARDBLANKPAGES);
                        intStr  = capInfo.CurrentIntStr;
                        int blankImageSizeThreshold = 0;
                        if (!Int32.TryParse(intStr, out blankImageSizeThreshold))
                        {
                            blankImageSizeThreshold = 0;
                        }
                        scanViewer = new ScanImageViewer(twSession, userConfig.MaxSize, blankImageSizeThreshold, resolution, fileFormat); //生成显示控件对象
                        scanViewer.StartTransfer();                                                                                       //开始图像传输
                        scanViewer.ShowDialog();                                                                                          //展示显示控件
                        scanResultList = scanViewer.ScanFileNames;                                                                        //获取文件传输过程中的列表
                    }
                    else
                    {
                        //方式二:设置鼠标为等待状态,在相同线程中启动文件传输
                        Mouse.OverrideCursor = Cursors.Wait;
                        TransferFiles();
                    }
                    //发出扫描完成事件
                    logger.Debug("扫描完成 :" + DateTime.Now);
                    RoutedEventArgs scanArgs = new RoutedEventArgs(ScanCompletedEvent);
                    RaiseEvent(scanArgs);
                    break;
                }
                catch (Exception e)
                {
                    logger.Error("在MessageFilter中发生异常,异常信息; " + e.Message);
                }
                finally
                {
                    if (scanViewer != null)
                    {
                        logger.Debug("关闭扫描仪。");
                        scanViewer.Close();
                    }
                    //结束扫描
                    logger.Debug("结束扫描,使DS变为非使能状态。");
                    EndingScan();
                    twSession.DisableDS();
                    if (!ShowProgress)
                    {
                        //将鼠标复位
                        Mouse.OverrideCursor = null;
                    }
                }
                break;
            }

            default:
                break;
            }
            handled = true;
        }