bool isSkip = true;  //ProgressDialogIndeterminate 回傳值 略過

        private void Button_AutoImport_Click(object sender, RoutedEventArgs e)
        {
            if (btnAutoImport.IsChecked == true)
            {
                if (PathCheck.IsPathExist(Agencys.Agency_ImagePath) && PathCheck.IsPathExist(Agencys.Agency_WifiCardPath))
                {
                    bool isEverChanged = false;

                    ProgressDialogIndeterminate progressDialogIndeterminate = new ProgressDialogIndeterminate();

                    progressDialogIndeterminate.Dispatcher.Invoke(() =>
                    {
                        progressDialogIndeterminate.PText                = "圖片偵測中";
                        progressDialogIndeterminate.PIsIndeterminate     = true;
                        progressDialogIndeterminate.ButtonContent        = "跳過";
                        progressDialogIndeterminate.ReturnValueCallback += new ProgressDialogIndeterminate.ReturnValueDelegate(SetReturnValueCallbackFun);
                        progressDialogIndeterminate.Show();
                    });

                    Registrations registrations = new Registrations();

                    Task t = Task.Factory.StartNew(() =>
                    {
                        using (var ide = new iDentalEntities())
                        {
                            CreateBitmapImage createBitmapImage = new CreateBitmapImage();

                            ObservableCollection <Templates_Images> Templates_ImagesCollect = tableTemplates_Images.QueryTemplatesImagesImportDateAndReturnFullImagePath(Agencys, Patients, SelectedTemplate, functionTemplateViewModel.SelectedDate);

                            //default Image[i] in UserControl Templates
                            int Imagei = 0;
                            //載入樣板設定
                            int ImageCount       = (int)SelectedTemplate.Template_ImageCount;
                            int DecodePixelWidth = (int)SelectedTemplate.Template_DecodePixelWidth;
                            while (Imagei < ImageCount)
                            {
                                progressDialogIndeterminate.Dispatcher.Invoke(() =>
                                {
                                    progressDialogIndeterminate.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));
                                });

                                //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(1000);

                                        //設定病患資料夾
                                        PatientImageFolderInfo patientImageFolderInfo = PatientFolderSetting.PatientImageFolderSetting(Agencys, Patients.Patient_ID, RegistrationDate);
                                        //檢查是否存在,不存在就新增
                                        PathCheck.CheckPathAndCreate(patientImageFolderInfo.PatientImageFullPath);

                                        string extension     = Path.GetExtension(f).ToUpper();
                                        string newFileName   = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                                        string imageFileName = newFileName + extension;

                                        //判斷圖片是否為正
                                        //不是,旋轉後儲存
                                        //是,直接File.Copy
                                        ImageHelper.RotateImageByExifOrientationData(f, patientImageFolderInfo.PatientImageFullPath + @"\" + newFileName + extension, extension, true);

                                        var queryRegistrations = from r in ide.Registrations
                                                                 where r.Patient_ID == Patients.Patient_ID && r.Registration_Date == RegistrationDate.Date
                                                                 select r;
                                        if (queryRegistrations.Count() > 0)
                                        {
                                            registrations = queryRegistrations.First();
                                        }
                                        else
                                        {
                                            registrations.Patient_ID        = Patients.Patient_ID;
                                            registrations.Registration_Date = RegistrationDate.Date;
                                            ide.Registrations.Add(registrations);
                                            //寫入Registrations
                                            ide.SaveChanges();
                                        }

                                        Images images = new Images()
                                        {
                                            Image_Path      = patientImageFolderInfo.PatientImagePath + @"\" + imageFileName,
                                            Image_FileName  = imageFileName,
                                            Image_Extension = extension
                                        };

                                        registrations.Images.Add(images);
                                        ide.SaveChanges();

                                        //複製完如果刪除發生問題
                                        //嘗試五次每次間隔3秒
                                        int deleteTime = 0;
                                        while (deleteTime < 5)
                                        {
                                            Thread.Sleep(3000);
                                            try
                                            {
                                                File.Delete(f);
                                                deleteTime = 5;
                                            }
                                            catch (Exception ex)
                                            {
                                                deleteTime++;
                                                if (deleteTime == 5)
                                                {
                                                    if (MessageBox.Show("影像搬移中出現問題", "錯誤", MessageBoxButton.OK, MessageBoxImage.Error) == MessageBoxResult.OK)
                                                    {
                                                        isStop = true;
                                                        ErrorLog.ErrorMessageOutput(ex.ToString());
                                                    }
                                                }
                                            }
                                        }

                                        TemplateContent.Dispatcher.Invoke(() =>
                                        {
                                            //INSERT TemplateImages
                                            //寫入資料庫再帶回畫面
                                            string ImageUID = string.Empty;
                                            ImageUID        = tableTemplates_Images.InsertOrUpdateTemplatesImages(Patients, functionTemplateViewModel.SelectedTemplate, functionTemplateViewModel.SelectedDate, images.Image_ID, images.Image_Path, Imagei.ToString());

                                            iTarget        = new Image();
                                            iTarget        = (Image)TemplateContent.FindName("Image" + Imagei);
                                            iTarget.Uid    = ImageUID;
                                            iTarget.Source = createBitmapImage.BitmapImageShow(patientImageFolderInfo.PatientImageFullPath + @"\" + imageFileName, DecodePixelWidth);
                                            isChanged      = true;
                                        });

                                        isEverChanged = true;
                                        //代表以處理完結束這回合的偵測
                                        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 Templates_ImagesCollect
                                                               where tc.Template_Image_Number == Imagei.ToString()
                                                               select tc;
                                            if (findOriImage.Count() > 0)
                                            {
                                                iTarget.Source = createBitmapImage.BitmapImageShow(findOriImage.First().Image_Path, DecodePixelWidth);
                                            }
                                            else
                                            {
                                                //iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/no.png", UriKind.RelativeOrAbsolute));
                                            }
                                        });
                                        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 Templates_ImagesCollect
                                                                       where tc.Template_Image_Number == Imagei.ToString()
                                                                       select tc;
                                                    if (findOriImage.Count() > 0)
                                                    {
                                                        iTarget.Source = createBitmapImage.BitmapImageShow(findOriImage.First().Image_Path, DecodePixelWidth);
                                                    }
                                                    else
                                                    {
                                                        //iTarget.Source = new BitmapImage(new Uri(@"/DigiDental;component/Resource/no.png", UriKind.RelativeOrAbsolute));
                                                    }
                                                });
                                            }
                                            Imagei++;
                                            isSkip = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }).ContinueWith(cw =>
                    {
                        //結束
                        progressDialogIndeterminate.Dispatcher.Invoke(() =>
                        {
                            progressDialogIndeterminate.PText = "處理完畢";
                            progressDialogIndeterminate.Close();
                        });

                        //委派回傳MainWindow
                        //刷新Registrations 資料
                        //刷新Images 資料
                        if (isEverChanged)
                        {
                            ReturnValueCallback(registrations.Registration_Date);
                        }

                        GC.Collect();

                        btnAutoImport.IsChecked = false;
                    }, TaskScheduler.FromCurrentSynchronizationContext());
                }
                else
                {
                    MessageBox.Show("影像路徑或Wifi Card路徑有問題,請至<設定>檢查是否有誤", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
Exemple #2
0
        bool isStop = false; //接ProcessingDialog 回傳值 停止
        private void ToggleButton_AutoImport_Click(object sender, RoutedEventArgs e)
        {
            if (btnAutoImport.IsChecked == true)
            {
                if (PathCheck.IsPathExist(Agencys.Agency_ImagePath) && PathCheck.IsPathExist(Agencys.Agency_WifiCardPath))
                {
                    DateTime RegistrationDate = patientInformationViewModel.ImportDate;

                    ProgressDialogIndeterminate progressDialogIndeterminate = new ProgressDialogIndeterminate();

                    progressDialogIndeterminate.Dispatcher.Invoke(() =>
                    {
                        progressDialogIndeterminate.PText                   = "準備匯入至(" + RegistrationDate.ToString("yyyy/MM/dd") + "),圖片偵測中";
                        progressDialogIndeterminate.PIsIndeterminate        = true;
                        progressDialogIndeterminate.ButtonContentVisibility = Visibility.Hidden;
                        progressDialogIndeterminate.ReturnValueCallback    += new ProgressDialogIndeterminate.ReturnValueDelegate(SetReturnValueCallbackFun);

                        progressDialogIndeterminate.Show();
                    });

                    Registrations registrations = new Registrations();
                    //匯入的圖片數
                    int imageCount = 0;

                    Task task = Task.Factory.StartNew(() =>
                    {
                        using (var ide = new iDentalEntities())
                        {
                            while (true)
                            {
                                //偵測資料夾
                                foreach (string f in Directory.GetFiles(Agencys.Agency_WifiCardPath))
                                {
                                    Thread.Sleep(1000);

                                    //設定病患資料夾
                                    PatientImageFolderInfo patientImageFolderInfo = PatientFolderSetting.PatientImageFolderSetting(Agencys, Patients.Patient_ID, RegistrationDate);
                                    //檢查是否存在,不存在就新增
                                    PathCheck.CheckPathAndCreate(patientImageFolderInfo.PatientImageFullPath);

                                    string extension     = Path.GetExtension(f).ToUpper();
                                    string newFileName   = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                                    string imageFileName = newFileName + extension;

                                    //判斷圖片是否為正
                                    //不是,旋轉後儲存
                                    //是,直接File.Copy
                                    ImageHelper.RotateImageByExifOrientationData(f, patientImageFolderInfo.PatientImageFullPath + @"\" + newFileName + extension, extension, true);

                                    var queryRegistrations = from r in ide.Registrations
                                                             where r.Patient_ID == Patients.Patient_ID && r.Registration_Date == RegistrationDate.Date
                                                             select r;
                                    if (queryRegistrations.Count() > 0)
                                    {
                                        registrations = queryRegistrations.First();
                                    }
                                    else
                                    {
                                        registrations.Patient_ID        = Patients.Patient_ID;
                                        registrations.Registration_Date = RegistrationDate.Date;
                                        ide.Registrations.Add(registrations);
                                        //寫入Registrations
                                        ide.SaveChanges();
                                    }

                                    //複製完如果刪除發生問題
                                    //嘗試五次每次間隔3秒
                                    int deleteTime = 0;
                                    while (deleteTime < 5)
                                    {
                                        Thread.Sleep(3000);
                                        try
                                        {
                                            File.Delete(f);
                                            deleteTime = 5;
                                        }
                                        catch (Exception ex)
                                        {
                                            deleteTime++;
                                            if (deleteTime == 5)
                                            {
                                                if (MessageBox.Show("影像搬移中出現問題", "錯誤", MessageBoxButton.OK, MessageBoxImage.Error) == MessageBoxResult.OK)
                                                {
                                                    isStop = true;
                                                    ErrorLog.ErrorMessageOutput(ex.ToString());
                                                }
                                            }
                                        }
                                    }

                                    Images images = new Images()
                                    {
                                        Image_Path      = patientImageFolderInfo.PatientImagePath + @"\" + imageFileName,
                                        Image_FileName  = imageFileName,
                                        Image_Extension = extension
                                    };
                                    registrations.Images.Add(images);
                                    ide.SaveChanges();

                                    //已匯入
                                    imageCount++;
                                    progressDialogIndeterminate.Dispatcher.Invoke(() =>
                                    {
                                        progressDialogIndeterminate.PText = "圖片匯入中,已匯入" + imageCount + "張";
                                    });

                                    Thread.Sleep(200);
                                }
                                //按停止
                                if (isStop)
                                {
                                    isStop = false;
                                    return;
                                }
                            }
                        }
                    }).ContinueWith(cw =>
                    {
                        progressDialogIndeterminate.Dispatcher.Invoke(() =>
                        {
                            progressDialogIndeterminate.PText = "處理完畢";
                            progressDialogIndeterminate.Close();
                        });

                        btnAutoImport.IsChecked = false;

                        if (imageCount > 0)
                        {
                            ReloadOrAddImage(registrations, RegistrationDate);
                            //patientInformationViewModel.RegistrationSetting();
                            //patientInformationViewModel.ComboBoxItemInfo = patientInformationViewModel.RegistrationsList.Where(w => w.SelectedValue.Equals(registrations.Registration_ID)).First();
                        }

                        GC.Collect();
                    }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
                }
                else
                {
                    MessageBox.Show("影像路徑或Wifi Card路徑有問題,請至<設定>檢查是否有誤", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }