C# 将dicom文件格式转换成可读图片

窗体设计:

namespace DcmColorDrawDemo
{
    partial class DcmDrawDemo
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DcmDrawDemo));
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton6 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton5 = new System.Windows.Forms.ToolStripButton();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.toolStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButton1,
            this.toolStripButton6,
            this.toolStripButton3,
            this.toolStripButton4,
            this.toolStripButton2,
            this.toolStripButton5});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(755, 25);
            this.toolStrip1.TabIndex = 0;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // toolStripButton1
            // 
            this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton1.Name = "toolStripButton1";
            this.toolStripButton1.Size = new System.Drawing.Size(73, 22);
            this.toolStripButton1.Text = "选择Dicom";
            this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
            // 
            // toolStripButton6
            // 
            this.toolStripButton6.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton6.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton6.Image")));
            this.toolStripButton6.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton6.Name = "toolStripButton6";
            this.toolStripButton6.Size = new System.Drawing.Size(60, 22);
            this.toolStripButton6.Text = "显示标签";
            this.toolStripButton6.Click += new System.EventHandler(this.toolStripButton6_Click);
            // 
            // toolStripButton3
            // 
            this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton3.Name = "toolStripButton3";
            this.toolStripButton3.Size = new System.Drawing.Size(84, 22);
            this.toolStripButton3.Text = "控件显示图像";
            this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton3_Click);
            // 
            // toolStripButton4
            // 
            this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton4.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton4.Name = "toolStripButton4";
            this.toolStripButton4.Size = new System.Drawing.Size(82, 22);
            this.toolStripButton4.Text = "GDI绘制图像";
            this.toolStripButton4.Click += new System.EventHandler(this.toolStripButton4_Click);
            // 
            // toolStripButton2
            // 
            this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton2.Name = "toolStripButton2";
            this.toolStripButton2.Size = new System.Drawing.Size(60, 22);
            this.toolStripButton2.Text = "保存图像";
            this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click);
            // 
            // toolStripButton5
            // 
            this.toolStripButton5.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton5.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton5.Image")));
            this.toolStripButton5.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton5.Name = "toolStripButton5";
            this.toolStripButton5.Size = new System.Drawing.Size(60, 22);
            this.toolStripButton5.Text = "内容重置";
            this.toolStripButton5.Click += new System.EventHandler(this.toolStripButton5_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            // 
            // saveFileDialog1
            // 
            this.saveFileDialog1.Filter = "Gif文件|*.gif|jpeg文件|*.jpg|bmp位图|*.bmp|可移植网络图形格式|*.png";
            // 
            // backgroundWorker1
            // 
            this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
            this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
            // 
            // textBox1
            // 
            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox1.Location = new System.Drawing.Point(0, 25);
            this.textBox1.Margin = new System.Windows.Forms.Padding(5);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(755, 508);
            this.textBox1.TabIndex = 1;
            this.textBox1.WordWrap = false;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 25);
            this.pictureBox1.Margin = new System.Windows.Forms.Padding(5);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(755, 508);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // 
            // DcmDrawDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(755, 533);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.toolStrip1);
            this.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Margin = new System.Windows.Forms.Padding(5);
            this.Name = "DcmDrawDemo";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "DicomViewer";
            this.Load += new System.EventHandler(this.DcmDrawDemo_Load);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripButton toolStripButton1;
        private System.Windows.Forms.ToolStripButton toolStripButton2;
        private System.Windows.Forms.ToolStripButton toolStripButton3;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.ComponentModel.BackgroundWorker backgroundWorker1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ToolStripButton toolStripButton4;
        private System.Windows.Forms.ToolStripButton toolStripButton5;
        private System.Windows.Forms.ToolStripButton toolStripButton6;
    }
}

