コード例 #1
0
        /// <summary>
        /// Captura a imagem da câmera digital.
        /// </summary>
        /// <param name="imageInfo">Imagem a ser capturada.</param>
        /// <param name="outputBitmap">Bitmap de saída.</param>
        /// <returns>True, se foi possível capturar a imagem e false, caso contrário.</returns>
        public bool GetPicture(WIAImageInfo imageInfo, out Bitmap outputBitmap)
        {
            // cria common dialog
            WIA.CommonDialogClass dlg = new WIA.CommonDialogClass();
            bool ret = false;

            // inicializa bitmap
            outputBitmap = null;

            try
            {
                // transfere imagem
                WIA.ImageFile imageFile = (ImageFile)dlg.ShowTransfer(imageInfo.WIAItem, FormatID.wiaFormatJPEG, false);

                // captura os dados binários da imagem
                System.IO.MemoryStream memStream = new System.IO.MemoryStream((byte[])imageFile.FileData.get_BinaryData());

                // cria bitmap em memória
                outputBitmap = new Bitmap(memStream);

                ret = true;
            }
            catch
            {
                ret = false;
            }

            // valor de retorno
            return(ret);
        }
コード例 #2
0
        /// <summary>
        /// Seleciona a impressora a ser utilziada para digitalizar um documento
        /// O usuário deve escolher onde salvar a imagem
        /// </summary>
        /// <param name="dialog"></param>
        public static void ScanningToDisk()
        {
            ////http://www.andrealveslima.com.br/blog/index.php/2015/12/16/como-escanear-documentos-com-o-c-digitalizacao-de-documentos/
            CommonDialogClass dialog = new WIA.CommonDialogClass();

            WIA.Device scanner = dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, true, false);
            if (scanner != null)
            {
                dialog.ShowAcquisitionWizard(scanner);
            }
        }
コード例 #3
0
ファイル: ScanAuto.cs プロジェクト: youthjoy/cshelper
        public ScanAuto()
        {
            DefaultScaner = XmlHelper.GetConfig("DefaultScan");
            LocalSavePath = XmlHelper.GetConfig("LocalPath");
            DeviceManagerClass manager = new DeviceManagerClass();
            Device WiaDev = null;
            CommonDialogClass devCdc = new WIA.CommonDialogClass();
            if (string.IsNullOrEmpty(DefaultScaner))
            {
                WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                if (WiaDev != null)
                {
                    XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID);
                }
            }
            else
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    if (info.DeviceID == DefaultScaner)
                    {

                        WiaDev = info.Connect();
                        break;
                    }
                }
            }
            manager.RegisterEvent("{0C5E2143-FD9B-490B-9AD5-7637A403566B}", WiaDev.DeviceID);

            manager.OnEvent += (eventID, deviceID, itemID) =>
            {
                Item item = WiaDev.Items[1];

                CommonDialogClass cdc = new WIA.CommonDialogClass();
                ImageFile imageFile = cdc.ShowTransfer(item,
                "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}",
                true) as ImageFile;

                if (imageFile != null)
                {
                    var buffer = imageFile.FileData.get_BinaryData() as byte[];
                    //imageFile.SaveFile(Path.Combine(LocalSavePath, DateTime.Now.Millisecond + ".jpg"));
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(buffer, 0, buffer.Length);
                        imgs.Add(System.Drawing.Image.FromStream(ms));
                    }
                }
            };
        }
