예제 #1
0
        //DICOMファイルの圧縮
        public static bool DicomToComp(string file, string dicomFile, Dictionary <string, string> tags)
        {
            try
            {
                //モダリティ
                string Modality = tags[DcmTag.Modality.Name];

                //圧縮タイプ
                string CompType;
                if (!AppUtil.ModalityCompType.TryGetValue(Modality, out CompType))
                {
                    CompType = AppUtil.CompType;
                }

                //圧縮オプション
                string CompOption;
                if (!AppUtil.ModalityCompOption.TryGetValue(Modality, out CompOption))
                {
                    CompOption = AppUtil.CompOption;
                }

                //ディレクトリ作成
                string dicomDir = Path.GetDirectoryName(dicomFile);
                if (!Directory.Exists(dicomDir))
                {
                    Directory.CreateDirectory(dicomDir);
                }

                //ファイル削除
                if (File.Exists(dicomFile))
                {
                    lock (fileLock)
                    {
                        File.Delete(dicomFile);
                    }
                }

                //圧縮
                if (CompType == "0")
                {
                    //なし
                    return(FileUtil.Copy(file, dicomFile));
                }
                else if (CompType == "2")
                {
                    //MEG
                    var scale = double.Parse(CompOption);
                    DicomToMeg(file, dicomFile, scale);
                    return(true);
                }
                else
                {
                    //転送構文UID
                    string TransferSyntaxUID = tags[DcmTag.TransferSyntaxUID.Name];

                    if (TransferSyntaxUID != "1.2.840.10008.1.2" && TransferSyntaxUID != "1.2.840.10008.1.2.1")
                    {
                        LogUtil.Debug1("TransferSyntaxUID[{0}]", TransferSyntaxUID);
                    }

                    //画素表現
                    string PixelRepresentation = tags[DcmTag.PixelRepresentation.Name];

                    var dcmCtrl = new DicomFile(file);

                    //圧縮対象
                    if (TransferSyntaxUID == "1.2.840.10008.1.2" || TransferSyntaxUID == "1.2.840.10008.1.2.1" || TransferSyntaxUID == "1.2.840.10008.1.2.2")
                    {
                        //JPEG-Lossy(8bit)圧縮
                        if (CompType == "50" && dcmCtrl.EncodeToJpeg("+eb +un", dicomFile))
                        {
                            dcmCtrl = new DicomFile(dicomFile);
                        }
                        //JPEG-Lossy(12bit)圧縮
                        else if (CompType == "51" && dcmCtrl.EncodeToJpeg("+ee +un", dicomFile))
                        {
                            dcmCtrl = new DicomFile(dicomFile);
                        }
                        //JPEG-Lossless圧縮
                        else if (CompType == "70" && dcmCtrl.EncodeToJpeg("+e1", dicomFile))
                        {
                            dcmCtrl = new DicomFile(dicomFile);
                        }
                        //JPEG2000圧縮
                        else if (CompType == "90" || CompType == "91")
                        {
                            string tmpFile = "";
                            try
                            {
                                //Explicit変換
                                if (TransferSyntaxUID != "1.2.840.10008.1.2.1")
                                {
                                    tmpFile = dcmCtrl.FileName + ".tmp";
                                    if (dcmconv("+te", dcmCtrl.FileName, tmpFile))
                                    {
                                        dcmCtrl = new DicomFile(tmpFile);
                                    }
                                }

                                //JPEG2000変換
                                if (!File.Exists(AppUtil.CompExe))
                                {
                                    throw new FileNotFoundException(AppUtil.CompExe + " NOT FOUND");
                                }

                                //EXE引数
                                string compArg;

                                //JPEG2000-Lossless圧縮
                                if (CompType == "90")
                                {
                                    compArg = "\"" + dcmCtrl.FileName + "\" \"" + dicomFile + "\"";
                                }
                                //JPEG2000-Lossy圧縮
                                else
                                {
                                    string ratio;
                                    if (!AppUtil.ModalityCompOption.TryGetValue(Modality, out ratio))
                                    {
                                        ratio = AppUtil.CompOption;
                                    }

                                    string[] ratios = ratio.Split(':');
                                    if (ratios.Length == 1 || (ratios.Length == 2 && ratios[0] == ratios[1]))
                                    {
                                        ratio = ratios[0];
                                    }
                                    else
                                    {
                                        //画素表現により決定
                                        if (PixelRepresentation == "1")
                                        {
                                            ratio = ratios[1];
                                        }
                                        else
                                        {
                                            ratio = ratios[0];
                                        }
                                    }

                                    compArg = "\"" + dcmCtrl.FileName + "\" \"" + dicomFile + "\" " + ratio;
                                }

                                bool moveFlag = true;

                                using (Process p = new Process())
                                {
                                    p.StartInfo.FileName        = AppUtil.CompExe;
                                    p.StartInfo.Arguments       = compArg;
                                    p.StartInfo.UseShellExecute = false;
                                    p.StartInfo.CreateNoWindow  = true;
                                    if (p.Start())
                                    {
                                        if (p.WaitForExit(AppUtil.ExeTimeout))
                                        {
                                            if (File.Exists(dicomFile))
                                            {
                                                dcmCtrl  = new DicomFile(dicomFile);
                                                moveFlag = false;
                                            }
                                            else
                                            {
                                                LogUtil.Warn("CompExe:ExitCode={0}" + p.ExitCode);
                                            }
                                        }
                                        else
                                        {
                                            LogUtil.Warn("CompExe:Timeout");
                                            try
                                            {
                                                p.Kill();
                                                p.WaitForExit();
                                            }
                                            catch { }
                                        }
                                    }
                                    else
                                    {
                                        LogUtil.Warn("CompExe:Process.Start()=false");
                                    }
                                }

                                if (moveFlag)
                                {
                                    for (int i = 0; i < AppUtil.RetryCount; i++)
                                    {
                                        try
                                        {
                                            if (File.Exists(dicomFile))
                                            {
                                                lock (fileLock2)
                                                {
                                                    File.Delete(dicomFile);
                                                }
                                            }
                                            return(FileUtil.Copy(file, dicomFile));
                                        }
                                        catch (Exception ex)
                                        {
                                            LogUtil.Warn(ex);
                                        }

                                        //リトライ
                                        if (i < AppUtil.RetryCount - 1)
                                        {
                                            System.Threading.Thread.Sleep(AppUtil.SleepTime);
                                            LogUtil.Info1("RETRY:{0}", i + 1);
                                        }
                                        else
                                        {
                                            LogUtil.Error1("DicomFile.Move():失敗 [{0}]", dicomFile);
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                if (tmpFile != "")
                                {
                                    FileUtil.Delete(tmpFile, true);
                                }
                            }
                        }
                        else
                        {
                            //無変換
                            return(FileUtil.Copy(file, dicomFile));
                        }
                    }
                    else
                    {
                        //無変換
                        return(FileUtil.Copy(file, dicomFile));
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex.ToString());
                return(false);
            }
        }
예제 #2
0
        //サムネイルの作成
        public static void GetThumbnail(SeriesKey key, out byte[] thumb)
        {
            thumb = null;

            if (key.SOPInstanceUID == null || key.SOPInstanceUID == "")
            {
                using (var pc = new PacsComm())
                {
                    DicomTagsResult ret = pc.CFindImage(key.StudyInstanceUID, key.SeriesInstanceUID);
                    if (!ret.IsSuccess)
                    {
                        LogUtil.Error("CFINDに失敗しました。");
                        return;
                    }

                    var tags = new List <Tuple <string, long> >();

                    foreach (var dcmTag in ret.Tags)
                    {
                        long InstanceNumber = 0;
                        Int64.TryParse(dcmTag.GetTagValue(DicomDic.Find("InstanceNumber").Tag), out InstanceNumber);

                        var tag = Tuple.Create(dcmTag.GetTagValue(DicomDic.Find("SOPInstanceUID").Tag), InstanceNumber);
                        tags.Add(tag);
                    }

                    //ソート
                    tags = tags.OrderBy(n => n.Item2).ToList();

                    if (tags.Count > 0)
                    {
                        key.SOPInstanceUID = tags[0].Item1;
                    }
                }
            }

            if (key.SOPInstanceUID != null && key.SOPInstanceUID != "")
            {
                //DICOMがある
                {
                    LCL.GetImageStorageID(ref key);
                    if (key.StorageID != null && key.StorageID != "")
                    {
                        var sto = DbCacheUtil.GetStorage(key.StorageID);
                        using (var imp = new Impersonate(sto.LogonUsername, sto.LogonPassword))
                        {
                            var file = FileUtil.GetDicomFile(key);
                            if (File.Exists(file))
                            {
                                DicomUtil.DicomToThumb(file, out thumb);
                                if (thumb != null)
                                {
                                    return;
                                }
                            }
                        }
                    }
                }

                //DICOMがない
                using (var pc = new PacsComm())
                {
                    //画像取得
                    DicomFilesResult ret = pc.CMoveImage(key.StudyInstanceUID, key.SeriesInstanceUID, key.SOPInstanceUID);
                    if (!ret.IsSuccess)
                    {
                        LogUtil.Error("CMoveImage");
                        return;
                    }

                    if (ret.Files.Length == 0)
                    {
                        LogUtil.Error("CMoveImage [File=0]");
                        return;
                    }

                    LogUtil.Debug1("CMoveImage [File={0}]", ret.Files.Length.ToString());

                    foreach (DicomFile dcmfile in ret.Files)
                    {
                        var file = dcmfile.FileName.Trim();
                        if (!File.Exists(file))
                        {
                            continue;
                        }

                        DicomUtil.DicomToThumb(file, out thumb);

                        if (thumb != null)
                        {
                            return;
                        }
                    }
                }
            }
        }
예제 #3
0
        public static bool DicomToDB(List <Dictionary <string, string> > data)
        {
            if (data == null || data.Count == 0)
            {
                return(false);
            }

            var  now         = DateTime.Now;
            bool studyInsert = true;

            foreach (var sop in data)
            {
                string studyUid  = sop[DcmTag.StudyInstanceUID.Name];
                string seriesUid = sop[DcmTag.SeriesInstanceUID.Name];
                string sopUid    = sop[DcmTag.SOPInstanceUID.Name];

                for (int i = 0; i < AppUtil.RetryCount; i++)
                {
                    using (var db = new TryDbConnection(settings))
                    {
                        db.BeginTransaction();
                        try
                        {
                            int cnt;

                            if (studyInsert)
                            {
                                //STUDY
                                do
                                {
                                    LogUtil.Info3("DB [{0}][{1}][{2}]", studyUid, seriesUid, sopUid);

                                    cnt = 0;
                                    using (var cmd = db.CreateCommand())
                                    {
                                        cmd.CommandText = "SELECT COUNT(*) FROM T_Study WHERE StudyInstanceUID=@0";
                                        cmd.Add(studyUid);

                                        cnt = Convert.ToInt32(cmd.ExecuteScalar());
                                    }

                                    if (cnt > 0 && Array.IndexOf(AppUtil.SkipModality, sop[DcmTag.Modality.Name]) >= 0)
                                    {
                                        break;
                                    }

                                    using (var cmd = db.CreateCommand())
                                    {
                                        var sb = new StringBuilder();
                                        if (cnt == 0)
                                        {
                                            sb.Append("INSERT INTO T_Study(StudyInstanceUID,SpecificCharacterSet,StudyDate,StudyTime,AccessionNumber,Modality,InstitutionName,ReferringPhysicianName,StudyDescription,OperatorsName,PatientName,PatientID,PatientBirthDate,PatientSex,PatientAge,BodyPartExamined,StudyID,RequestingPhysician,RequestingService,AETitle,Comment,UploadUser)");
                                            sb.Append(" VALUES(@0,@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19,@20,@21)");
                                        }
                                        else
                                        {
                                            sb.Append("UPDATE T_Study SET SpecificCharacterSet=@1,StudyDate=@2,StudyTime=@3,AccessionNumber=@4,Modality=@5,InstitutionName=@6,ReferringPhysicianName=@7,StudyDescription=@8,OperatorsName=@9,PatientName=@10,PatientID=@11,PatientBirthDate=@12,PatientSex=@13,PatientAge=@14,BodyPartExamined=@15,StudyID=@16,RequestingPhysician=@17,RequestingService=@18,AETitle=@19,Comment=@20,UploadUser=@21,UploadDate=@22");
                                            sb.Append(" WHERE StudyInstanceUID=@0");
                                        }

                                        cmd.CommandText = sb.ToString();
                                        cmd.Add(studyUid);
                                        cmd.Add(sop[DcmTag.SpecificCharacterSet.Name]);
                                        cmd.Add(sop[DcmTag.StudyDate.Name].Replace(".", ""));
                                        cmd.Add(sop[DcmTag.StudyTime.Name]);
                                        cmd.Add(sop[DcmTag.AccessionNumber.Name]);
                                        cmd.Add(sop[DcmTag.Modality.Name]);
                                        cmd.Add(sop[DcmTag.InstitutionName.Name]);
                                        cmd.Add(sop[DcmTag.ReferringPhysicianName.Name]);
                                        cmd.Add(sop[DcmTag.StudyDescription.Name].Substring2(LEN_Study_StudyDescription));
                                        cmd.Add(sop[DcmTag.OperatorsName.Name]);
                                        cmd.Add(sop[DcmTag.PatientName.Name]);
                                        cmd.Add(sop[DcmTag.PatientID.Name]);
                                        cmd.Add(sop[DcmTag.PatientBirthDate.Name].Replace(".", "").Substring2(LEN_Study_PatientBirthDate));
                                        cmd.Add(sop[DcmTag.PatientSex.Name]);
                                        cmd.Add(sop[DcmTag.PatientAge.Name].Substring2(LEN_Study_PatientAge));
                                        cmd.Add(sop[DcmTag.BodyPartExamined.Name]);
                                        cmd.Add(sop[DcmTag.StudyID.Name]);
                                        cmd.Add(sop[DcmTag.RequestingPhysician.Name]);
                                        cmd.Add(sop[DcmTag.RequestingService.Name]);

                                        string aetitle = "";
                                        if (sop.ContainsKey("AETitle"))
                                        {
                                            aetitle = sop["AETitle"].Substring2(LEN_Study_AETitle);
                                        }
                                        cmd.Add(aetitle);

                                        string comment = "";
                                        if (sop.ContainsKey("Comment"))
                                        {
                                            comment = sop["Comment"].Substring2(LEN_Study_Comment);
                                        }
                                        cmd.Add(comment);

                                        cmd.Add(sop["UserName"]);
                                        cmd.Add(now);

                                        cmd.ExecuteNonQuery();
                                    }
                                    LogUtil.Debug("DB_STUDY end");
                                } while (false);

                                //SERIES

                                cnt = 0;
                                using (var cmd = db.CreateCommand())
                                {
                                    cmd.CommandText = "SELECT COUNT(*) FROM T_Series WHERE StudyInstanceUID=@0 AND SeriesInstanceUID=@1";
                                    cmd.Add(studyUid);
                                    cmd.Add(seriesUid);

                                    cnt = Convert.ToInt32(cmd.ExecuteScalar());
                                }

                                using (var cmd = db.CreateCommand())
                                {
                                    var sb = new StringBuilder();
                                    if (cnt == 0)
                                    {
                                        sb.Append("INSERT INTO T_Series(StudyInstanceUID,SeriesInstanceUID,SOPInstanceUID,Modality,SeriesDescription,BodyPartExamined,SeriesNumber,StorageID)");
                                        sb.Append(" VALUES(@0,@1,@2,@3,@4,@5,@6,@7)");
                                    }
                                    else
                                    {
                                        sb.Append("UPDATE T_Series SET SOPInstanceUID=@2,Modality=@3,SeriesDescription=@4,BodyPartExamined=@5,SeriesNumber=@6,StorageID=@7");
                                        sb.Append(" WHERE StudyInstanceUID=@0 AND SeriesInstanceUID=@1");
                                    }

                                    cmd.CommandText = sb.ToString();
                                    cmd.Add(studyUid);
                                    cmd.Add(seriesUid);
                                    cmd.Add(sop[DcmTag.SOPInstanceUID.Name]);
                                    cmd.Add(sop[DcmTag.Modality.Name]);
                                    cmd.Add(sop[DcmTag.SeriesDescription.Name].Substring2(LEN_Series_SeriesDescription));
                                    cmd.Add(sop[DcmTag.BodyPartExamined.Name].Substring2(LEN_Series_BodyPartExamined));
                                    cmd.Add(sop[DcmTag.SeriesNumber.Name] == "" ? "0" : sop[DcmTag.SeriesNumber.Name]);
                                    cmd.Add(sop["StorageID"]);

                                    cmd.ExecuteNonQuery();
                                }
                                LogUtil.Debug("DB_SERIES end");
                            }

                            //IMAGE

                            cnt = 0;
                            using (var cmd = db.CreateCommand())
                            {
                                cmd.CommandText = "DELETE FROM T_Image WHERE StudyInstanceUID=@0 AND SeriesInstanceUID=@1 AND SOPInstanceUID=@2";
                                cmd.Add(studyUid);
                                cmd.Add(seriesUid);
                                cmd.Add(sopUid);

                                cnt = cmd.ExecuteNonQuery();
                            }

                            if (cnt > 0)
                            {
                                LogUtil.Debug1("DB_SOP 削除[{0}]", sopUid);
                            }

                            using (var cmd = db.CreateCommand())
                            {
                                var sb = new StringBuilder();
                                sb.Append("INSERT INTO T_Image(StudyInstanceUID,SeriesInstanceUID,SOPInstanceUID,InstanceNumber,SliceThickness,ImagePositionPatient,ImageOrientationPatient,SliceLocation,PhotometricInterpretation,NumberOfFrames,Rows,Columns,PixelSpacing,WindowCenter,WindowWidth,RescaleIntercept,RescaleSlope,StorageID,FileSize,CompFileSize)");
                                sb.Append(" VALUES(@0,@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19)");

                                cmd.CommandText = sb.ToString();
                                cmd.Add(studyUid);
                                cmd.Add(seriesUid);
                                cmd.Add(sopUid);
                                cmd.Add(sop[DcmTag.InstanceNumber.Name] == "" ? "0" : sop[DcmTag.InstanceNumber.Name]);
                                cmd.Add(sop[DcmTag.SliceThickness.Name]);
                                cmd.Add(sop[DcmTag.ImagePositionPatient.Name]);
                                cmd.Add(sop[DcmTag.ImageOrientationPatient.Name]);
                                cmd.Add(sop[DcmTag.SliceLocation.Name]);
                                cmd.Add(sop[DcmTag.PhotometricInterpretation.Name]);
                                cmd.Add(sop[DcmTag.NumberOfFrames.Name] == "" ? "1" : sop[DcmTag.NumberOfFrames.Name]);
                                cmd.Add(sop[DcmTag.Rows.Name]);
                                cmd.Add(sop[DcmTag.Columns.Name]);
                                cmd.Add(sop[DcmTag.PixelSpacing.Name]);
                                cmd.Add(sop[DcmTag.WindowCenter.Name].Substring2(LEN_Image_WindowCenter));
                                cmd.Add(sop[DcmTag.WindowWidth.Name].Substring2(LEN_Image_WindowWidth));
                                cmd.Add(sop[DcmTag.RescaleIntercept.Name]);
                                cmd.Add(sop[DcmTag.RescaleSlope.Name]);
                                cmd.Add(sop["StorageID"]);

                                int fileSize = 0;
                                if (!Int32.TryParse(sop["FileSize"], out fileSize))
                                {
                                    fileSize = 0;
                                }
                                cmd.Add(fileSize);

                                int compFileSize = 0;
                                if (sop.ContainsKey("CompFileSize") && !Int32.TryParse(sop["CompFileSize"], out compFileSize))
                                {
                                    compFileSize = 0;
                                }
                                cmd.Add(compFileSize);

                                cmd.ExecuteNonQuery();
                            }

                            db.Commit();

                            if (studyInsert && sop[DcmTag.Rows.Name] != "" && Array.IndexOf(AppUtil.SkipModality, sop[DcmTag.Modality.Name]) < 0)
                            {
                                studyInsert = false;
                            }
                            break;
                        }
                        catch (Exception ex)
                        {
                            db.Rollback();
                            LogUtil.Warn(ex);
                        }
                    }

                    //リトライ
                    if (i < AppUtil.RetryCount - 1)
                    {
                        System.Threading.Thread.Sleep(AppUtil.SleepTime);
                        LogUtil.Info1("RETRY:{0}", i + 1);
                    }
                    else
                    {
                        LogUtil.Error3("DB登録:失敗 [{0}][{1}][{2}]", studyUid, seriesUid, sopUid);
                        return(false);
                    }
                }
            }

            LogUtil.Debug("DB_SOP end");

            //T_SERIESの更新
            {
                var    sop       = data[0];
                string studyUid  = sop[DcmTag.StudyInstanceUID.Name];
                string seriesUid = sop[DcmTag.SeriesInstanceUID.Name];

                for (int i = 0; i < AppUtil.RetryCount; i++)
                {
                    using (var db = new TryDbConnection(settings))
                    {
                        var sopUid = new List <string>();

                        using (var cmd = db.CreateCommand())
                        {
                            cmd.CommandText = "SELECT SOPInstanceUID FROM T_Image WHERE StudyInstanceUID=@0 AND SeriesInstanceUID=@1 AND Rows>0 ORDER BY InstanceNumber";
                            cmd.Add(studyUid);
                            cmd.Add(seriesUid);

                            using (var dr = cmd.ExecuteReader())
                            {
                                while (dr.Read())
                                {
                                    sopUid.Add((string)dr["SOPInstanceUID"]);
                                }
                            }
                        }

                        if (sopUid.Count == 0)
                        {
                            break;
                        }

                        //サムネイル位置
                        string mod = sop[DcmTag.Modality.Name];
                        string thumbPos;
                        if (!AppUtil.ModalityThumbPos.TryGetValue(mod, out thumbPos))
                        {
                            thumbPos = AppUtil.ThumbPos;
                        }

                        int pos = 0;
                        switch (thumbPos)
                        {
                        case "0":
                            pos = 0;
                            break;

                        case "1":
                            pos = sopUid.Count / 2;
                            break;

                        case "2":
                            pos = sopUid.Count - 1;
                            break;

                        default:
                            pos = 0;
                            break;
                        }

                        //イメージ枚数、フレーム枚数
                        int imageCnt = 0;
                        int frameCnt = 0;
                        using (var cmd = db.CreateCommand())
                        {
                            cmd.CommandText = "SELECT COUNT(*) icnt,SUM(NumberOfFrames) fcnt FROM T_Image WHERE StudyInstanceUID=@0 AND SeriesInstanceUID=@1 AND Rows>0";
                            cmd.Add(studyUid);
                            cmd.Add(seriesUid);

                            using (var dr = cmd.ExecuteReader())
                            {
                                if (dr.Read())
                                {
                                    imageCnt = Convert.ToInt32(dr["icnt"]);
                                    frameCnt = Convert.ToInt32(dr["fcnt"]);
                                }
                            }
                        }

                        //更新
                        db.BeginTransaction();
                        try
                        {
                            using (var cmd = db.CreateCommand())
                            {
                                cmd.CommandText = "UPDATE T_Series SET SOPInstanceUID=@2,NumberOfImages=@3,NumberOfFrames=@4,StorageID=@5 WHERE StudyInstanceUID=@0 AND SeriesInstanceUID=@1";
                                cmd.Add(studyUid);
                                cmd.Add(seriesUid);
                                cmd.Add(sopUid[pos]);
                                cmd.Add(imageCnt);
                                cmd.Add(frameCnt);
                                cmd.Add(sop["StorageID"]);

                                cmd.ExecuteNonQuery();
                            }

                            db.Commit();
                            LogUtil.Debug("DB_SERIES_2 end");

                            break;
                        }
                        catch (Exception ex)
                        {
                            db.Rollback();
                            LogUtil.Warn1("DB_SERIES_2:{0}", ex.ToString());
                        }
                    }

                    //リトライ
                    if (i < AppUtil.RetryCount - 1)
                    {
                        System.Threading.Thread.Sleep(AppUtil.SleepTime);
                        LogUtil.Info1("RETRY:{0}", i + 1);
                    }
                    else
                    {
                        LogUtil.Error3("SERIES登録:失敗 STUDY[{0}] SERIES[{1}] SOP[{2}]", studyUid, seriesUid, sop[DcmTag.SOPInstanceUID.Name]);
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #4
0
        private static void GetImage(string UserName, string StorageID, string StudyUID, string SeriesUID, string SopUID)
        {
            LogUtil.Debug3("CMOVE [{0}][{1}][{2}]", StudyUID, SeriesUID, SopUID);

            var sto = DbCacheUtil.GetStorage(StorageID);

            using (var pc = new PacsComm())
            {
                //画像取得
                DicomFilesResult ret = pc.CMoveImage(StudyUID, SeriesUID, SopUID);
                if (!ret.IsSuccess)
                {
                    LogUtil.Error("CMoveImage");
                    return;
                }

                if (ret.Files.Length == 0)
                {
                    LogUtil.Error("CMoveImage [File=0]");
                    return;
                }

                LogUtil.Debug1("CMoveImage [File={0}]", ret.Files.Length.ToString());

                var seriesList = new Dictionary <string, List <Dictionary <string, string> > >();
                int cnt        = 0;

                foreach (DicomFile dcmfile in ret.Files)
                {
                    var file = dcmfile.FileName.Trim();
                    if (!File.Exists(file))
                    {
                        continue;
                    }

                    var tags = new Dictionary <string, string>();

                    tags.Add("StorageID", sto.StorageID);
                    tags.Add("UserName", UserName);

                    tags.Add("AETitle", "");
                    tags.Add("File", file);

                    var fi = new FileInfo(file);
                    tags.Add("FileSize", fi.Length.ToString());

                    //タグ取得
                    using (var dcm = new DicomData(file))
                    {
                        var tmpTags = new List <uint>(DcmTag.ToTagArray(AppUtil.IMAGE_TAG));
                        var dic     = DicomTagDictionary.Open(dcm, tmpTags.ToArray());

                        foreach (DcmTag dcmTag in AppUtil.IMAGE_TAG)
                        {
                            tags.Add(dcmTag.Name, dic.GetString(dcmTag.Tag));
                        }

                        if (dcm.Images.Load())
                        {
                            if (dcm.Images.Bits == 8 || dcm.Images.Bits == 24)
                            {
                                tags.SetString("WindowCenter", "127");
                                tags.SetString("WindowWidth", "255");
                            }
                            else
                            {
                                ImageControl ctrl = null;

                                if (dcm.Images.WindowCenters.Length > 0)
                                {
                                    tags.SetString("WindowCenter", dcm.Images.WindowCenters[0].ToString());
                                }
                                else
                                {
                                    if (ctrl == null)
                                    {
                                        ctrl = dcm.Images.CreateImageControl(0);
                                    }

                                    //MinMax
                                    if (ctrl != null)
                                    {
                                        tags.SetString("WindowCenter", ctrl.WindowCenter.ToString());
                                    }
                                }

                                if (dcm.Images.WindowWidths.Length > 0)
                                {
                                    tags.SetString("WindowWidth", dcm.Images.WindowWidths[0].ToString());
                                }
                                else
                                {
                                    if (ctrl == null)
                                    {
                                        ctrl = dcm.Images.CreateImageControl(0);
                                    }

                                    //MinMax
                                    if (ctrl != null)
                                    {
                                        tags.SetString("WindowWidth", ctrl.WindowWidth.ToString());
                                    }
                                }
                            }
                        }
                    }

                    var studyUid  = tags[DcmTag.StudyInstanceUID.Name];
                    var seriesUid = tags[DcmTag.SeriesInstanceUID.Name];
                    var sopUid    = tags[DcmTag.SOPInstanceUID.Name];
                    var mod       = tags[DcmTag.Modality.Name];

                    //DICOMチェック
                    if (studyUid == "" || seriesUid == "" || sopUid == "" || mod == "")
                    {
                        LogUtil.Error1("NOT DICOM [{0}]", Path.GetFileName(file));
                        continue;
                    }

                    //無視するモダリティ
                    if (Array.IndexOf(AppUtil.SkipModality, mod) >= 0)
                    {
                        LogUtil.Debug2("SKIP:{0} [{1}]", mod, Path.GetFileName(file));
                        continue;
                    }

                    if (!seriesList.ContainsKey(seriesUid))
                    {
                        seriesList.Add(seriesUid, new List <Dictionary <string, string> >());
                    }

                    seriesList[seriesUid].Add(tags);
                    cnt++;
                }

                //SERIES毎のSOPソート
                foreach (var series in seriesList.Values)
                {
                    series.Sort((x, y) =>
                    {
                        long n1, n2;
                        if (!long.TryParse(x[DcmTag.SeriesNumber.Name], out n1))
                        {
                            n1 = long.MaxValue;
                        }
                        if (!long.TryParse(y[DcmTag.SeriesNumber.Name], out n2))
                        {
                            n2 = long.MaxValue;
                        }
                        return((int)(n1 - n2));
                    });
                }

                int idx = 0;

                foreach (var series in seriesList.Values)
                {
                    if (series.Count == 0)
                    {
                        continue;
                    }

                    LogUtil.Info5("AETitle[{0}] StudyDate[{1}] StudyTime[{2}] Modality[{3}] PatientID[{4}]",
                                  series[0]["AETitle"], series[0][DcmTag.StudyDate.Name], series[0][DcmTag.StudyTime.Name], series[0][DcmTag.Modality.Name], series[0][DcmTag.PatientID.Name]);

                    var importList = new List <Dictionary <string, string> >();

                    foreach (var sop in series)
                    {
                        var studyUid  = sop[DcmTag.StudyInstanceUID.Name];
                        var seriesUid = sop[DcmTag.SeriesInstanceUID.Name];
                        var sopUid    = sop[DcmTag.SOPInstanceUID.Name];
                        var file      = sop["File"];

                        idx++;
                        LogUtil.Debug3("{0}/{1} [{2}]", idx, cnt, Path.GetFileName(file));

                        //画像圧縮
                        using (var imp = new Impersonate(sto.LogonUsername, sto.LogonPassword))
                        {
                            var dicomFile = Path.Combine(sto.DicomPath, studyUid, seriesUid, sopUid + AppUtil.DicomExt);
                            if (DicomUtil.DicomToComp(file, dicomFile, sop))
                            {
                                sop.Add("DicomFile", dicomFile);
                                LogUtil.Debug("圧縮 end");

                                var fi = new FileInfo(dicomFile);
                                sop.Add("CompFileSize", fi.Length.ToString());
                            }
                            else
                            {
                                LogUtil.Error1("圧縮エラー [{0}]", file);
                                return;
                            }
                        }

                        importList.Add(sop);
                    }

                    //DB登録
                    if (importList.Count > 0)
                    {
                        if (DbUtil.DicomToDB(importList))
                        {
                            LogUtil.Debug("DB登録 end");
                        }
                        else
                        {
                            LogUtil.Error("DB登録エラー");
                        }
                    }
                    else
                    {
                        LogUtil.Debug("DB登録 なし");
                    }
                }

                //DB更新
                DbUtil.UpdateStudy(StudyUID);
            }
        }