DICOM文件修改方法
/// <summary>
/// 读取dicom文件
/// </summary>
/// <param name="srcdirectory"></param>
/// <param name="desdirectory"></param>
public static void ReadAndEditFileInfo(string srcdirectory, string desdirectory)
{
string[] Srcfilenames = Directory.GetFileSystemEntries(srcdirectory);
string[] Destfilenames = Directory.GetFileSystemEntries(desdirectory);
foreach (string srcfilename in Srcfilenames)
{
foreach (string destfilename in Destfilenames)
{
int oldLength = destfilename.Length;
int length = srcfilename.Length;
int oldlocation = destfilename.LastIndexOf("\\");
int location = srcfilename.LastIndexOf("\\");
string name = srcfilename.Substring(location, length - location);
string oldname = destfilename.Substring(oldlocation, oldLength - oldlocation);
//如果查找最后文件名称里面带.dcm的后缀就不进行文件比较了
if (!name.Contains(".dcm") || !oldname.Contains(".dcm"))
{
if (name == oldname)
{
//获取文件名称
if (Directory.Exists(srcfilename))
{
//获取文件夹下的所有文件名称
DirectoryInfo root = new DirectoryInfo(srcfilename);
FileInfo[] files = root.GetFiles();
DirectoryInfo oldroot = new DirectoryInfo(destfilename);
FileInfo[] oldfiles = oldroot.GetFiles();
foreach (var file in files)
{
foreach (var oldfile in oldfiles)
{
if (file.Name == oldfile.Name)
{
string path = srcfilename + "\\" + file;
string oldpath = destfilename + "\\" + oldfile;
ImageEditor.UpdateDicomFileInfo(oldpath, path);
}
}
}
}
ReadAndEditFileInfo(srcfilename, destfilename);
}
}
}
}
}
/// <summary>
/// 修改DICOM文件属性值
/// </summary>
/// <param name="srcdic"></param>
/// <param name="desdic"></param>
/// <returns></returns>
public static bool UpdateDicomFileInfo(string srcdic, string desdic)
{
try
{
FileStream fs = new FileStream(desdic, FileMode.Create);
Stream stream = fs;
var dicomFile = DicomFile.Open(srcdic);
var datasetFile = dicomFile.Dataset.GetDicomItem<Dicom.DicomStringElement>(DicomTag.StudyDate);
string patientId = dicomFile.Dataset.GetString(DicomTag.PatientID);
string patientName = dicomFile.Dataset.GetString(DicomTag.PatientName);
string studyInstanceUId = dicomFile.Dataset.GetString(DicomTag.StudyInstanceUID);
string seriesInstanceUId = dicomFile.Dataset.GetString(DicomTag.SeriesInstanceUID);
//dicomFile.Dataset.AddOrUpdate(DicomTag.StudyDate, DateTime.Now);
//dicomFile.Dataset.AddOrUpdate(DicomTag.StudyTime, DateTime.Now);
//dicomFile.Dataset.AddOrUpdate(DicomTag.PatientSex, "F");
//dicomFile.Dataset.AddOrUpdate(DicomTag.PatientAge, "015Y");//DWMY (year,month,week,day)
dicomFile.Dataset.AddOrUpdate(DicomTag.Manufacturer, "");
dicomFile.Dataset.AddOrUpdate(DicomTag.ManufacturerModelName, "");
dicomFile.Dataset.AddOrUpdate(DicomTag.PatientName, patientName + "-Name");
dicomFile.Dataset.AddOrUpdate(DicomTag.PatientID, patientId + "-ID");
dicomFile.Dataset.AddOrUpdate(DicomTag.StudyInstanceUID, studyInstanceUId + "1");
dicomFile.Dataset.AddOrUpdate(DicomTag.SeriesInstanceUID, seriesInstanceUId + "2");
//dicomFile.Dataset.AddOrUpdate(DicomTag.TableHeight,12.13);
dicomFile.Save(stream);
}
catch(Exception e)
{
return false;
}
return true;
}