コード例 #4
0
        public WIA.ImageFile ScanAndSaveOnePage()
        {
            WIA.CommonDialog  Dialog1        = new WIA.CommonDialogClass();
            WIA.DeviceManager DeviceManager1 = new WIA.DeviceManagerClass();
            System.Object     Object1        = null;
            System.Object     Object2        = null;
            WIA.Device        Scanner        = null;

            try
            {
                Scanner = Dialog1.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);
            }
            catch
            {
                MessageBox.Show("请确认是否联系设备");
                return(null);

                throw;
            }
            WIA.Item Item1 = Scanner.Items[1];
            setItem(Item1, "4104", 24);
            setItem(Item1, "6146", 2);
            setItem(Item1, "6147", 150);
            setItem(Item1, "6148", 150);
            setItem(Item1, "6151", 150 * 8.5);
            setItem(Item1, "6152", 150 * 11);

            WIA.ImageFile    Image1        = new WIA.ImageFile();
            WIA.ImageProcess ImageProcess1 = new WIA.ImageProcess();
            Object1 = (Object)"Convert";
            ImageProcess1.Filters.Add(ImageProcess1.FilterInfos.get_Item(ref Object1).FilterID, 0);

            Object1 = (Object)"FormatID";
            Object2 = (Object)WIA.FormatID.wiaFormatBMP;
            ImageProcess1.Filters[1].Properties.get_Item(ref Object1).set_Value(ref Object2);

            Object1 = null;
            Object2 = null;

            Image1 = (WIA.ImageFile)Item1.Transfer(WIA.FormatID.wiaFormatBMP);

            return(Image1);

            //string DestImagePath = @"C:\test.bmp";
            //File.Delete(DestImagePath);
            //Image1.SaveFile(DestImagePath);
        }
コード例 #5
0
        /// <summary>
        /// 文件扫描函数
        /// </summary>
        /// <param name="byteImage">输出图片二进制字节流</param>
        /// <param name="x">扫描起始点x轴</param>
        /// <param name="y">扫描起始点y轴</param>
        /// <param name="w">扫描范围宽度</param>
        /// <param name="h">扫描范围高度</param>
        /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns>
        public bool ScanFile(out byte[] byteImage, int x, int y, int w, int h)
        {
            byteImage = null;
            DeviceManager manager = new DeviceManagerClass();
            Device        device  = null;

            foreach (DeviceInfo info in manager.DeviceInfos)
            {
                if (info.Type != WiaDeviceType.ScannerDeviceType)
                {
                    continue;
                }

                device = info.Connect();

                break;
            }
            Item item = device.Items[1];

            CommonDialogClass cdc       = new WIA.CommonDialogClass();
            ImageFile         imageFile = cdc.ShowTransfer(item,
                                                           "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}",
                                                           true) as ImageFile;

            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                using (System.IO.MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp";
                    Image.FromStream(ms).Save(strCurpath, ImageFormat.Bmp);
                    Image img = Clip((Bitmap)Image.FromStream(ms), x, y, w, h);
                    img.Save(strCurpath, ImageFormat.Bmp);
                    byteImage = System.IO.File.ReadAllBytes(strCurpath);
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }
コード例 #6
0
        private void btnScan_Click(object sender, EventArgs e)
        {
            ImageFile         imageFile = null;
            CommonDialogClass cdc       = new WIA.CommonDialogClass();

            try
            {
                imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
                                                 WIA.WiaImageIntent.TextIntent,
                                                 WIA.WiaImageBias.MaximizeQuality,
                                                 "{00000000-0000-0000-0000-000000000000}",
                                                 false,
                                                 false,
                                                 false);
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                imageFile = null;
            }
            if (imageFile != null)
            {
                // SaveFileDialog s = new SaveFileDialog();
                //s.ShowDialog();
                // imageFile.SaveFile(s.FileName);
                //using (FileStream stream = new FileStream(s.FileName, FileMode.Open,
                //    FileAccess.Read, FileShare.Read))
                //{
                // pictureBox1.Image = Image.FromFile(s.FileName);
                // }
            }


            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    pictureBox1.Image = Image.FromStream(ms);
                }
            }
        }
