Esempio n. 1
0
        public static byte[] GetBinaryValues(DicomDataSet dcm, long tag)
        {
            DicomElement element;

            element = dcm.FindFirstElement(null, tag, true);
            if (element != null)
            {
                if (element.Length > 0)
                {
                    return(dcm.GetBinaryValue(element, (int)element.Length));
                }
            }

            return(null);
        }
Esempio n. 2
0
        public static TimeSpan?MyGetTime(this DicomDataSet ds, long dicomTime)
        {
            TimeSpan?    timeValue = null;
            DicomElement element   = null;

            if (dicomTime != 0)
            {
                element = ds.FindFirstElement(null, dicomTime, true);
            }

            if (null == element || element.Length == 0)
            {
                timeValue = null;
            }
            else
            {
                byte[] timeByte   = ds.GetBinaryValue(element, (int)element.Length);
                string timeString = System.Text.ASCIIEncoding.ASCII.GetString(timeByte);

                DateTime time;

                if (timeString.Length > 6)
                {
                    timeString = timeString.Substring(0, 6);
                }
                else if (timeString.Length < 6)
                {
                    timeString = timeString.PadRight(6, '0');
                }

                if (!string.IsNullOrEmpty(timeString) && DateTime.TryParseExact(timeString, "HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
                {
                    timeValue = new TimeSpan(time.Hour, time.Minute, time.Second, time.Millisecond);
                }
            }
            return(timeValue);
        }
Esempio n. 3
0
        /*
         * Copies the data from one element to another.
         */
        bool CopyElementData(DicomElement DstElement, DicomElement SrcElement, DicomDataSet SrcDS)
        {
            try
            {
                if ((DstElement == null) || (SrcElement == null) || (SrcDS == null))
                {
                    return(false);
                }

                if ((SrcElement.Length == 0) || (SrcElement.Length == ELEMENT_LENGTH_MAX) || (DstElement.Length == ELEMENT_LENGTH_MAX))
                {
                    return(false);
                }

                byte[] BinaryValue = SrcDS.GetBinaryValue(SrcElement, (int)SrcElement.Length);
                SrcDS.SetBinaryValue(DstElement, BinaryValue, (int)SrcElement.Length);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }

            return(true);
        }
Esempio n. 4
0
      public static byte[] GetBinaryValues(DicomDataSet dcm, long tag)
      {
         DicomElement element;

         element = dcm.FindFirstElement(null, tag, true);
         if(element != null)
         {
            if(element.Length > 0)
            {
               return dcm.GetBinaryValue(element, (int)element.Length);
            }
         }

         return null;
      }
        static string ExtractPDFData(DicomDataSet ds)
        {
            string       pdfFullPath           = string.Empty;
            string       sopInstanceUID        = string.Empty;
            DicomElement sopInstanceUIDElement = ds.FindFirstElement(null, DicomTag.SOPInstanceUID, true);

            if (sopInstanceUIDElement == null)
            {
                return(pdfFullPath);
            }

            sopInstanceUID = ds.GetStringValue(sopInstanceUIDElement, 0);
            if (string.IsNullOrEmpty(sopInstanceUID))
            {
                return(pdfFullPath);
            }

            string tempFolder  = Path.GetTempPath();
            string pdfFileName = String.Format("{0}.pdf", sopInstanceUID);

            pdfFullPath = Path.Combine(tempFolder, pdfFileName);

            long         lCount;
            DicomElement ele = null;

            if (pdfFullPath == "")
            {
                return(string.Empty);
            }

            // check if the pdf has already been extracted
            if (File.Exists(pdfFullPath))
            {
                return(pdfFullPath);
            }

            ele = ds.FindFirstElement(null, DicomTag.EncapsulatedDocument, true);
            if (ele != null)
            {
                lCount = ele.Length;
                if (lCount == 0)
                {
                    return(string.Empty);
                }

                byte[] buf;

                // Get the bytes
                buf = ds.GetBinaryValue(ele, (int)lCount);

                // Dump PDF to file
                using (FileStream fileStream = new FileStream(pdfFullPath, FileMode.Create))
                {
                    fileStream.Write(buf, 0, buf.Length);
                    fileStream.Close();
                    //_pdfCreatedFiles.Add(path);
                }

                return(pdfFullPath);
            }

            return(string.Empty);
        }
Esempio n. 6
0
        public static DateTime?MyGetDateTime(this DicomDataSet ds, long dicomDate, long dicomTime)
        {
            DateTime?    dateValue = null;
            DateTime?    timeValue = null;
            DicomElement element   = null;

            if (dicomDate != 0)
            {
                element = ds.FindFirstElement(null, dicomDate, true);
            }

            if (null == element || element.Length == 0)
            {
                dateValue = null;
            }
            else
            {
                byte[] dateByte = ds.GetBinaryValue(element, (int)element.Length);

                string dateString = System.Text.ASCIIEncoding.ASCII.GetString(dateByte);

                DateTime date;

                string format = "yyyyMMdd";

                if (dateString.Length > 8)
                {
                    dateString.Substring(0, 8);
                }

                if (!string.IsNullOrEmpty(dateString) && DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    dateValue = date;
                }
            }


            element = null;

            if (dicomTime != 0)
            {
                element = ds.FindFirstElement(null, dicomTime, true);
            }

            if (null == element || element.Length == 0)
            {
                timeValue = null;
            }
            else
            {
                byte[] timeByte   = ds.GetBinaryValue(element, (int)element.Length);
                string timeString = System.Text.ASCIIEncoding.ASCII.GetString(timeByte);

                DateTime time;

                if (timeString.Length > 6)
                {
                    timeString = timeString.Substring(0, 6);
                }
                else if (timeString.Length < 6)
                {
                    timeString = timeString.PadRight(6, '0');
                }

                if (!string.IsNullOrEmpty(timeString) && DateTime.TryParseExact(timeString, "HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
                {
                    timeValue = time;
                }
            }

            if (null != dateValue)
            {
                if (null != timeValue)
                {
                    return(new DateTime(dateValue.Value.Year,
                                        dateValue.Value.Month,
                                        dateValue.Value.Day,
                                        timeValue.Value.Hour,
                                        timeValue.Value.Minute,
                                        timeValue.Value.Second));
                }
                else
                {
                    return(new DateTime(dateValue.Value.Year,
                                        dateValue.Value.Month,
                                        dateValue.Value.Day));
                }
            }
            else if (null != timeValue)
            {
                return(new DateTime(DateTime.Now.Year,
                                    DateTime.Now.Month,
                                    DateTime.Now.Day,
                                    timeValue.Value.Hour,
                                    timeValue.Value.Minute,
                                    timeValue.Value.Second));
            }
            else
            {
                return(null);
            }
        }