Save() public méthode

Saves a DICOM file in the isolated storage area
public Save ( string file, DicomWriteOptions options, bool useIsoStore = false ) : void
file string Filename
options DicomWriteOptions DICOM write options
useIsoStore bool Save in isolated storage
Résultat void
Exemple #1
0
		public void UpdateDicomFile(string updateFile, string dicomTag, string newValue)
		{
            try
            {
                DicomFileFormat dicomFile = new DicomFileFormat();
                DicomTag updateDicomTag = DicomTag.Parse(dicomTag);

                dicomFile.Load(updateFile, DicomReadOptions.DeferLoadingLargeElements);
                string what = updateDicomTag.Entry.ToString();

                if (updateDicomTag.Entry.ToString().StartsWith("0002"))
                {
                    dicomFile.FileMetaInfo.SetString(updateDicomTag, newValue);
                }

                else
                {
                    dicomFile.Dataset.SetString(updateDicomTag, newValue);
                }

                dicomFile.Dataset.PreloadDeferredBuffers();
                dicomFile.Save(updateFile, DicomWriteOptions.Default);
            }

            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Exemple #2
0
        public void UpdateDicomFile(UpdateData updateData)
        {
            DicomFileFormat dicomFile = new DicomFileFormat();

            dicomFile.Load(updateData.DicomFileName, DicomReadOptions.DeferLoadingLargeElements);

            foreach (KeyValuePair<DicomTag, string> kvp in updateData.UpdateDataset)
            {
                dicomFile.Dataset.SetString(kvp.Key, kvp.Value);
            }

            foreach (KeyValuePair<DicomTag, string> kvp in updateData.UpdateMetadata)
            {
                dicomFile.FileMetaInfo.SetString(kvp.Key, kvp.Value);
            }

            dicomFile.Dataset.PreloadDeferredBuffers();
            dicomFile.Save(updateData.DicomFileName, DicomWriteOptions.Default);

            //if (updateData.DicomFileName != null)
            //{
            //    string path = Path.GetDirectoryName(updateData.DicomFilePath);
            //    File.Move(updateData.DicomFilePath, path + @"\" + updateData.DicomFileName);
            //}
        }
Exemple #3
0
        private void SaveDimseToFile(DcmDimseInfo dimse, byte pcid, string fileName)
        {
            string path = Path.GetFullPath(fileName);
            for (int i = 1; File.Exists(path); i++) {
                path = Path.GetFullPath(fileName);
                string ext = Path.GetExtension(path);
                path = path.Substring(0, path.Length - ext.Length);
                path += String.Format(" ({0})", i);
                path += ext;
            }

            if (String.IsNullOrEmpty(dimse.DatasetFile)) {
                DcmPresContext pres = Associate.GetPresentationContext(pcid);

                DicomFileFormat ff = new DicomFileFormat();
                ff.FileMetaInfo.FileMetaInformationVersion = DcmFileMetaInfo.Version;
                ff.FileMetaInfo.MediaStorageSOPClassUID = pres.AbstractSyntax;
                ff.FileMetaInfo.MediaStorageSOPInstanceUID = dimse.Command.AffectedSOPInstanceUID;
                ff.FileMetaInfo.TransferSyntax = pres.AcceptedTransferSyntax;
                ff.FileMetaInfo.ImplementationClassUID = Implementation.ClassUID;
                ff.FileMetaInfo.ImplementationVersionName = Implementation.Version;
                ff.FileMetaInfo.SourceApplicationEntityTitle = Associate.CalledAE;
                ff.Save(fileName, DicomWriteOptions.Default);
            }

            long pos = dimse.DatasetStream.Position;

            using (FileStream fs = File.OpenWrite(fileName)) {
                fs.Seek(0, SeekOrigin.End);
                dimse.DatasetStream.Seek(0, SeekOrigin.Begin);
                StreamUtility.Copy(dimse.DatasetStream, fs);
            }

            dimse.DatasetStream.Position = pos;
        }
Exemple #4
0
        private bool ProcessPDataTF(PDataTF pdu)
        {
            try {
                byte pcid = 0;
                foreach (PDV pdv in pdu.PDVs) {
                    pcid = pdv.PCID;
                    if (pdv.IsCommand) {
                        if (_dimse.CommandData == null)
                            _dimse.CommandData = new ChunkStream();

                        _dimse.CommandData.AddChunk(pdv.Value);

                        if (_dimse.Command == null) {
                            _dimse.Command = new DcmCommand();
                        }

                        if (_dimse.CommandReader == null) {
                            _dimse.CommandReader = new DicomStreamReader(_dimse.CommandData);
                            _dimse.CommandReader.Dataset = _dimse.Command;
                        }

                        _dimse.CommandReader.Read(null, DicomReadOptions.Default);

                        _dimse.Progress.BytesTransfered += pdv.Value.Length;
                        _dimse.Progress.EstimatedCommandLength = (int)_dimse.CommandReader.BytesEstimated;

                        if (pdv.IsLastFragment) {
                            _dimse.CloseCommand();

                            bool isLast = true;
                            if (_dimse.Command.Contains(DicomTags.DataSetType)) {
                                if (_dimse.Command.GetUInt16(DicomTags.DataSetType, 0x0101) != 0x0101) {
                                    isLast = false;

                                    DcmCommandField commandField = (DcmCommandField)_dimse.Command.GetUInt16(DicomTags.CommandField, 0);
                                    if (commandField == DcmCommandField.CStoreRequest) {
                                        ushort messageID = _dimse.Command.GetUInt16(DicomTags.MessageID, 1);
                                        DcmPriority priority = (DcmPriority)_dimse.Command.GetUInt16(DicomTags.Priority, 0);
                                        DicomUID affectedInstance = _dimse.Command.GetUID(DicomTags.AffectedSOPInstanceUID);
                                        string moveAE = _dimse.Command.GetString(DicomTags.MoveOriginatorApplicationEntityTitle, null);
                                        ushort moveMessageID = _dimse.Command.GetUInt16(DicomTags.MoveOriginatorMessageID, 1);
                                        OnPreReceiveCStoreRequest(pcid, messageID, affectedInstance, priority,
                                            moveAE, moveMessageID, out _dimse.DatasetFile);

                                        if (_dimse.DatasetFile != null) {
                                            DcmPresContext pres = Associate.GetPresentationContext(pcid);

                                            DicomFileFormat ff = new DicomFileFormat();
                                            ff.FileMetaInfo.FileMetaInformationVersion = DcmFileMetaInfo.Version;
                                            ff.FileMetaInfo.MediaStorageSOPClassUID = pres.AbstractSyntax;
                                            ff.FileMetaInfo.MediaStorageSOPInstanceUID = affectedInstance;
                                            ff.FileMetaInfo.TransferSyntax = pres.AcceptedTransferSyntax;
                                            ff.FileMetaInfo.ImplementationClassUID = Implementation.ClassUID;
                                            ff.FileMetaInfo.ImplementationVersionName = Implementation.Version;
                                            ff.FileMetaInfo.SourceApplicationEntityTitle = Associate.CalledAE;
                                            ff.Save(_dimse.DatasetFile, DicomWriteOptions.Default);

                                            _dimse.DatasetFileStream = new FileStream(_dimse.DatasetFile, FileMode.Open);
                                            _dimse.DatasetFileStream.Seek(0, SeekOrigin.End);
                                            _dimse.DatasetStream = _dimse.DatasetFileStream;
                                        }
                                    }
                                }
                            }
                            if (isLast) {
                                if (_dimse.IsNewDimse)
                                    OnReceiveDimseBegin(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                                OnReceiveDimseProgress(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                                OnReceiveDimse(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                                ProcessDimse(pcid);
                                _dimse = null;
                                return true;
                            }
                        }
                    } else {
                        if (_dimse.DatasetFile != null) {
                            long pos = _dimse.DatasetFileStream.Position;
                            _dimse.DatasetFileStream.Seek(0, SeekOrigin.End);
                            _dimse.DatasetFileStream.Write(pdv.Value, 0, pdv.Value.Length);
                            _dimse.DatasetFileStream.Position = pos;
                        } else {
                            if (_dimse.DatasetData == null) {
                                _dimse.DatasetData = new ChunkStream();
                                _dimse.DatasetStream = _dimse.DatasetData;
                            }
                            _dimse.DatasetData.AddChunk(pdv.Value);
                        }

                        if (_dimse.Dataset == null) {
                            DicomTransferSyntax ts = _assoc.GetAcceptedTransferSyntax(pdv.PCID);
                            _dimse.Dataset = new DcmDataset(ts);
                        }

                        if ((EnableStreamParse && !_dimse.Dataset.InternalTransferSyntax.IsDeflate) || pdv.IsLastFragment) {
                            if (_dimse.DatasetReader == null) {
                                if (_dimse.Dataset.InternalTransferSyntax.IsDeflate) {
                                    // DicomStreamReader needs a seekable stream
                                    MemoryStream ms = StreamUtility.Deflate(_dimse.DatasetStream, false);
                                    _dimse.DatasetReader = new DicomStreamReader(ms);
                                }
                                else
                                    _dimse.DatasetReader = new DicomStreamReader(_dimse.DatasetStream);
                                _dimse.DatasetReader.Dataset = _dimse.Dataset;
                            }

                            _dimse.Progress.BytesTransfered += pdv.Value.Length;

                            long remaining = _dimse.DatasetReader.BytesRemaining + pdv.Value.Length;
                            if (remaining >= _dimse.DatasetReader.BytesNeeded || pdv.IsLastFragment) {
                                if (_dimse.DatasetReader.Read(null, DicomReadOptions.Default) != DicomReadStatus.Success && pdv.IsLastFragment) {
                                    // ???
                                }

                                _dimse.Progress.EstimatedDatasetLength = (int)_dimse.DatasetReader.BytesEstimated;
                            }
                        }

                        if (pdv.IsLastFragment) {
                            _dimse.Close();

                            if (_dimse.IsNewDimse)
                                OnReceiveDimseBegin(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                            OnReceiveDimseProgress(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                            OnReceiveDimse(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                            ProcessDimse(pcid);
                            _dimse = null;
                            return true;
                        }
                    }
                }

                if (_dimse.IsNewDimse) {
                    OnReceiveDimseBegin(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                    _dimse.IsNewDimse = false;
                } else {
                    OnReceiveDimseProgress(pcid, _dimse.Command, _dimse.Dataset, _dimse.Progress);
                }

                return true;
            } catch (Exception e) {
            #if DEBUG
                Log.Error("{0} -> Error reading DIMSE: {1}", LogID, e.ToString());
            #else
                Log.Error("{0} -> Error reading DIMSE: {1}", LogID, e.ToString());//e.Message);
            #endif
                _dimse.Abort();
                _dimse = null;
                return false;
            }
        }
Exemple #5
0
        private void OnClickSaveWithTransferSyntax(object sender, EventArgs e)
        {
            if (_selected == -1)
                return;

            TransferSyntaxForm tsForm = new TransferSyntaxForm();
            if (tsForm.ShowDialog(this) == DialogResult.OK) {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.RestoreDirectory = true;
                if (sfd.ShowDialog(this) == DialogResult.OK) {
                    DicomFileFormat ff = new DicomFileFormat();
                    ff.Load(_files[_selected], DicomReadOptions.Default);
                    if (tsForm.SelectedTransferSyntax != null)
                        ff.ChangeTransferSytnax(tsForm.SelectedTransferSyntax, null);
                    ff.Save(sfd.FileName, DicomWriteOptions.Default);
                }
            }
        }
        private void SaveDimseToFile(DcmDataset dataset)
        {
            var now = DateTime.Now;

            Trace.WriteLine(String.Format("{0} Receive DIMSE {1} from {2} ", now, dataset.GetString(DicomTags.Modality, "UN"), this.Associate.CallingAE));
            ImageCountOnConnection++;

            bool isFiltered = IsFilteredOutData(dataset);

            if (isFiltered && !Settings.Default.ReceiveFilter_Keep)
                return;

            var fileName = GetFileNameForDicomDataset(dataset);
            string filePath;

            if (!isFiltered)
            {
                filePath = DefineStorageLocationOfFile(fileName);
            }
            else
            {
                filePath = Path.Combine(Settings.Default.ReceiveFilter_Folder, fileName);
            }

            Trace.WriteLine(String.Format("{0} Saving DIMSE {1} from {2} to {3} ", now, dataset.GetString(DicomTags.Modality, "UN"), this.Associate.CallingAE, filePath));

            var ff = new DicomFileFormat(dataset);
            ff.Save( filePath, DicomWriteOptions.Default);
        }
Exemple #7
0
        public string MakeGreyDicom(byte[] greybytes, ushort imgwidth, ushort imgheight)
        {
            DcmUID studyUid = DcmUID.Generate();
            DcmUID seriesUid = DcmUID.Generate(studyUid, 1);
            DcmUID instUid = DcmUID.Generate(seriesUid, 1);
            DcmDataset data = new DcmDataset(DcmTS.ExplicitVRBigEndian);//.ImplicitVRLittleEndian  ok
            data.AddElementWithValue(DcmTags.SOPClassUID, DcmUIDs.CTImageStorage);//ComputedRadiographyImageStorage  ok
            //data.AddElementWithValue(DcmTags.SOPClassUID, DcmUIDs .SecondaryCapture);
            data.AddElementWithValue(DcmTags.StudyInstanceUID, studyUid);
            data.AddElementWithValue(DcmTags.SeriesInstanceUID, seriesUid);
            data.AddElementWithValue(DcmTags.SOPInstanceUID, instUid);//"1.3.6.1.4.1.30071.6.635719267134010719.1.1"

            //data.AddElementWithValue(DcmTags.MediaStorageSOPClassUID, DcmUIDs.ImplicitVRLittleEndian);
            //data.AddElementWithValueString(DcmTags.MediaStorageSOPClassUID, DcmUIDs.ComputedRadiographyImageStorage.ToString());

            //type 2 attributes
            ////data.AddElement(DcmTags.PrinterStatus);
            if (tags.ContainsKey("0010,0020"))
                data.AddElementWithValueString(DcmTags.PatientID, tags["0010,0020"].Substring(5));
            if (tags.ContainsKey("0010,0010"))
                data.AddElementWithValueString(DcmTags.PatientsName, tags["0010,0010"].Substring(5));
            if (tags.ContainsKey("0010,0030"))
                data.AddElementWithValueString(DcmTags.PatientsBirthDate, tags["0010,0030"].Substring(5));
            if (tags.ContainsKey("0010,0040"))
                data.AddElementWithValueString(DcmTags.PatientsSex, tags["0010,0040"].Substring(5));
            if (tags.ContainsKey("0010,1010"))
                data.AddElementWithValueString(DcmTags.PatientsAge, tags["0010,1010"].Substring(5));

            if (tags.ContainsKey("0008,0005"))
                data.AddElementWithValueString(DcmTags.SpecificCharacterSet, tags["0008,0005"].Substring(5));
            if (tags.ContainsKey("0008,0008"))
                data.AddElementWithValueString(DcmTags.ImageType, tags["0008,0008"].Substring(5));
            //if (tags.ContainsKey("0008,0016"))
            //    data.AddElementWithValueString(DcmTags.ContentTime, DateTime.Now.ToString());
            //if (tags.ContainsKey("0008,0018"))
            //    data.AddElementWithValueString(DcmTags.ContentTime, DateTime.Now.ToString());
            if (tags.ContainsKey("0008,0020"))
                data.AddElementWithValueString(DcmTags.StudyDate, tags["0008,0020"].Substring(5));
            if (tags.ContainsKey("0008,0021"))
                data.AddElementWithValueString(DcmTags.SeriesDate, tags["0008,0021"].Substring(5));
            if (tags.ContainsKey("0008,0022"))
                data.AddElementWithValueString(DcmTags.AcquisitionDate, tags["0008,0022"].Substring(5));
            if (tags.ContainsKey("0008,0023"))
                data.AddElementWithValueString(DcmTags.ContentDate, tags["0008,0023"].Substring(5));
            if (tags.ContainsKey("0008,002a"))
                data.AddElementWithValueString(DcmTags.AcquisitionDateTime, tags["0008,002a"].Substring(5));
            if (tags.ContainsKey("0008,0030"))
                data.AddElementWithValueString(DcmTags.StudyTime, tags["0008,0030"].Substring(5));
            if (tags.ContainsKey("0008,0031"))
                data.AddElementWithValueString(DcmTags.SeriesTime, tags["0008,0031"].Substring(5));
            if (tags.ContainsKey("0008,0032"))
                data.AddElementWithValueString(DcmTags.AcquisitionTime, tags["0008,0032"].Substring(5));
            if (tags.ContainsKey("0008,0033"))
                data.AddElementWithValueString(DcmTags.ContentTime, tags["0008,0033"].Substring(5));

            if (tags.ContainsKey("0008,0050"))
                data.AddElementWithValueString(DcmTags.AcquisitionNumber, tags["0008,0050"].Substring(5));
            if (tags.ContainsKey("0008,0060"))
                data.AddElementWithValueString(DcmTags.Modality, tags["0008,0060"].Substring(5));
            if (tags.ContainsKey("0008,0070"))
                data.AddElementWithValueString(DcmTags.Manufacturer, tags["0008,0070"].Substring(5));
            if (tags.ContainsKey("0008,0080"))
                data.AddElementWithValueString(DcmTags.InstitutionName, tags["0008,0080"].Substring(5));
            if (tags.ContainsKey("0008,0081"))
                data.AddElementWithValueString(DcmTags.InstitutionAddress, tags["0008,0081"].Substring(5));
            if (tags.ContainsKey("0008,0090"))
                data.AddElementWithValueString(DcmTags.ReferringPhysiciansName, tags["0008,0090"].Substring(5));
            if (tags.ContainsKey("0008,1010"))
                data.AddElementWithValueString(DcmTags.StationName, tags["0008,1010"].Substring(5));
            if (tags.ContainsKey("0008,1030"))
                data.AddElementWithValueString(DcmTags.StudyDescription, tags["0008,1030"].Substring(5));
            if (tags.ContainsKey("0008,103e"))
                data.AddElementWithValueString(DcmTags.SeriesDescription, tags["0008,103e"].Substring(5));
            if (tags.ContainsKey("0008,1090"))
                data.AddElementWithValueString(DcmTags.ManufacturersModelName, tags["0008,1090"].Substring(5));

            if (tags.ContainsKey("0018,0010"))
                data.AddElementWithValueString(DcmTags.ContrastBolusAgent, tags["0018,0010"].Substring(5));
            if (tags.ContainsKey("0018,0015"))
                data.AddElementWithValueString(DcmTags.BodyPartExamined, tags["0018,0015"].Substring(5));
            if (tags.ContainsKey("0018,0050"))
                data.AddElementWithValueString(DcmTags.SliceThickness, tags["0018,0050"].Substring(5));
            if (tags.ContainsKey("0018,0060"))
                data.AddElementWithValueString(DcmTags.KVP, tags["0018,0060"].Substring(5));
            if (tags.ContainsKey("0018,0090"))
                data.AddElementWithValueString(DcmTags.DataCollectionDiameter, tags["0018,0090"].Substring(5));
            if (tags.ContainsKey("0018,1000"))
                data.AddElementWithValueString(DcmTags.DeviceSerialNumber, tags["0018,1000"].Substring(5));
            if (tags.ContainsKey("0018,1020"))
                data.AddElementWithValueString(DcmTags.SoftwareVersions, tags["0018,1020"].Substring(5));
            if (tags.ContainsKey("0018,1030"))
                data.AddElementWithValueString(DcmTags.ProtocolName, tags["0018,1030"].Substring(5));
            if (tags.ContainsKey("0018,1041"))
                data.AddElementWithValueString(DcmTags.ContrastBolusVolume, tags["0018,1041"].Substring(5));
            if (tags.ContainsKey("0018,1042"))
                data.AddElementWithValueString(DcmTags.ContrastBolusStartTime, tags["0018,1042"].Substring(5));
            if (tags.ContainsKey("0018,1043"))
                data.AddElementWithValueString(DcmTags.ContrastBolusStopTime, tags["0018,1043"].Substring(5));
            if (tags.ContainsKey("0018,1044"))
                data.AddElementWithValueString(DcmTags.ContrastBolusTotalDose, tags["0018,1044"].Substring(5));
            if (tags.ContainsKey("0018,1046"))
                data.AddElementWithValueString(DcmTags.ContrastFlowRate, tags["0018,1046"].Substring(5));
            if (tags.ContainsKey("0018,1047"))
                data.AddElementWithValueString(DcmTags.ContrastFlowDuration, tags["0018,1047"].Substring(5));
            if (tags.ContainsKey("0018,1049"))
                data.AddElementWithValueString(DcmTags.ContrastBolusIngredientConcentration, tags["0018,1049"].Substring(5));
            if (tags.ContainsKey("0018,1100"))
                data.AddElementWithValueString(DcmTags.ReconstructionDiameter, tags["0018,1100"].Substring(5));
            if (tags.ContainsKey("0018,1110"))
                data.AddElementWithValueString(DcmTags.DistanceSourceToDetector, tags["0018,1110"].Substring(5));
            if (tags.ContainsKey("0018,1111"))
                data.AddElementWithValueString(DcmTags.DistanceSourceToPatient, tags["0018,1111"].Substring(5));
            if (tags.ContainsKey("0018,1120"))
                data.AddElementWithValueString(DcmTags.GantryDetectorTilt, tags["0018,1120"].Substring(5));
            if (tags.ContainsKey("0018,1130"))
                data.AddElementWithValueString(DcmTags.TableHeight, tags["0018,1130"].Substring(5));
            if (tags.ContainsKey("0018,1140"))
                data.AddElementWithValueString(DcmTags.RotationDirection, tags["0018,1140"].Substring(5));
            if (tags.ContainsKey("0018,1150"))
                data.AddElementWithValueString(DcmTags.ExposureTime, tags["0018,1150"].Substring(5));
            if (tags.ContainsKey("0018,1151"))
                data.AddElementWithValueString(DcmTags.XRayTubeCurrent, tags["0018,1151"].Substring(5));
            if (tags.ContainsKey("0018,1152"))
                data.AddElementWithValueString(DcmTags.Exposure, tags["0018,1152"].Substring(5));
            if (tags.ContainsKey("0018,1160"))
                data.AddElementWithValueString(DcmTags.FilterType, tags["0018,1160"].Substring(5));
            if (tags.ContainsKey("0018,1170"))
                data.AddElementWithValueString(DcmTags.GeneratorPower, tags["0018,1170"].Substring(5));
            if (tags.ContainsKey("0018,1190"))
                data.AddElementWithValueString(DcmTags.FocalSpots, tags["0018,1190"].Substring(5));
            if (tags.ContainsKey("0018,1200"))
                data.AddElementWithValueString(DcmTags.DateOfLastCalibration, tags["0018,1200"].Substring(5));
            if (tags.ContainsKey("0018,1201"))
                data.AddElementWithValueString(DcmTags.TimeOfLastCalibration, tags["0018,1201"].Substring(5));
            if (tags.ContainsKey("0018,1210"))
                data.AddElementWithValueString(DcmTags.ConvolutionKernel, tags["0018,1210"].Substring(5));
            if (tags.ContainsKey("0018,5100"))
                data.AddElementWithValueString(DcmTags.PatientPosition, tags["0018,5100"].Substring(5));

            //if (tags.ContainsKey("0020,000D"))
            //    data.AddElementWithValueString(DcmTags.ContrastBolusStopTime, DateTime.Now.ToString());
            //if (tags.ContainsKey("0020,000E"))
            //    data.AddElementWithValueString(DcmTags.ContrastBolusStopTime, DateTime.Now.ToString());
            if (tags.ContainsKey("0020,0010"))
                data.AddElementWithValueString(DcmTags.StudyID, tags["0020,0010"].Substring(5));
            if (tags.ContainsKey("0020,0011"))
                data.AddElementWithValueString(DcmTags.SeriesNumber, tags["0020,0011"].Substring(5));
            if (tags.ContainsKey("0020,0012"))
                data.AddElementWithValueString(DcmTags.AccessionNumber, tags["0020,0012"].Substring(5));
            if (tags.ContainsKey("0020,0013"))
                data.AddElementWithValueString(DcmTags.InstanceNumber, tags["0020,0013"].Substring(5));
            if (tags.ContainsKey("0020,0032"))
                data.AddElementWithValueString(DcmTags.ImagePositionPatient, tags["0020,0032"].Substring(5));
            if (tags.ContainsKey("0020,0037"))
                data.AddElementWithValueString(DcmTags.ImageOrientationPatient, tags["0020,0037"].Substring(5));
            if (tags.ContainsKey("0020,0052"))
                data.AddElementWithValueString(DcmTags.FrameOfReferenceUID, tags["0020,0052"].Substring(5));
            if (tags.ContainsKey("0020,1040"))
                data.AddElementWithValueString(DcmTags.PositionReferenceIndicator, tags["0020,1040"].Substring(5));
            if (tags.ContainsKey("0020,1041"))
                data.AddElementWithValueString(DcmTags.SliceLocation, tags["0020,1041"].Substring(5));
            if (tags.ContainsKey("0020,4000"))
                data.AddElementWithValueString(DcmTags.ImageComments, tags["0020,4000"].Substring(5));

            //data.AddElementWithValueString(DcmTags.StudyTime, DateTime.Now.ToString());
            //data.AddElementWithValueString(DcmTags.AccessionNumber, "");
            //data.AddElementWithValueString(DcmTags.ReferringPhysiciansName, "");
            //data.AddElementWithValueString(DcmTags.StudyID, "1");
            //data.AddElementWithValueString(DcmTags.SeriesNumber, "1");
            //data.AddElementWithValueString(DcmTags.ModalitiesInStudy, "CT");//CR
            //data.AddElementWithValueString(DcmTags.Modality, "CT");//CR
            //data.AddElementWithValueString(DcmTags.NumberOfStudyRelatedInstances, "1");
            //data.AddElementWithValueString(DcmTags.NumberOfStudyRelatedSeries, "1");
            //data.AddElementWithValueString(DcmTags.NumberOfSeriesRelatedInstances, "1");
            //data.AddElementWithValueString(DcmTags.PatientOrientation, "HFS");//F/A
            //data.AddElementWithValueString(DcmTags.ImageLaterality, "U");
            if (tags.ContainsKey("0028,1050"))
                data.AddElementWithValueString(DcmTags.WindowCenter, "1113");
            if (tags.ContainsKey("0028,1051"))
                data.AddElementWithValueString(DcmTags.WindowWidth, "749");
            //data.AddElementWithValueString(DcmTags.WindowCenterWidthExplanation, "WINDOW1\\WINDOW2");
            data.AddElementWithValueString(DcmTags.PixelRepresentation, "0");
            data.AddElementWithValueString(DcmTags.RescaleIntercept, "0");//0
            data.AddElementWithValueString(DcmTags.RescaleSlope, "1");
            //data.AddElementWithValueString(DcmTags.RotationDirection, "CW");
            //ushort bitdepth = 2;未使用过

            DcmPixelData pixelData = new DcmPixelData(DcmTS.ImplicitVRLittleEndian);

            pixelData.PixelRepresentation = 0;//ok
            pixelData.ImageWidth = imgwidth;
            pixelData.ImageHeight = imgheight;

            pixelData.SamplesPerPixel = 1;//ok
            pixelData.HighBit = 15;//ok
            pixelData.BitsStored = 16;//ok
            pixelData.BitsAllocated = 16;//ok
            //pixelData.SamplesPerPixel = 1;
            //pixelData.HighBit = 7;
            //pixelData.BitsStored = 8;
            //pixelData.BitsAllocated = 8;
            pixelData.ImageType = "ORIGINAL\\PRIMARY\\AXIAL";
            pixelData.PhotometricInterpretation = "MONOCHROME2";//2 byte gray? //ok

            //pixelData.FragmentSize
            //pixelData.IsLossy = true;
            //pixelData.LossyCompressionMethod = "01";
            pixelData.PixelDataElement = DcmElement.Create(DcmTags.PixelData, DcmVR.OW); //OB: Other Byte, OW: Other Word

            //pixelData.AddFrame(bmpBytes);
            pixelData.AddFrame(greybytes);

            pixelData.UpdateDataset(data);
            DicomFileFormat ff = new DicomFileFormat(data);
            //string fileout = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "greyimg_test.dcm");
            ff.Save(fileout, Dicom.DicomWriteOptions.Default);//Default
            ff = null;
            return fileout;
        }