コード例 #7
0
ファイル: ScanHelper.cs プロジェクト: youthjoy/cshelper
        public List<Image> StartScan()
        {
            imgs = null;
            imgs = new List<Image>();

            //var tempPath=Path.GetTempPath();

            ImageFile imageFile;
            DeviceManagerClass manager = new DeviceManagerClass();
            Device WiaDev = null;
            CommonDialogClass devCdc = new WIA.CommonDialogClass();
            if (string.IsNullOrEmpty(DefaultScaner))
            {
                WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                if (WiaDev != null)
                {
                    XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID);
                }
            }
            else
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    if (info.DeviceID == DefaultScaner)
                    {

                        WiaDev = info.Connect();
                        break;
                    }
                }
            }

            if (WiaDev == null)
            {
                try
                {
                    WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                    if (WiaDev != null)
                    {
                        XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID); ;
                    }
                    else
                    {
                        throw new Exception("请确认扫描仪是否正常连接!");
                        //MessageBox.Show("请确认扫描仪是否正常连接!");
                        //return null;
                    }
                }
                catch
                {
                    return null;
                }

            }

            Property documentHandlingSelect1 = null;

            foreach (Property prop in WiaDev.Properties)
            {
                if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                {
                    documentHandlingSelect1 = prop;
                    object obj = new object();
                    //obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);

                    obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);
                    documentHandlingSelect1.set_Value(ref obj);

                }
                //else if (prop.PropertyID == 3013)
                //{
                //    object val = 1;
                //    prop.set_Value(ref val);
                //}
                //Pages
                else if (prop.PropertyID == 3096)
                {
                    object val = 1;
                    prop.set_Value(ref val);
                }
                else if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                {
                    documentHandlingSelect1 = prop;

                }
            }
            int dpi = 200;

            Item item = WiaDev.Items[1];
            foreach (WIA.Property itemProperty in item.Properties)
            {
                //IProperty tempProperty;
                Object tempNewProperty;

                if (itemProperty.Name.Equals("Horizontal Resolution"))
                {
                    tempNewProperty = dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                else if (itemProperty.Name.Equals("Vertical Resolution"))
                {
                    tempNewProperty = dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }//Colour intent
                else if (itemProperty.PropertyID == 6146)
                {
                    object val2 = 4;
                    ((IProperty)itemProperty).set_Value(ref val2);
                }
                //else if (itemProperty.PropertyID == 4110)
                //{
                //    object val2 = 1100;
                //    ((IProperty)itemProperty).set_Value(ref val2);
                //}
                //else if (itemProperty.PropertyID == 4120)
                //{
                //    object val1 = 2;
                //    ((IProperty)itemProperty).set_Value(ref val1);
                //}
                //else if (itemProperty.PropertyID == 4102)
                //{
                //    object val1 = 1;

                //    ((IProperty)itemProperty).set_Value(ref val1);
                //}
                //4104 _ Bits Per Pixel
                //else if (itemProperty.PropertyID == 4112)
                //{
                //    object val1 = 2481;

                //    ((IProperty)itemProperty).set_Value(ref val1);
                //}
                else if (itemProperty.Name.Equals("Horizontal Extent"))
                {
                    tempNewProperty = 8.27 * dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                else if (itemProperty.Name.Equals("Vertical Extent"))
                {
                    tempNewProperty = 11.69 * dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
            }

            CommonDialogClass cdc = new WIA.CommonDialogClass();
            try
            {
                imageFile = cdc.ShowTransfer(item,wiaFormatJPEG, false) as ImageFile;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                imageFile = null;

            }

            while (imageFile != null)
            {

                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                //imageFile.SaveFile(Path.Combine(LocalSavePath, DateTime.Now.Millisecond + ".bmp"));
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    imgs.Add(System.Drawing.Image.FromStream(ms));
                    ms.Close();
                    ms.Dispose();
                }
                buffer = null;
                imageFile = null;
                //MethodInvoker mm = delegate
                //{
                //    try
                //    {
                //        var tmp = Path.GetTempPath();
                //        foreach (string d in Directory.GetFileSystemEntries(tmp))
                //        {
                //            if (d.Contains("img") && File.Exists(d))
                //            {
                //                FileInfo fi = new FileInfo(d);
                //                if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                //                    fi.Attributes = FileAttributes.Normal;
                //                File.Delete(d);//直接删除其中的文件
                //            }
                //        }
                //    }
                //    catch (Exception ex)
                //    {

                //    }
                //};

                //mm.BeginInvoke(null, null);
                try
                {
                    imageFile = cdc.ShowTransfer(item, wiaFormatJPEG, false) as ImageFile;
                }
                catch (Exception ex)
                {

                    continue;
                    //return imgs;
                    //throw new Exception(ex.Message);
                }
            }
            return imgs.ToList();
        }
コード例 #8
0
        private void btnAutoScan_Click(object sender, EventArgs e)
        {
            DeviceManager manager = new DeviceManagerClass();
            Device        device  = null;

            foreach (DeviceInfo info in manager.DeviceInfos)
            {
                if (info.Type != WiaDeviceType.ScannerDeviceType)
                {
                    continue;
                }
                device = info.Connect();
                break;
            }
            Item item = device.Items[1];
            //string s = "";
            //for (int i = 1; i < item.Properties.Count - 1; i++)
            //{
            //    object o = (int)i;
            //    Property p = item.Properties.get_Item(ref o);

            //    s += p.Name + "----" + p.PropertyID + "----" + Convert.ToString(p.get_Value()) + "\r\n";
            //}



            int       dpi   = (int)numericUpDown1.Value;
            ScanColor color = (ScanColor)this._colors[this.comboBox1.SelectedIndex];
            object    obj   = (int)color;
            object    obj2  = "6146";//WiaImageIntent

            setItem(item, obj2, obj);

            object obj3 = dpi;
            object obj4 = "6147";

            setItem(item, obj4, obj3);
            object obj5 = dpi;
            object obj6 = "6148";

            setItem(item, obj6, obj5);

            setItem(item, "6151", 800);
            setItem(item, "6152", 100 * 11);



            CommonDialogClass cdc       = new WIA.CommonDialogClass();
            ImageFile         imageFile = cdc.ShowTransfer(item,
                                                           "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}",
                                                           true) as ImageFile;

            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    pictureBox1.Image = Image.FromStream(ms);
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// 扫描文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnImageMagnify_Click(object sender, EventArgs e)
        {
            try
            {
                DeviceManager manager = new DeviceManagerClass();
                Device        device  = null;
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType)
                    {
                        continue;
                    }
                    device = info.Connect();
                    break;
                }
                Item item                   = device.Items[1];
                CommonDialogClass cdc       = new WIA.CommonDialogClass();
                ImageFile         imageFile = null;
                imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
                                                 WIA.WiaImageIntent.TextIntent,
                                                 WIA.WiaImageBias.MaximizeQuality,
                                                 "{00000000-0000-0000-0000-000000000000}",
                                                 true,
                                                 true,
                                                 false);
                if (imageFile != null)
                {
                    var buffer = imageFile.FileData.get_BinaryData() as byte[];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(buffer, 0, buffer.Length);
                        string filePath = Server.MapPath("~/") + AttachPath;  ///文件夹
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        string name = "\\";
                        var    menu = BLL.SysMenuService.GetSysMenuByMenuId(this.MenuId);
                        if (menu != null)
                        {
                            name += menu.MenuName;
                        }
                        name += Funs.GetNewFileName() + ".jpg";
                        string url = filePath + name;
                        if (!string.IsNullOrEmpty(url))
                        {
                            using (FileStream fs = new FileStream(url, FileMode.Create, FileAccess.Write))
                            {
                                ms.WriteTo(fs);
                                string attachUrl = AttachPath + name;
                                if (!string.IsNullOrEmpty(attachUrl))
                                {
                                    attachUrl = attachUrl.Replace('/', '\\');
                                }
                                string           oldSrouce = string.Empty;
                                string           FullPath  = string.Empty;
                                Model.AttachFile att       = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.ToKeyId);
                                if (att != null && !string.IsNullOrEmpty(att.AttachUrl))
                                {
                                    FullPath  = att.AttachUrl + "," + attachUrl;
                                    oldSrouce = att.AttachSource;
                                }
                                else
                                {
                                    FullPath = attachUrl;
                                }
                                string source = BLL.UploadFileService.GetSourceByAttachUrl(attachUrl, buffer.Length, oldSrouce);
                                //this.SaveData(source, FullPath); ///保存方法
                                Session[sessionName] = JArray.Parse(source);
                            }

                            this.BindGrid();
                            ShowNotify("扫描完成!", MessageBoxIcon.Success);
                        }
                    }
                }
            }
            catch
            {
                ShowNotify("请检查扫描仪是否连接正确!", MessageBoxIcon.Warning);
            }
        }