窗体CS:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DcmColorDrawDemo
{
    public partial class DcmDrawDemo : Form
    {
        DicomHandler m_handler;//处理类
        Graphics m_Graphics = null;//GDI
        string titleName = "DicomViewer";//标题
        string m_dicomFilePathName = string.Empty;//文件路径

        public DcmDrawDemo()
        {
            InitializeComponent();
        }

        private void DcmDrawDemo_Load(object sender, EventArgs e)
        {
            m_Graphics = this.CreateGraphics();
            this.BackColor = Color.WhiteSmoke;
        }

        // 选择Dicom
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                MessageBox.Show("影像载入中,请稍后...");
                return;
            }

            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            m_dicomFilePathName = openFileDialog1.FileName;
            Text = titleName + "-" + m_dicomFilePathName;
            m_handler = new DicomHandler(m_dicomFilePathName);
            backgroundWorker1.RunWorkerAsync();
        }

        // 显示标签
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (!CheckLoadDicom())
                return;

            textBox1.Visible = true;
            pictureBox1.Visible = false;
            m_handler.readAndShow(textBox1);
        }

        // 控件显示图像
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (!CheckLoadDicom())
                return;

            textBox1.Visible = false;
            pictureBox1.Visible = true;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            if (m_handler != null && m_handler.gdiImg != null)
            {
                pictureBox1.Image = m_handler.gdiImg;
            }
        }

        // GDI绘制图像
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (!CheckLoadDicom())
                return;

            textBox1.Visible = false;
            pictureBox1.Visible = false;
            if (m_handler != null && m_handler.gdiImg != null)
            {
                this.BackColor = Color.WhiteSmoke;
                m_Graphics.DrawImage(m_handler.gdiImg, 0, 0, m_handler.gdiImg.Width, m_handler.gdiImg.Height);
            }
        }

        // 保存图像
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (!CheckLoadDicom())
                return;

            if (saveFileDialog1.ShowDialog() != DialogResult.OK || m_handler == null || m_handler.gdiImg == null)
                return;

            m_handler.saveAs(saveFileDialog1.FileName);
        }

        // 判断选择加载情况
        private bool CheckLoadDicom()
        {
            if (string.IsNullOrEmpty(m_dicomFilePathName))
            {
                MessageBox.Show("请先选择Dicom!");
                return false;
            }
            if (backgroundWorker1.IsBusy)
            {
                MessageBox.Show("影像载入中,请稍后...");
                return false;
            }
            return true;
        }

        // 内容重置
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            textBox1.Text = string.Empty;
            Text = titleName;
            textBox1.Visible = true;
            pictureBox1.Visible = true;
            m_Graphics.Clear(Color.WhiteSmoke);
        }

        #region backgroundWorker
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            m_handler.tagRead();
            m_handler.getImg();
            //if (m_handler.getImg())
            //    pictureBox1.Image = m_handler.gdiImg;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //if (backgroundWorker1.IsBusy)
            //{
            //    Bitmap loadImg = new Bitmap(200, 100);
            //    Graphics g = Graphics.FromImage(loadImg);
            //    g.DrawString("载入中,请稍后...", new Font(new FontFamily("Arial"), 15.0f), Brushes.Black, new PointF(0, 0));
            //    pictureBox1.Image = loadImg;
            //    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            //}
        }
        #endregion

    }
}

DicomHandler:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Text.RegularExpressions;

namespace DcmColorDrawDemo
{
    public class DicomHandler
    {
        string fileName = "";
        Dictionary tags = new Dictionary();//dicom文件中的标签 
        BinaryReader dicomFile;//dicom文件流 

        //文件元信息 
        public Bitmap gdiImg;//转换后的gdi图像 
        UInt32 fileHeadLen;//文件头长度 
        long fileHeadOffset;//文件数据开始位置 
        UInt32 pixDatalen;//像素数据长度 
        long pixDataOffset = 0;//像素数据开始位置 
        bool isLitteEndian = true;//是否小端模式(小端在前 、大端在前) 在 C# 中,所有的整数类型都是小端模式(即低位在前)。如果您需要使用大端模式(即高位在前),可以使用 byte 类型,或者在读写整数时手动进行大小端的转换
        bool isExplicitVR = true;//有无VR 

