Esempio n. 1
0
        public MainWindow()
        {
            InitializeComponent();
            try
            {
                Loading Loading = new Loading();
                bool?   result  = Loading.ShowDialog();
                if ((bool)result)
                {
                    //Client 電腦名稱
                    HostName = Loading.HostName;
                    //Agencys 載入的機構設定
                    Agencys = Loading.Agencys;
                    //Patients載入的病患 或 沒有
                    Patients = Loading.Patients;

                    if (Patients != null)
                    {
                        mwvm = new MainWindowViewModel(HostName, Agencys, Patients, DateTime.Now);
                    }
                    else
                    {
                        mwvm = new MainWindowViewModel();
                    }
                    DataContext = mwvm;

                    //mwvm.SelectedDate = DateTime.Now;

                    if (dde == null)
                    {
                        dde = new DigiDentalEntities();
                    }

                    if (dbr == null)
                    {
                        dbr = new DBRegistrations();
                    }

                    if (dbi == null)
                    {
                        dbi = new DBImages();
                    }
                }
                else
                {
                    Application.Current.Shutdown();
                }
            }
            catch (Exception ex)
            {
                Error_Log.ErrorMessageOutput(ex.ToString());
                Application.Current.Shutdown();
            }
        }
        bool isSkip = true;  //接ProcessingDialog 回傳值 略過

        private void Button_AutoImport_Click(object sender, RoutedEventArgs e)
        {
            Button btnAutoImport = (Button)sender;

            //先載入原本樣板的圖片
            //如果略過就塞回原圖
            btnAutoImport.Dispatcher.Invoke(() =>
            {
                bool isEverChanged = false;

                btnAutoImport.IsEnabled = false;

                pd     = new ProcessingDialog();
                Task t = Task.Factory.StartNew(() =>
                {
                    pd.Dispatcher.Invoke(() =>
                    {
                        pd.PText                = "圖片偵測中";
                        pd.PIsIndeterminate     = true;
                        pd.ButtonContent        = "跳過";
                        pd.ReturnValueCallback += new ProcessingDialog.ReturnValueDelegate(this.SetReturnValueCallbackFun);

                        pd.Show();
                    });

                    //病患資料夾路徑
                    if (pf == null)
                    {
                        pf = new PatientsFolder(Agencys, Patients, Registration_Date);
                    }

                    //資料表Registrations 處理
                    if (dbr == null)
                    {
                        dbr = new DBRegistrations();
                    }

                    //資料表Images 處理
                    if (dbi == null)
                    {
                        dbi = new DBImages();
                    }

                    //處理載入BitmapImage
                    if (lbi == null)
                    {
                        lbi = new LoadBitmapImage();
                    }

                    //載入TemplateImages
                    if (dbti == null)
                    {
                        dbti = new DBTemplateImages();
                    }

                    tICollection = new ObservableCollection <TemplateImages>();
                    tICollection = dbti.GetTemplateImagesCollection(Agencys, Patients, TemplateItem, tfvm.TemplateImportDate);

                    //default Image[i] in UserControl Templates
                    int Imagei           = 0;
                    int ImageCount       = (int)TemplateItem.Template_ImageCount;
                    int DecodePixelWidth = (int)TemplateItem.Template_DecodePixelWidth;
                    while (Imagei < ImageCount)
                    {
                        pd.PText = "圖片 " + (Imagei + 1) + " 偵測中";

                        //目前處理的Image[i]
                        Image iTarget;

                        TemplateContent.Dispatcher.Invoke(() =>
                        {
                            iTarget = new Image();
                            iTarget = (Image)TemplateContent.FindName("Image" + Imagei);

                            iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/yes.png", UriKind.RelativeOrAbsolute));
                            //iTarget.Source = lbi.SettingBitmapImage(@"/DigiDental;component/Resource/yes.png", DecodePixelWidth);
                        });

                        //set the paramater default
                        bool isChanged = false;
                        bool detecting = true;
                        while (true)
                        {
                            //開始偵測wifi card路徑
                            foreach (string f in Directory.GetFiles(Agencys.Agency_WifiCardPath))
                            {
                                Thread.Sleep(500);

                                string extension   = Path.GetExtension(f).ToUpper();
                                string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                                if (!Directory.Exists(pf.PatientFullFolderPathOriginal))
                                {
                                    Directory.CreateDirectory(pf.PatientFullFolderPathOriginal);
                                }

                                //System.Drawing.RotateFlipType rft = ImageHelper.RotateImageByExifOrientationData(f, pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension, GetImageFormat(extension), true);

                                ////複製原圖到目的Original
                                //File.Copy(f, pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension);

                                // Rotate the image according to EXIF data
                                var bmp = new System.Drawing.Bitmap(f);
                                System.Drawing.RotateFlipType fType = ImageHelper.RotateImageByExifOrientationData(bmp, true);
                                if (fType != System.Drawing.RotateFlipType.RotateNoneFlipNone)
                                {
                                    bmp.Save(pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension, GetImageFormat(extension));
                                }
                                else
                                {
                                    File.Copy(f, pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension);
                                }

                                Registration_ID = dbr.CreateRegistrationsAndGetID(Patients, Registration_Date);

                                string imagePath      = @"\" + pf.PatientFolderPathOriginal + @"\" + newFileName + @"ori" + extension;
                                string imageFileName  = newFileName + @"ori" + extension;
                                string imageSize      = "Original";
                                string imageExtension = extension;
                                //寫資料庫
                                //INSERT Images
                                int imageID   = dbi.InsertImage(imagePath, imageFileName, imageSize, imageExtension, Registration_ID);
                                isEverChanged = true;
                                TemplateContent.Dispatcher.Invoke(() =>
                                {
                                    iTarget = new Image();
                                    iTarget = (Image)TemplateContent.FindName("Image" + Imagei);

                                    //INSERT TemplateImages
                                    dbti.InsertOrUpdateImage(Patients, TemplateItem, tfvm.TemplateImportDate, imageID, imagePath, iTarget.Uid);
                                    iTarget.Source = lbi.SettingBitmapImage(pf.PatientFullFolderPathOriginal + @"\" + newFileName + @"ori" + extension, DecodePixelWidth);
                                    isChanged      = true;
                                });

                                File.Delete(f);
                                detecting = false;
                            }
                            //ProcessingDialog STOP
                            if (isStop)
                            {
                                isStop = false;
                                TemplateContent.Dispatcher.Invoke(() =>
                                {
                                    iTarget          = new Image();
                                    iTarget          = (Image)TemplateContent.FindName("Image" + Imagei);
                                    var findOriImage = from tc in tICollection
                                                       where tc.TemplateImage_Number == Imagei.ToString()
                                                       select tc;
                                    if (findOriImage.Count() > 0)
                                    {
                                        iTarget.Source = lbi.SettingBitmapImage(findOriImage.First().Image_Path, DecodePixelWidth);
                                    }
                                    else
                                    {
                                        iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/no.png", UriKind.RelativeOrAbsolute));
                                        //iTarget.Source = lbi.SettingBitmapImage(@"/DigiDental;component/Resource/key.ico", DecodePixelWidth);
                                    }
                                });
                                return;
                            }
                            else
                            {
                                // import pic OR skip import (NEXT)
                                if (!detecting || !isSkip)
                                {
                                    if (!isChanged)
                                    {
                                        TemplateContent.Dispatcher.Invoke(() =>
                                        {
                                            iTarget          = new Image();
                                            iTarget          = (Image)TemplateContent.FindName("Image" + Imagei);
                                            var findOriImage = from tc in tICollection
                                                               where tc.TemplateImage_Number == Imagei.ToString()
                                                               select tc;
                                            if (findOriImage.Count() > 0)
                                            {
                                                iTarget.Source = lbi.SettingBitmapImage(findOriImage.First().Image_Path, DecodePixelWidth);
                                            }
                                            else
                                            {
                                                iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/no.png", UriKind.RelativeOrAbsolute));
                                                //iTarget.Source = lbi.SettingBitmapImage(@"/DigiDental;component/Resource/key.ico", DecodePixelWidth);
                                            }
                                        });
                                    }
                                    Imagei++;
                                    isSkip = true;
                                    break;
                                }
                            }
                        }
                    }
                }).ContinueWith(cw =>
                {
                    //結束
                    pd.PText = "處理完畢";
                    pd.Close();
                    //委派回傳MainWindow
                    //刷新Registrations 資料
                    //刷新Images 資料
                    if (isEverChanged)
                    {
                        ReturnValueCallback(Registration_ID, Registration_Date);
                    }
                    GC.Collect();

                    btnAutoImport.IsEnabled = true;
                }, TaskScheduler.FromCurrentSynchronizationContext());
            });
        }