コード例 #10
0
ファイル: ScanAuto.cs プロジェクト: youthjoy/cshelper
        public List<Image> StartScan()
        {
            ImageFile imageFile;
            DeviceManagerClass manager = new DeviceManagerClass();
            Device WiaDev = null;
            CommonDialogClass devCdc = new WIA.CommonDialogClass();

            if (string.IsNullOrEmpty(DefaultScaner))
            {
                WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                if (WiaDev != null)
                {
                    XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID);
                }
            }
            else
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    if (info.DeviceID == DefaultScaner)
                    {

                        WiaDev = info.Connect();
                        break;
                    }
                }
            }

            if (WiaDev == null)
            {
                try
                {
                    WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                    if (WiaDev != null)
                    {
                        XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID); ;
                    }
                    else
                    {
                        throw new Exception("请确认扫描仪是否正常连接!");
                        //MessageBox.Show("请确认扫描仪是否正常连接!");
                        //return null;
                    }
                }
                catch
                {
                    return null;
                }

            }

            Property documentHandlingSelect1 = null;

            foreach (Property prop in WiaDev.Properties)
            {
                if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                {
                    documentHandlingSelect1 = prop;
                    object obj = new object();
                    //obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);

                    obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);
                    documentHandlingSelect1.set_Value(ref obj);

                }
                //else if (prop.PropertyID == 3013)
                //{
                //    object val = 1;
                //    prop.set_Value(ref val);
                //}
                //Pages
                else if (prop.PropertyID == 3096)
                {
                    object val = 1;
                    prop.set_Value(ref val);
                }
                else if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                {
                    documentHandlingSelect1 = prop;

                }
            }

            Item item = WiaDev.Items[1];
            foreach (WIA.Property itemProperty in item.Properties)
            {
                //IProperty tempProperty;
                Object tempNewProperty;

                if (itemProperty.Name.Equals("Horizontal Resolution"))
                {
                    tempNewProperty = 100;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                else if (itemProperty.Name.Equals("Vertical Resolution"))
                {
                    tempNewProperty = 100;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                //else if (itemProperty.Name.Equals("Horizontal Extent"))
                //{
                //    //tempNewProperty = 619;
                //    //((IProperty)itemProperty).set_Value(ref tempNewProperty);
                //}
                //else if (itemProperty.Name.Equals("Vertical Extent"))
                //{
                //    //tempNewProperty = 876;
                //    //((IProperty)itemProperty).set_Value(ref tempNewProperty);
                //}
            }

            CommonDialogClass cdc = new WIA.CommonDialogClass();
            try
            {
                imageFile = cdc.ShowTransfer(item, wiaFormatJPEG, false) as ImageFile;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                imageFile = null;

            }

            while (imageFile != null)
            {

                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                //imageFile.SaveFile(Path.Combine(LocalSavePath, DateTime.Now.Millisecond + ".jpg"));
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    imgs.Add(System.Drawing.Image.FromStream(ms));

                }
                imageFile = null;

                try
                {
                    imageFile = cdc.ShowTransfer(item, wiaFormatJPEG, false) as ImageFile;
                }
                catch (Exception ex)
                {

                    continue;
                    //return imgs;
                    //throw new Exception(ex.Message);
                }
            }
            return imgs;
        }
