Ejemplo n.º 1
0
         public void OpenDicom( ref bool IsCroping, ref int _Idx, string fileName, bool IsOnlyImg, bool CanCreatePicBox)
         {
             //Stopwatch _sw = new Stopwatch();
             try
             {
                 _DicomMedicalViewer._medicalViewer.BeginUpdate();
                 pnlScheduled.Enabled = false;
                 //_sw.Start();
                
                 #region Chế độ Demo
                 if (_AppMode == AppType.AppEnum.AppMode.Demo && !File.Exists(fileName))
                 {
                     ScheduledControl _selected = GetSelectedScheduled();
                     bool _isSelected = true;
                     string RAWFilePath = "";
                     //Tạo các thư mục theo cấp ngày\Bệnh nhân(Mã bệnh nhân _ Tên Bệnh nhân _ Tuổi)
                     if (!Directory.Exists(txtImgDir.Text + @"\" + SubDirLv1())) Directory.CreateDirectory(txtImgDir.Text + @"\" + SubDirLv1());
                     if (!Directory.Exists(txtImgDir.Text + @"\" + SubDirLv1() + @"\" + SubDirLv2_Patient())) Directory.CreateDirectory(txtImgDir.Text + @"\" + SubDirLv1() + @"\" + SubDirLv2_Patient());
                     //Kiểm tra nếu chưa chọn thủ tục nào thì cần lưu ảnh thành tên file theo định dạng
                     //YYYY_MM_DD_HH_mm_ss
                     if (_selected == null || RAWFileNameWillbeCreated == "NONE_SELECTED")
                     {
                         _isSelected = false;
                         RAWFileNameWillbeCreated = "NONE_SELECTED_" + Utility.GetYYYYMMDDHHMMSS(DateTime.Now);
                         if (!Directory.Exists(txtImgDir.Text + @"\" + SubDirLv1() + @"\" + SubDirLv2_Patient() + @"\NONE_SELECTED")) Directory.CreateDirectory(txtImgDir.Text + @"\" + SubDirLv1() + @"\" + SubDirLv2_Patient() + @"\NONE_SELECTED");
                         RAWFilePath = txtImgDir.Text + @"\" + SubDirLv1() + @"\" + SubDirLv2_Patient() + @"\NONE_SELECTED\" + RAWFileNameWillbeCreated + ".RAW";
                     }
                     else
                         RAWFilePath = txtImgDir.Text + @"\" + SubDirLv1() + @"\" + SubDirLv2_Patient() + @"\" + RAWFileNameWillbeCreated + ".RAW";//RAWFileNameWillbeCreated=REGNUM+_Detail_ID+_ACode+_Pcode tại phần click của Scheduled
                     fileName = Application.StartupPath + @"\DemoImg\" + m_strCurrACode + "-" + m_strCurrPCode + ".DCM";
                     string destFile = RAWFilePath.ToUpper().Replace(".RAW", ".DCM");
                     try2MoveFileInDemoMode(fileName, destFile);
                     fileName = destFile;
                     //Tự động cập nhật kết quả đã có hình ảnh
                     AutoUpdateResultAfterCapturingPictureFromModality();
                     AutoUpdatePatientInforInDcmFile_DemoMode(Path.GetDirectoryName(fileName), fileName, txtID2.Text, txtName2.Text, txtAge.Text, Sex,_selected.StudyInstanceUID, _selected.SeriesInstanceUID, _selected.SOPInstanceUID);
                 }
                 #endregion

                 isLoadding = true;
                 //stopToolStripMenuItem1_Click(mnuStop, new EventArgs());
                 TempCrop = IsCroping;
                 int _ww = 0;
                 int _wc = 0;
                 this.Text = MultiLanguage.GetText(globalVariables.DisplayLanguage, "VietBaIT JCS - DROC", "VietBaIT JSC-DROC");
                 if (!IsCroping) FilePath = fileName;
                 bool IsRawFile = false;
                 _images = 1;
                
                     try
                     {
                         #region Xử lý lại ảnh dcm từ ảnh gốc(Raw file)
                         try
                         {
                             if (IsGenDcmFromRaw)
                             {
                                 IsGenDcmFromRaw = false;
                                 string tempf_raw = fileName.ToUpper().Replace(".DCM", ".RAW");

                                 //Kiểm tra xem có ảnh Raw không
                                 if (File.Exists(tempf_raw))
                                 {
                                     //xóa file Dcm đang có
                                     string tempf_dcm = fileName.ToUpper().Replace(".RAW", ".DCM");
                                     try
                                     {
                                         File.Delete(tempf_dcm);
                                     }
                                     catch
                                     {
                                     }
                                     //Gán lại giá trị cho fileName để bước kế tiếp load lại raw file
                                     fileName = tempf_raw;
                                 }

                             }
                         }
                         catch(Exception ex0)
                         {
                             AppLogger.LogAction.LogActions("==>OpenDicom.if (IsGenDcmFromRaw)().Exception occurred at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "." + ex0.ToString());
                         }
                         try
                         {
                             if (Path.GetExtension(fileName).ToUpper().Contains("RAW"))
                             {
                                 if (IsUsingDicomConverter || lblAutoDcmConverter.IsChecked)//Tự động convert thành file Dicom
                                 {
                                     ConvertRaw2DicomFileReprocess(fileName);
                                     AutoUpdateResultAfterCapturingPictureFromModality();
                                     fileName = fileName.ToLower().Replace(".raw", ".dcm");
                                     IsUsingDicomConverter = true;
                                     v_blnHasConvertRawin2DicomFile = true;
                                     //Thực hiện thuật toán xử lý ảnh ở ngay sau bước load ảnh Dicom

                                 }
                                 else
                                 {
                                     new frm_LargeMsgBoxOK(MultiLanguage.GetText(globalVariables.DisplayLanguage, "Thông báo", "Warning"), MultiLanguage.GetText(globalVariables.DisplayLanguage, "Cần vào tab Cấu hình đánh dấu vào mục tự động Convert thành file Dicom", "go to Configuration Tab and check Auto Convert to Dicom File"), MultiLanguage.GetText(globalVariables.DisplayLanguage, "Đã hiểu", "OK"), MultiLanguage.GetText(globalVariables.DisplayLanguage, "Không hiểu", "Cancel")).ShowDialog();
                                 }
                             }
                         }
                         catch (Exception ex01)
                         {
                             AppLogger.LogAction.LogActions("==>OpenDicom.ReGenDcmFromRAWFile().Exception occurred at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "." + ex01.ToString());
                         }
                         #endregion
                         
                         if (File.Exists(fileName))
                         {
                             AppLogger.LogAction.AddLog2List(lstFPD560,DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + fileName + " exists");
                             FreeMemoryCapturedByMedicalviewerCell();
                             using (RasterCodecs _codecs = new RasterCodecs())
                             {
                                 //using (CounterDialog counter = new CounterDialog(this, _codecs))
                                 //{
                                 //    counter.Show(this);
                                 //    counter.Update();
                                 //if (chkLoadIn2Memory.IsChecked==false ) _codecs.Options.Load.DiskMemory = true;

                                 if (_codecs.Options.Load.DiskMemory) SetText(lblUsingMemo, "MEM");
                                 else SetText(lblUsingMemo, "NOTMEM");
                                 using (RasterImage _image = _codecs.Load(fileName))
                                 {

                                     cmdCreateDcmfromRaw.Enabled = true;
                                     //try2FreeImage(orginalImg);
                                    _DicomMedicalViewer.try2FreeImage(ref orginalImg);
                                     // try2FreeOriginalImage();
                                     if (_CurrCell != null)
                                     {
                                         RasterImage img = _CurrCell.Image;
                                         _DicomMedicalViewer.try2FreeImage(ref img);
                                     }
                                     orginalImg = _image.CloneAll();
                                     //Xóa các cell sau khi đã giải phóng bộ nhớ
                                     _DicomMedicalViewer._medicalViewer.Cells.Clear();
                                     //Tạo cell mới
                                     MedicalViewerMultiCell cell = new MedicalViewerMultiCell();
                                     cell.BeginUpdate();
                                     cell.FitImageToCell = true;
                                     cell.Columns = 1;
                                     cell.Rows = 1;
                                     _DicomMedicalViewer.InitializeCell(cell);
                                     _DicomMedicalViewer.CopyPropertiesFromGlobalCell(cell);
                                     _Idx = 0;
                                     if (IsOnlyImg) return;
                                     CurrCellFileName = fileName;
                                     //cell.SetTag(4, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Frame);
                                     cell.SetTag(6, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Scale);
                                     cell.SetTag(2, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.WindowLevelData);
                                     //cell.SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.FieldOfView);
                                     cell.SetTag(0, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.RulerUnit);
                                     DicomDataSet ds = new DicomDataSet();

                                     try
                                     {
                                         ds.Load(fileName, DicomDataSetLoadFlags.LoadAndClose);
                                         CurrentDicomDS = ds;
                                     }
                                     catch (Exception ex) { ds = null; }
                                     //DicomDS.Add(fileName, ds);
                                     if (ds != null)
                                     {

                                         if (IsRawFile)
                                             AutpSetDefaultTagForImgInfor(fileName, ds);
                                         _ww = Convert.ToInt32(GetStringValue(ds, DicomTag.WindowWidth));
                                         _wc = Convert.ToInt32(GetStringValue(ds, DicomTag.WindowCenter));
                                         string ID_Name = GetStringValue(ds, Leadtools.Dicom.DicomTag.PatientID) + " " + GetStringValue(ds, Leadtools.Dicom.DicomTag.PatientName);
                                         string Birthday_Sex_Age = "";
                                         string BD = GetStringValue(ds, Leadtools.Dicom.DicomTag.PatientBirthDate).Trim();
                                         string BT = GetStringValue(ds, Leadtools.Dicom.DicomTag.PatientBirthTime).Trim();
                                         string Sex = TranslateSex(GetStringValue(ds, Leadtools.Dicom.DicomTag.PatientSex).Trim());
                                         string Age = GetStringValue(ds, Leadtools.Dicom.DicomTag.PatientAge).Trim();
                                         ID_Name_Age_Sex = ID_Name + " " + Age + " " + Sex;
                                         if (BD != "") Birthday_Sex_Age += BD;
                                         //if (BT != "") Birthday_Sex_Age += BD;
                                         if (Sex != "") Birthday_Sex_Age += " [" + Sex + "] ";
                                         if (Age != "") Birthday_Sex_Age += Age + " T";
                                         //Mã+Tên BN
                                         cell.SetTag(0, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.UserData, ID_Name);
                                         //Ngày sinh-Giới tính-Tuổi
                                         cell.SetTag(1, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.UserData, Birthday_Sex_Age);
                                         //Các thông tin khác

                                         cell.SetTag(0, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, HospitalName);
                                         cell.SetTag(1, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, DepartmentName);
                                         cell.SetTag(2, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                                     }
                                     else
                                     {
                                         //Mã+Tên BN
                                         cell.SetTag(0, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.UserData, "");
                                         //Ngày sinh-Giới tính-Tuổi
                                         cell.SetTag(1, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.UserData, "");
                                         //Các thông tin khác

                                         cell.SetTag(0, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "");
                                         cell.SetTag(1, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "");
                                         cell.SetTag(2, MedicalViewerTagAlignment.TopRight, MedicalViewerTagType.UserData, "");
                                     }

                                     //Load Annotation
                                     string Anfn = GetAnnPath(fileName);
                                     if (System.IO.File.Exists(Anfn))
                                     {
                                         LoadAnnotation(cell, Anfn, true);
                                     }

                                    _DicomMedicalViewer.ApplyToCell(cell);
                                     cell.ShowTags = lblDisplayTag.IsChecked;
                                     cell.DisplayRulers = (MedicalViewerRulers)cboRuler.SelectedIndex;
                                     AddNewMecicalViewerCell(cell);
                                     int CellCount = _DicomMedicalViewer._medicalViewer.Cells.Count;
                                     _Idx = CellCount - 1;
                                     //Dùng CloneAll() để giải phóng _image

                                     cell.Tag = fileName;
                                     if (GetSelectedScheduled() != null)
                                         cell.TabIndex = GetSelectedScheduled().DETAIL_ID;
                                     else
                                         cell.TabIndex = m_intCurrentDetail_ID <= 0 ? 0 : m_intCurrentDetail_ID;
                                     //v_blnHasConvertRawin2DicomFile=true xảy ra ở 2 sự kiện
                                     //1. Nhận ảnh từ FPD và có tự động chuyển thành file Dicom
                                     //2. Nhận ảnh từ FPD và để dưới dạng file RAW, sau đó tại hàm OpenDicom() nếu chưa có ảnh Dcm sẽ tự động Convert file RAW thành DCM
                                     if (v_blnHasConvertRawin2DicomFile && lblDisplayRaw.ImageIndex != 0)
                                     {
                                             using (Cursor _Cursor = Cursors.WaitCursor)
                                             {
                                                 AppLogger.LogAction.ShowEventStatus(lblFPDStatus,MultiLanguage.GetText(globalVariables.DisplayLanguage, "Đang xử lý ảnh...", "image processing..."));
                                                 AppLogger.LogAction.AddLog2List(lstFPD560,DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + MultiLanguage.GetText(globalVariables.DisplayLanguage, "Đang xử lý ảnh...", "image processing..."));
                                                 _DicomMedicalViewer.ApplyIEConfig(_currDRIEData, _image, lblGridMode.IsChecked, lblAppliedMed.IsChecked);
                                                 AutoApplyWW_WC(_image);
                                                 AppLogger.LogAction.AddLog2List(lstFPD560,DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + MultiLanguage.GetText(globalVariables.DisplayLanguage, "Đã xử lý xong...", "image processed..."));
                                                 AppLogger.LogAction.ShowEventStatus(lblFPDStatus,MultiLanguage.GetText(globalVariables.DisplayLanguage, "Đã xử lý xong...", "image processed..."));

                                                 if ( ((_FPDMode != AppType.AppEnum.FPDMode.Other && chkAutoVFlip1.IsChecked) || (_FPDMode == AppType.AppEnum.FPDMode.Other && ((FPDSeq == 1 && chkAutoVFlip1.IsChecked) || (FPDSeq == 2 && chkAutoVFlip2.IsChecked)))))
                                                 {
                                                     _DicomMedicalViewer.ApplyFilter(_image, new FlipCommand(true));
                                                 }
                                                 if (((_FPDMode != AppType.AppEnum.FPDMode.Other && chkAutoHFlip1.IsChecked) || (_FPDMode == AppType.AppEnum.FPDMode.Other && ((FPDSeq == 1 && chkAutoHFlip1.IsChecked) || (FPDSeq == 2 && chkAutoHFlip2.IsChecked)))))
                                                 {
                                                     _DicomMedicalViewer.ApplyFilter(_image, new FlipCommand(false));
                                                 }
                                                 if (AUTO_FLIPV == 1)
                                                 {
                                                     _DicomMedicalViewer.ApplyFilter(_image, new FlipCommand(true));
                                                 }
                                                 if (AUTO_FLIPH == 1)
                                                 {
                                                     _DicomMedicalViewer.ApplyFilter(_image, new FlipCommand(false));
                                                 }
                                                 AutoDetectRowsAndCols(cell, _image.PageCount);
                                                 cell.Image = _image.CloneAll();
                                                 if (!_DicomMedicalViewer.IsValidCell()) return;
                                                 _DicomMedicalViewer._medicalViewer.Cells.SelectAll(false);
                                                 _DicomMedicalViewer._medicalViewer.VisibleRow = _Idx;
                                                 cell.Selected = true;
                                                 _DicomMedicalViewer._medicalViewer.Invalidate();
                                                 if (AllowAppliedWL)
                                                 {
                                                     _DicomMedicalViewer.SetWindowLevel(cell, WW,WC);
                                                     
                                                 }
                                                 AllowAppliedWL = true;
                                             }

                                             _ww = WW;
                                             _wc = WC;
                                         //Thử tạo thông tin Annotation
                                         //CreateDefaultAnnotationOnImage(_DicomMedicalViewer._medicalViewer, Color.White, Color.Black, HospitalName, DepartmentName, ID_Name_Age_Sex, DateTime.Now.ToString("dd/MM/yyyy"));
                                         SaveImg();
                                         v_blnHasConvertRawin2DicomFile = false;
                                         //Tự động chuyển sang Tab xử lý ảnh
                                         tabCtrlAcq.SelectedTab = tabPageImgTools;
                                         //PlayBeep(5);
                                     }
                                     else
                                     {
                                         AutoDetectRowsAndCols(cell, _image.PageCount);
                                         cell.Image = _image.CloneAll();
                                         //cell.Bounds = cell.GetDisplayedClippedImageRectangle();
                                         if (!_DicomMedicalViewer.IsValidCell()) return;
                                         _DicomMedicalViewer._medicalViewer.Cells.SelectAll(false);
                                         _DicomMedicalViewer._medicalViewer.VisibleRow = _Idx;
                                         cell.Selected = true;
                                         _DicomMedicalViewer._medicalViewer.Invalidate();
                                         if (_AppMode == AppType.AppEnum.AppMode.Demo && FirstExposure)
                                         {
                                             SaveImg();
                                         }
                                     }
                                     //Áp dụng windowLeveling khi thực hiện lưu ảnh.
                                     //AutoApplyWW_WC();
                                     if (_ww != 0)
                                     {
                                         _DicomMedicalViewer.SetWindowLevel(cell,_ww,_wc);

                                         _DicomMedicalViewer._medicalViewer.Invalidate();
                                     }
                                     cell.EndUpdate();
                                     _DicomMedicalViewer._medicalViewer.Invalidate();
                                 }
                                 // }
                             }
                             _DicomMedicalViewer._medicalViewer.EndUpdate();
                             AppLogger.LogAction.ShowEventStatus(lblFPDStatus,MultiLanguage.GetText(globalVariables.DisplayLanguage, "Mời bạn tiếp tục xử lý", "Image Result"));
                         }
                         else
                         {
                             mdlStatic.isDisplayImg = false;
                             if (_AppMode == AppType.AppEnum.AppMode.License || (_AppMode == AppType.AppEnum.AppMode.Demo && _ViewState == AppType.AppEnum.ViewState.Capture))
                                 MessageBox.Show("Không tồn tại file ảnh sau:\n" + fileName);
                         }
                     }
                     catch (Exception ex)
                     {

                         AppLogger.LogAction.LogActions("==>OpenDicom.SecondsException().Exception occurred at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "." + ex.ToString());
                         Utility.ShowMsg("2225 " + ex.ToString() + "\n" + fileName);
                         GC.Collect();
                         isLoadding = false;
                        
                         DeleteCurrentImg(_DicomMedicalViewer._medicalViewer, _DicomMedicalViewer._medicalViewerCellIndex);

                     }
                     finally
                     {
                         isLoadding = false;

                     }
                
                 isLoadding = false;
             }
             catch (Exception ex1)
             {
                 AppLogger.LogAction.LogActions("==>OpenDicom.FirstException().Exception occurred at " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "." + ex1.ToString());
                 AppLogger.LogAction.AddLog2List(lstFPD560,"0002: " + ex1.Message);
             }
             finally
             {
                 _DicomMedicalViewer._medicalViewer.BeginUpdate();
                 pnlScheduled.Enabled = true;
                //_sw.Stop();
                 //AppLogger.LogAction.ShowEventStatus(lblFPDStatus,((decimal)_sw.ElapsedMilliseconds / 1000).ToString());
                 IsUsingDicomConverter = false;
                 lblMemory.Text = getAvailableRAM();
                 //Kiểm tra nếu đang chế độ Crop mà lại chọn mục xem lại ảnh gốc thì cần khôi phục lại chế độ đó
                 if (_DicomMedicalViewer._IsCropping && IsLoadOriginalImage)
                 {
                     cmdAcqCrop_Click(cmdAcqCrop,new EventArgs());//Trạng thái ban đầu
                     cmdAcqCrop_Click(cmdAcqCrop, new EventArgs());//Trạng thái Crop
                 } 
             }
         }