        //像素信息 
        int colors;//颜色数 RGB为3 黑白为1 
        public int windowWith = 0, windowCenter = 0 / 2;//在未设定时 窗宽窗位为0 
        int rows, cols;

        public void readAndShow(TextBox textBox1)
        {
            textBox1.Clear();
            IDictionaryEnumerator enor = tags.GetEnumerator();
            while (enor.MoveNext())
            {
                if (enor.Key.ToString().Length > 9)
                {
                    textBox1.Text += enor.Key.ToString() + "\r\n";
                    textBox1.Text += enor.Value.ToString().Replace('\0', ' ');
                }
                else
                    textBox1.Text += enor.Key.ToString() + enor.Value.ToString().Replace('\0', ' ') + "\r\n";
            }
        }

        public DicomHandler(string _filename)
        {
            fileName = _filename;
        }

        public void saveAs(string filename)
        {
            switch (filename.Substring(filename.LastIndexOf('.')))
            {
                case ".jpg":
                    gdiImg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case ".bmp":
                    gdiImg.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
                case ".png":
                    gdiImg.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                case ".gif":
                    gdiImg.Save(filename, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                default:
                    break;
            }
        }

        //获取图像 在图像数据偏移量已经确定的情况下(附带简略调窗代码)  
        public bool getImg()
        {
            if (fileName == string.Empty)
                return false;

            int dataLen, validLen, hibit;//数据长度 有效位 
                                         //int imgNum;//帧数 

            rows = int.Parse(tags["0028,0010"].Substring(5));
            cols = int.Parse(tags["0028,0011"].Substring(5));

            colors = int.Parse(tags["0028,0002"].Substring(5));
            dataLen = int.Parse(tags["0028,0100"].Substring(5));//bits allocated 
            validLen = int.Parse(tags["0028,0101"].Substring(5));
            bool signed = int.Parse(tags["0028,0103"].Substring(5)) == 0 ? false : true;
            hibit = int.Parse(tags["0028,0102"].Substring(5));
            float rescaleSlop = 1, rescaleinter = 0;
            if (tags.ContainsKey("0028,1052") && tags.ContainsKey("0028,1053"))
            {
                rescaleSlop = float.Parse(tags["0028,1053"].Substring(5));
                rescaleinter = float.Parse(tags["0028,1052"].Substring(5));
            }

            //读取预设窗宽窗位 
            //预设窗值读取代码...... 
            #region//读取预设窗宽窗位 
            if (windowWith == 0 && windowCenter == 0)
            {
                Regex r = new Regex(@"([0-9]+)+");
                if (tags.ContainsKey("0028,1051"))
                {
                    Match m = r.Match(tags["0028,1051"].Substring(5));

                    if (m.Success)
                        windowWith = int.Parse(m.Value);
                    else
                        windowWith = 1 << validLen;
                }
                else
                {
                    windowWith = 1 << validLen;
                }

                if (tags.ContainsKey("0028,1050"))
                {
                    Match m = r.Match(tags["0028,1050"].Substring(5));
                    if (m.Success)
                        windowCenter = int.Parse(m.Value);//窗位 
                    else
                        windowCenter = windowWith / 2;
                }
                else
                {
                    windowCenter = windowWith / 2;
                }
            }
            #endregion

            gdiImg = new Bitmap(cols, rows);
            BinaryReader dicomFile = new BinaryReader(File.OpenRead(fileName));
            dicomFile.BaseStream.Seek(pixDataOffset, SeekOrigin.Begin);
            long reads = 0;
            int max = 0, min = int.MaxValue;

            for (int i = 0; i < gdiImg.Height; i++)
            {
                for (int j = 0; j < gdiImg.Width; j++)
                {
                    if (reads >= pixDatalen)
                        break;
                    byte[] pixData;

                    pixData = dicomFile.ReadBytes(dataLen / 8 * colors);
                    reads += pixData.Length;

                    Color c = Color.Empty;
                    if (colors == 1)
                    {
                        int grayGDI;

                        double gray;
                        if (validLen <= 8)
                            gray = (double)pixData[0];
                        else
                        {
                            if (isLitteEndian == false)
                                Array.Reverse(pixData, 0, 2);

                            if (signed == false)
                                gray = BitConverter.ToUInt16(pixData, 0);
                            else
                                gray = BitConverter.ToInt16(pixData, 0);

                            if ((rescaleSlop != 1.0f) || (rescaleinter != 0.0f))
                            {
                                float fValue = (float)gray * rescaleSlop + rescaleinter;
                                gray = (short)fValue;
                            }

                            if (gray > max)
                                max = (int)gray;
                            if (gray < min)
                                min = (int)gray;
                        }

                        //调窗代码,就这么几句而已  
                        //1先确定窗口范围 2映射到8位灰度
                        int grayStart = (windowCenter - windowWith / 2);
                        int grayEnd = (windowCenter + windowWith / 2);

                        if (gray < grayStart)
                            grayGDI = 0;
                        else if (gray > grayEnd)
                            grayGDI = 255;
                        else
                        {
                            grayGDI = (int)((gray - grayStart) * 255 / windowWith);
                        }

                        if (grayGDI > 255)
                            grayGDI = 255;
                        else if (grayGDI < 0)
                            grayGDI = 0;
                        c = Color.FromArgb(grayGDI, grayGDI, grayGDI);
                    }
                    else if (colors == 3)
                    {
                        c = Color.FromArgb(pixData[0], pixData[1], pixData[2]);
                    }
                    gdiImg.SetPixel(j, i, c);
                }
            }
            dicomFile.Close();
            return true;
        }

        public void tagRead()//不断读取所有tag 及其值 直到碰到图像数据 (7fe0 0010 ) 
        {
            if (fileName == string.Empty)
                return;
            dicomFile = new BinaryReader(File.OpenRead(fileName));

            //跳过128字节导言部分 
            dicomFile.BaseStream.Seek(128, SeekOrigin.Begin);

            if (new string(dicomFile.ReadChars(4)) != "DICM")
            {
                MessageBox.Show("没有dicom标识头,文件格式错误");
                return;
            }
            bool enDir = false;
            int leve = 0;
            StringBuilder folderData = new StringBuilder();//该死的文件夹标签 
            string folderTag = "";
            try
            {
                while (dicomFile.BaseStream.Position + 6 < dicomFile.BaseStream.Length)
            {
                //读取tag 
                string tag = dicomFile.ReadUInt16().ToString("x4") + "," +
                dicomFile.ReadUInt16().ToString("x4");

                string VR = string.Empty;
                UInt32 Len = 0;
                //读取VR跟Len 
                //对OB OW SQ 要做特殊处理 先置两个字节0 然后4字节值长度 
                //------------------------------------------------------这些都是在读取VR一步被阻断的情况 
                if (tag.Substring(0, 4) == "0002")//文件头 特殊情况 
                {
                    VR = new string(dicomFile.ReadChars(2));

                    if (VR == "OB" || VR == "OW" || VR == "SQ" || VR == "OF" || VR == "UT" || VR == "UN")
                    {
                        dicomFile.BaseStream.Seek(2, SeekOrigin.Current);
                        Len = dicomFile.ReadUInt32();
                    }
                    else
                        Len = dicomFile.ReadUInt16();
                }
                else if (tag == "fffe,e000" || tag == "fffe,e00d" || tag == "fffe,e0dd")//文件夹标签 
                {
                    VR = "**";
                    Len = dicomFile.ReadUInt32();
                }
                else if (isExplicitVR == true)//有无VR的情况 
                {
                    VR = new string(dicomFile.ReadChars(2));

                    if (VR == "OB" || VR == "OW" || VR == "SQ" || VR == "OF" || VR == "UT" || VR == "UN")
                    {
                        dicomFile.BaseStream.Seek(2, SeekOrigin.Current);
                        Len = dicomFile.ReadUInt32();
                    }
                    else
                        Len = dicomFile.ReadUInt16();
                }
                else if (isExplicitVR == false)
                {
                    VR = getVR(tag);//无显示VR时根据tag一个一个去找 真tm烦啊。 
                    Len = dicomFile.ReadUInt32();
                }
                //判断是否应该读取VF 以何种方式读取VF 
                //-------------------------------------------------------这些都是在读取VF一步被阻断的情况 
                byte[] VF = { 0x00 };

                if (tag == "7fe0,0010")//图像数据开始了 
                {
                    pixDatalen = Len;
                    pixDataOffset = dicomFile.BaseStream.Position;
                    dicomFile.BaseStream.Seek(Len, SeekOrigin.Current);
                    VR = "UL";
                    VF = BitConverter.GetBytes(Len);
                }
                else if ((VR == "SQ" && Len == UInt32.MaxValue) || (tag == "fffe,e000" && Len == UInt32.MaxValue))//靠 遇到文件夹开始标签了 
                {
                    if (enDir == false)
                    {
                        enDir = true;
                        folderData.Remove(0, folderData.Length);
                        folderTag = tag;
                    }
                    else
                    {
                        leve++;//VF不赋值 
                    }
                }
                else if ((tag == "fffe,e00d" && Len == UInt32.MinValue) || (tag == "fffe,e0dd" && Len == UInt32.MinValue))//文件夹结束标签 
                {
                    if (enDir == true)
                    {
                        enDir = false;
                    }
                    else
                    {
                        leve--;
                    }
                }
                else
                    VF = dicomFile.ReadBytes((int)Len);

                string VFStr;

                VFStr = getVF(VR, VF);

                //----------------------------------------------------------------针对特殊的tag的值的处理 
                //特别针对文件头信息处理 
                if (tag == "0002,0000")
                {
                    fileHeadLen = Len;
                    fileHeadOffset = dicomFile.BaseStream.Position;
                }
                else if (tag == "0002,0010")//传输语法 关系到后面的数据读取 
                {
                    switch (VFStr)
                    {
                        case "1.2.840.10008.1.2.1\0"://显示little 
                            isLitteEndian = true;
                            isExplicitVR = true;
                            break;
                        case "1.2.840.10008.1.2.2\0"://显示big 
                            isLitteEndian = false;
                            isExplicitVR = true;
                            break;
                        case "1.2.840.10008.1.2\0"://隐式little 
                            isLitteEndian = true;
                            isExplicitVR = false;
                            break;
                        default:
                            break;
                    }
                }
                for (int i = 1; i <= leve; i++)
                    tag = "--" + tag;
                //------------------------------------数据搜集代码 
                if ((VR == "SQ" && Len == UInt32.MaxValue) || (tag == "fffe,e000" && Len == UInt32.MaxValue) || leve > 0)//文件夹标签代码 
                {
                    folderData.AppendLine(tag + "(" + VR + "):" + VFStr);
                }
                else if (((tag == "fffe,e00d" && Len == UInt32.MinValue) || (tag == "fffe,e0dd" && Len == UInt32.MinValue)) && leve == 0)//文件夹结束标签 
                {
                    folderData.AppendLine(tag + "(" + VR + "):" + VFStr);
                    tags.Add(folderTag + "SQ", folderData.ToString());
                }
                else
                    tags.Add(tag, "(" + VR + "):" + VFStr);
            }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
            finally
            {
                dicomFile.Close();
            }
        }

        string getVR(string tag)
        {
            switch (tag)
            {
                case "0002,0000"://文件元信息长度 
                    return "UL";
                    break;
                case "0002,0010"://传输语法 
                    return "UI";
                    break;
                case "0002,0013"://文件生成程序的标题 
                    return "SH";
                    break;
                case "0008,0005"://文本编码 
                    return "CS";
                    break;
                case "0008,0008":
                    return "CS";
                    break;
                case "0008,1032"://成像时间 
                    return "SQ";
                    break;
                case "0008,1111":
                    return "SQ";
                    break;
                case "0008,0020"://检查日期 
                    return "DA";
                    break;
                case "0008,0060"://成像仪器 
                    return "CS";
                    break;
                case "0008,0070"://成像仪厂商 
                    return "LO";
                    break;
                case "0008,0080":
                    return "LO";
                    break;
                case "0010,0010"://病人姓名 
                    return "PN";
                    break;
                case "0010,0020"://病人id 
                    return "LO";
                    break;
                case "0010,0030"://病人生日 
                    return "DA";
                    break;
                case "0018,0060"://电压 
                    return "DS";
                    break;
                case "0018,1030"://协议名 
                    return "LO";
                    break;
                case "0018,1151":
                    return "IS";
                    break;
                case "0020,0010"://检查ID 
                    return "SH";
                    break;
                case "0020,0011"://序列 
                    return "IS";
                    break;
                case "0020,0012"://成像编号 
                    return "IS";
                    break;
                case "0020,0013"://影像编号 
                    return "IS";
                    break;
                case "0028,0002"://像素采样1为灰度3为彩色 
                    return "US";
                    break;
                case "0028,0004"://图像模式MONOCHROME2为灰度 
                    return "CS";
                    break;
                case "0028,0006"://颜色值排列顺序 可能骨头重建那个的显示就是这个问题 
                    return "US";
                    break;
                case "0028,0008"://图像的帧数 
                    return "IS";
                    break;
                case "0028,0010"://row高 
                    return "US";
                    break;
                case "0028,0011"://col宽 
                    return "US";
                    break;
                case "0028,0100"://单个采样数据长度 
                    return "US";
                    break;
                case "0028,0101"://实际长度 
                    return "US";
                    break;
                case "0028,0102"://采样最大值 
                    return "US";
                    break;
                case "0028,0103"://像素数据类型 
                    return "US";
                    break;
                case "0028,1050"://窗位 
                    return "DS";
                    break;
                case "0028,1051"://窗宽 
                    return "DS";
                    break;
                case "0028,1052":
                    return "DS";
                    break;
                case "0028,1053":
                    return "DS";
                    break;
                case "0040,0008"://文件夹标签 
                    return "SQ";
                    break;
                case "0040,0260"://文件夹标签 
                    return "SQ";
                    break;
                case "0040,0275"://文件夹标签 
                    return "SQ";
                    break;
                case "7fe0,0010"://像素数据开始处 
                    return "OW";
                    break;
                default:
                    return "UN";
                    break;
            }
        }

        string getVF(string VR, byte[] VF)
        {
            if (VF.Length == 0)
                return "";
            string VFStr = string.Empty;
            if (isLitteEndian == false)//如果是big字节序 先把数据翻转一下 
                Array.Reverse(VF);
            switch (VR)
            {
                case "SS":
                    VFStr = BitConverter.ToInt16(VF, 0).ToString();
                    break;
                case "US":
                    VFStr = BitConverter.ToUInt16(VF, 0).ToString();
                    break;
                case "SL":
                    VFStr = BitConverter.ToInt32(VF, 0).ToString();
                    break;
                case "UL":
                    VFStr = BitConverter.ToUInt32(VF, 0).ToString();
                    break;
                case "AT":
                    VFStr = BitConverter.ToUInt16(VF, 0).ToString();
                    break;
                case "FL":
                    VFStr = BitConverter.ToSingle(VF, 0).ToString();
                    break;
                case "FD":
                    VFStr = BitConverter.ToDouble(VF, 0).ToString();
                    break;
                case "OB":
                    VFStr = BitConverter.ToString(VF, 0);
                    break;
                case "OW":
                    VFStr = BitConverter.ToString(VF, 0);
                    break;
                case "SQ":
                    VFStr = BitConverter.ToString(VF, 0);
                    break;
                case "OF":
                    VFStr = BitConverter.ToString(VF, 0);
                    break;
                case "UT":
                    VFStr = BitConverter.ToString(VF, 0);
                    break;
                case "UN":
                    VFStr = Encoding.Default.GetString(VF);
                    break;
                default:
                    VFStr = Encoding.Default.GetString(VF);
                    break;
            }
            return VFStr;
        }
    }
}

原文链接:https://blog.csdn.net/cygzsw/article/details/109714304

 

你可能感兴趣的:(C#,DICOM,C#,DICOM)