コード例 #11
0
        /// <summary>
        /// 自动扫描文件
        /// </summary>
        /// <param name="byteImage">输出图片二进制字节流</param>
        /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns>
        public bool AutoScanFile(out byte[] byteImage)
        {
            byteImage = null;
            int           xLeft   = 0;
            int           xRight  = 0;
            int           yTotp   = 0;
            int           yBotom  = 0;
            DeviceManager manager = new DeviceManagerClass();
            Device        device  = null;

            foreach (DeviceInfo info in manager.DeviceInfos)
            {
                if (info.Type != WiaDeviceType.ScannerDeviceType)
                {
                    continue;
                }

                device = info.Connect();

                break;
            }
            Item item = device.Items[1];

            CommonDialogClass cdc       = new WIA.CommonDialogClass();
            ImageFile         imageFile = cdc.ShowTransfer(item,
                                                           "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}",
                                                           true) as ImageFile;

            System.IO.MemoryStream ms = new MemoryStream();
            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];

                ms.Write(buffer, 0, buffer.Length);
            }
            else
            {
                return(false);
            }

            Color c1 = new Color();
            Color c2 = new Color();
            Color c3 = new Color();
            Color c4 = new Color();
            Color c5 = new Color();
            Color c6 = new Color();
            Color c7 = new Color();
            Color c8 = new Color();
            Color c9 = new Color();

            int rr, r1, r2, r3, r4, r5, r6, r7, r8, r9, fxr, i, j;
            // int g1, g2, g3, g4, fxg, fyg, b1, b2, b3, b4, fxb, fyb;
            Bitmap imgTem = (Bitmap)Bitmap.FromStream(ms);

            for (i = 1; i < imgTem.Width - 2; i++)
            {
                for (j = 1; j < imgTem.Height - 2; j++)
                {
                    c1  = imgTem.GetPixel(i, j - 1);
                    c2  = imgTem.GetPixel(i - 1, j);
                    c3  = imgTem.GetPixel(i, j);
                    c4  = imgTem.GetPixel(i + 1, j);
                    c5  = imgTem.GetPixel(i, j + 1);
                    c6  = imgTem.GetPixel(i - 1, j - 1);
                    c7  = imgTem.GetPixel(i - 1, j + 1);
                    c8  = imgTem.GetPixel(i + 1, j - 1);
                    c9  = imgTem.GetPixel(i + 1, j + 1);
                    r1  = c1.R;
                    r2  = c2.R;
                    r3  = c3.R;
                    r4  = c4.R;
                    r5  = c5.R;
                    r6  = c6.R;
                    r7  = c7.R;
                    r8  = c8.R;
                    r9  = c9.R;
                    fxr = 8 * r3 - r1 - r2 - r4 - r5 - r6 - r7 - r8 - r9;
                    // fyr = r6 + 2 * r1 + r8 - r7 - 2 * r5 - r9;
                    rr = Math.Abs(fxr);
                    if (rr < 0)
                    {
                        rr = 0;
                    }
                    if (rr > 255)
                    {
                        rr = 255;
                    }
                    Color cc = Color.FromArgb(rr, rr, rr);
                    imgTem.SetPixel(i, j, cc);
                }
            }

            bool  isc = false;
            Color pixel;
            //污点熟练
            int maxc = 5;

            #region 扫描左空白
            for (int x = 1; x < imgTem.Width - 2; x++)
            {
                int c = 0;
                for (int y = 1; y < imgTem.Height - 2; y++)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;

                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        isc = true;
                    }
                }
                if (isc == false)
                {
                    xLeft = x;
                    break;
                }
            }
            #endregion

            #region 扫描右空白
            for (int x = imgTem.Width - 2; x > 2; x--)
            {
                int c = 0;
                for (int y = imgTem.Height - 2; y > 2; y--)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        isc = true;
                    }
                }
                if (isc == false)
                {
                    xRight = x;
                    break;
                }
            }
            #endregion

            #region 扫描底部空白
            for (int y = imgTem.Height - 2; y > 2; y--)
            {
                int c = 0;
                for (int x = imgTem.Width - 2; x > 2; x--)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        //imgTem = Clip(imgTem, 0, 0, x, imgTem.Height);
                        //pictureimgTem.Image = imgTem;
                        //pictureimgTem.Image = imgTem;
                        isc = true;
                    }
                }
                if (isc == false)
                {
                    yBotom = y;
                    break;
                }
            }
            #endregion

            #region 扫描上部空白
            for (int y = 1; y < imgTem.Height - 2; y++)
            {
                int c = 0;
                for (int x = 1; x < imgTem.Width - 2; x++)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;

                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        isc = true;
                    }
                }
                if (isc == false)
                {
                    yTotp = y;
                    break;
                }
            }
            #endregion

            //剪切图片
            imgTem = (Bitmap)Bitmap.FromStream(ms);
            imgTem = Clip(imgTem, xLeft - 5, yTotp - 5, xRight - xLeft + 5, yBotom - yTotp + 5);


            string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp";
            imgTem.Save(strCurpath, ImageFormat.Bmp);
            byteImage = System.IO.File.ReadAllBytes(strCurpath);
            return(true);
        }
コード例 #12
0
        /// <summary>
        /// 自动扫描文件
        /// </summary>
        /// <param name="byteImage">输出图片二进制字节流</param>
        /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns>
        public bool AutoScanFile(out byte[] byteImage)
        {
            byteImage = null;
            int xLeft = 0;
            int xRight = 0;
            int yTotp=0;
            int yBotom=0;
            DeviceManager manager = new DeviceManagerClass();
            Device device = null;
            foreach (DeviceInfo info in manager.DeviceInfos)
            {

                if (info.Type != WiaDeviceType.ScannerDeviceType) continue;

                device = info.Connect();

                break;

            }
            Item item = device.Items[1];

            CommonDialogClass cdc = new WIA.CommonDialogClass();
            ImageFile imageFile = cdc.ShowTransfer(item,
                "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}",
                true) as ImageFile;
            System.IO.MemoryStream ms = new MemoryStream();
            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];

                ms.Write(buffer, 0, buffer.Length);
            }
            else
            {
                return false;
            }

            Color c1 = new Color();
            Color c2 = new Color();
            Color c3 = new Color();
            Color c4 = new Color();
            Color c5 = new Color();
            Color c6 = new Color();
            Color c7 = new Color();
            Color c8 = new Color();
            Color c9 = new Color();

            int rr, r1, r2, r3, r4, r5, r6, r7, r8, r9, fxr, i, j;
            // int g1, g2, g3, g4, fxg, fyg, b1, b2, b3, b4, fxb, fyb;
            Bitmap imgTem = (Bitmap)Bitmap.FromStream(ms);
            for (i = 1; i < imgTem.Width - 2; i++)
            {
                for (j = 1; j < imgTem.Height - 2; j++)
                {
                    c1 = imgTem.GetPixel(i, j - 1);
                    c2 = imgTem.GetPixel(i - 1, j);
                    c3 = imgTem.GetPixel(i, j);
                    c4 = imgTem.GetPixel(i + 1, j);
                    c5 = imgTem.GetPixel(i, j + 1);
                    c6 = imgTem.GetPixel(i - 1, j - 1);
                    c7 = imgTem.GetPixel(i - 1, j + 1);
                    c8 = imgTem.GetPixel(i + 1, j - 1);
                    c9 = imgTem.GetPixel(i + 1, j + 1);
                    r1 = c1.R;
                    r2 = c2.R;
                    r3 = c3.R;
                    r4 = c4.R;
                    r5 = c5.R;
                    r6 = c6.R;
                    r7 = c7.R;
                    r8 = c8.R;
                    r9 = c9.R;
                    fxr = 8 * r3 - r1 - r2 - r4 - r5 - r6 - r7 - r8 - r9;
                    // fyr = r6 + 2 * r1 + r8 - r7 - 2 * r5 - r9;
                    rr = Math.Abs(fxr);
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    Color cc = Color.FromArgb(rr, rr, rr);
                    imgTem.SetPixel(i, j, cc);

                }

            }

            bool isc = false;
            Color pixel;
            //污点熟练
            int maxc = 5;
            #region 扫描左空白
            for (int x = 1; x < imgTem.Width - 2; x++)
            {
                int c = 0;
                for (int y = 1; y < imgTem.Height - 2; y++)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;

                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        isc = true;
                    }

                }
                if (isc == false)
                {

                    xLeft = x;
                    break;
                }
            }
            #endregion

            #region 扫描右空白
            for (int x = imgTem.Width - 2; x > 2; x--)
            {
                int c = 0;
                for (int y = imgTem.Height - 2; y > 2; y--)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        isc = true;
                    }

                }
                if (isc == false)
                {

                    xRight = x;
                    break;
                }
            }
            #endregion

            #region 扫描底部空白
            for (int y = imgTem.Height - 2; y > 2; y--)
            {
                int c = 0;
                for (int x = imgTem.Width - 2; x > 2; x--)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        //imgTem = Clip(imgTem, 0, 0, x, imgTem.Height);
                        //pictureimgTem.Image = imgTem;
                        //pictureimgTem.Image = imgTem;
                        isc = true;
                    }

                }
                if (isc == false)
                {

                    yBotom = y;
                    break;
                }
            }
            #endregion

            #region 扫描上部空白
            for (int y = 1; y < imgTem.Height - 2; y++)
            {
                int c = 0;
                for (int x = 1; x < imgTem.Width - 2; x++)
                {
                    pixel = imgTem.GetPixel(x, y);
                    int r, g, b;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;

                    if (r == 255 && g == 255 && b == 255)
                    {
                        c++;
                        if (c >= maxc)
                        {
                            isc = false;
                            break;
                        }
                    }
                    else
                    {
                        isc = true;
                    }

                }
                if (isc == false)
                {

                    yTotp = y;
                    break;
                }
            }
            #endregion

            //剪切图片
            imgTem = (Bitmap)Bitmap.FromStream(ms);
            imgTem = Clip(imgTem, xLeft - 5, yTotp - 5, xRight - xLeft + 5, yBotom - yTotp + 5);

            string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp";
            imgTem.Save(strCurpath, ImageFormat.Bmp);
            byteImage = System.IO.File.ReadAllBytes(strCurpath);
            return true;
        }
コード例 #13
0
        /// <summary>
        /// 文件扫描函数
        /// </summary>
        /// <param name="byteImage">输出图片二进制字节流</param>
        /// <param name="x">扫描起始点x轴</param>
        /// <param name="y">扫描起始点y轴</param>
        /// <param name="w">扫描范围宽度</param>
        /// <param name="h">扫描范围高度</param>
        /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns>
        public bool ScanFile(out byte[] byteImage, int x, int y, int w, int h)
        {
            byteImage = null;
            DeviceManager manager = new DeviceManagerClass();
            Device device = null;
            foreach (DeviceInfo info in manager.DeviceInfos)
            {

                if (info.Type != WiaDeviceType.ScannerDeviceType) continue;

                device = info.Connect();

                break;

            }
            Item item = device.Items[1];

            CommonDialogClass cdc = new WIA.CommonDialogClass();
            ImageFile imageFile = cdc.ShowTransfer(item,
                "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}",
                true) as ImageFile;

            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                using (System.IO.MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp";
                    Image.FromStream(ms).Save(strCurpath, ImageFormat.Bmp);
                    Image img = Clip((Bitmap)Image.FromStream(ms), x, y, w, h);
                    img.Save(strCurpath, ImageFormat.Bmp);
                    byteImage = System.IO.File.ReadAllBytes(strCurpath);
                    return true;
                }

            }
            else
            {
                return false;
            }
        }