如何在C#中读取DICOM(* .dcm)文件..? [英] How to read DICOM (*.dcm) file in C#..?

查看:315
本文介绍了如何在C#中读取DICOM(* .dcm)文件..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些建议来读取带有DICOM格式的文​​件意味着它将带有* .dcm文件扩展名。



这里DICOM的意思是数字成像和通信医学



意味着我需要读取该文件并将其转换为Image ..

I need some suggetion to read a file with DICOM formate means it will be with *.dcm file extension.

Here DICOM means "Digital Imaging and Communications in Medicine"

means i need to read that file and convert it into Image..

推荐答案

DICOM图像将在.dic&单帧的.dcm格式。

您需要使用DICOM库来查看dicom的图像。

fot表格1你可以使用以下代码..



actually the DICOM images will be in .dic & .dcm format for single frames.
you need to use the DICOM libraries to view an image of dicom.
fot the form 1 u can use the following code..

   public void  button1_Click(object sender, EventArgs e)
{
    panel2.Controls.Clear();
    panel3.Controls.Clear();
    var ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    ofd.Filter = "DICOM Files (*.dcm;*.dic)|*.dcm;*.dic|All Files (*.*)|*.*";

    if (ofd.ShowDialog() == DialogResult.Cancel)
        return;

    for (int i=0 ; i <= ofd.FileNames.Length-1; i++)
    {
        OpenFile(ofd.FileNames[i]);

        var a = new DisplayForm(_file);
        a.Visible = true;
        a.TopLevel = false;
        a.Dock = DockStyle.Fill;
        a.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;


        var p = new Panel();
        var p1 = new Panel();



        p1.BorderStyle = BorderStyle.FixedSingle;
        p.BorderStyle = BorderStyle.FixedSingle;

        string s = i.ToString();
        p1.Name = s;

        p.Dock = DockStyle.Top;
        p1.Dock = DockStyle.Top;



        var lbl = new Label();


        lbl.Width = 300;
        string fName = ofd.FileNames[i];
        lbl.Text = fName;
        lbl.Click += new EventHandler(lbl_Click);

        lbl.ForeColor = Color.White;
        p.Controls.Add(lbl);
        p1.Controls.Add(a);
        panel2.Controls.Add(p);
        panel2.Controls.Add(p1);

        p.AutoSize = true;
        p1.Click +=new EventHandler(p1_Click);
    }
}

private void p1_Click(object sender, EventArgs e)
{
    panel3.Controls.Clear();

    Panel p1 = (Panel)sender;
    Label lbl = (Label)sender;
    OpenFile(@" " + lbl.Text);
    var a = new DisplayForm(_file);
    a.TopLevel = false;
    a.Visible = true;
    a.Dock = DockStyle.Fill;
    a.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    panel3.Controls.Add(a);
}
public void lbl_Click(object sender, EventArgs e)
{
    panel3.Controls.Clear();
    Label lbl = (Label)sender;

    OpenFile(@" " + lbl.Text);
    var a = new DisplayForm(_file);
    a.TopLevel = false;
    a.Visible = true;
    a.Dock = DockStyle.Fill;
    a.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    panel3.Controls.Add(a);

}

public static void FitPanel(Panel pnl)
{
    int maxright = 0;
    int maxbottom = 0;
    foreach (Control ctl in pnl.Controls)
    {
        maxright = (ctl.Right > maxright ? ctl.Right : maxright);
        maxbottom = (ctl.Bottom > maxbottom ? ctl.Bottom : maxbottom);
    }
    int deltabottom = pnl.Bottom - (pnl.Top + maxbottom);
    int deltaright = pnl.Right - (pnl.Left + maxright);
    Form frm = pnl.FindForm();
    frm.SuspendLayout();
    frm.Height = frm.Height - deltabottom;
    frm.Width = frm.Width - deltaright;
    frm.ResumeLayout();
}
private void Reset()
{
    listView1.Items.Clear();
}

public void OpenFile(string fileName)
{
    DicomFile file = null;

    try
    {
        file = DicomFile.Open(fileName);
    }
    catch (DicomFileException ex)
    {
        file = ex.File;
        MessageBox.Show(this, "Exception while loading DICOM file: " + ex.Message, "Error loading DICOM file", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    OpenFile(file);

}

private delegate void AddItemDelegate(string tag, string vr, string length, string value);

private void AddItem(string tag, string vr, string length, string value)
{
    if (InvokeRequired)
    {
        BeginInvoke(new AddItemDelegate(AddItem), tag, vr, length, value);
        return;

    }

    var lvi = listView1.Items.Add(tag);
    lvi.SubItems.Add(vr);
    lvi.SubItems.Add(length);
    lvi.SubItems.Add(value);


}

private void mahesh()
{

    var a = new DisplayForm(_file);
    a.Visible = true;
    a.TopLevel = false;
    a.Dock = DockStyle.Fill;
    a.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;


}



public void OpenFile(DicomFile file)
{
    try
    {
        listView1.BeginUpdate();

        Reset();

        _file = file;

        new DicomDatasetWalker(_file.FileMetaInfo).Walk(new DumpWalker(this));
        new DicomDatasetWalker(_file.Dataset).Walk(new DumpWalker(this));


    }
    catch (Exception ex)
    {
        MessageBox.Show(this, "Exception while loading DICOM file: " + ex.Message, "Error loading DICOM file", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        listView1.EndUpdate();

    }
}


private class DumpWalker : IDicomDatasetWalker
{
    int _level = 0;

    public DumpWalker(Form1 form)
    {
        Form = form;
        Level = 0;
    }

    public Form1 Form
    {
        get;
        set;
    }

    public int Level
    {
        get { return _level; }
        set
        {
            _level = value;
            Indent = String.Empty;
            for (int i = 0; i < _level; i++)
                Indent += "    ";
        }
    }

    private string Indent
    {
        get;
        set;
    }

    public void OnBeginWalk(DicomDatasetWalker walker, DicomDatasetWalkerCallback callback)
    {
    }

    public bool OnElement(DicomElement element)
    {
        var tag = String.Format("{0}{1}  {2}", Indent, element.Tag.ToString().ToUpper(), element.Tag.DictionaryEntry.Name);

        string value = "<large value not displayed>";
        if (element.Length <= 2048)
            value = String.Join("\\", element.Get<string[]>());

        if (element.ValueRepresentation == DicomVR.UI && element.Count > 0)
        {
            var uid = element.Get<DicomUID>(0);
            var name = uid.Name;
            if (name != "Unknown")
                value = String.Format("{0} ({1})", value, name);
        }

        Form.AddItem(tag,
            element.ValueRepresentation.Code,
            element.Length.ToString(),
            value);
        return true;
    }

    public bool OnBeginSequence(DicomSequence sequence)
    {
        var tag = String.Format("{0}{1}  {2}", Indent, sequence.Tag.ToString().ToUpper(), sequence.Tag.DictionaryEntry.Name);

        Form.AddItem(tag, "SQ", String.Empty, String.Empty);

        Level++;
        return true;
    }

    public bool OnBeginSequenceItem(DicomDataset dataset)
    {
        var tag = String.Format("{0}Sequence Item:", Indent);

        Form.AddItem(tag, String.Empty, String.Empty, String.Empty);

        Level++;
        return true;
    }

    public bool OnEndSequenceItem()
    {
        Level--;
        return true;
    }

    public bool OnEndSequence()
    {
        Level--;
        return true;
    }

    public bool OnBeginFragment(DicomFragmentSequence fragment)
    {
        var tag = String.Format("{0}{1}  {2}", Indent, fragment.Tag.ToString().ToUpper(), fragment.Tag.DictionaryEntry.Name);

        Form.AddItem(tag, fragment.ValueRepresentation.Code, String.Empty, String.Empty);

        Level++;
        return true;
    }

    public bool OnFragmentItem(Dicom.IO.Buffer.IByteBuffer item)
    {
        var tag = String.Format("{0}Fragment", Indent);

        Form.AddItem(tag, String.Empty, item.Size.ToString(), String.Empty);
        return true;
    }

    public bool OnEndFragment()
    {
        Level--;
        return true;
    }

    public void OnEndWalk()
    {
    }
}

int nextImageNumber = 1;





和第二种形式你可以使用这个





and for the second form you can use this

 using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Image;
using Dicom;
using System.Drawing.Drawing2D;
using Dicom.Imaging;
using Dicom.Imaging.Render;


namespace WindowsFormsApplication1
{
    public partial class DisplayForm : Form
    {


        private DicomFile _file;
        private DicomImage _image;

        private bool _grayscale;
        private double _windowWidth;
        private double _windowCenter;
        private int _frame;


        public DisplayForm(DicomFile file)
        {

            _file = file;
            InitializeComponent();

        }


        protected override void OnLoad(EventArgs e)
        {
            // execute on ThreadPool to avoid STA WaitHandle.WaitAll exception
            ThreadPool.QueueUserWorkItem(delegate(object s)
            {
                _image = new DicomImage(_file.Dataset);
                _grayscale = !_image.PhotometricInterpretation.IsColor;
                if (_grayscale)
                {
                    _windowWidth = _image.WindowWidth;
                    _windowCenter = _image.WindowCenter;
                }
                _frame = 0;
                Invoke(new WaitCallback(DisplayImage), _image);
            });

        }



        protected void DisplayImage(object state)
        {


            var image = (DicomImage)state;


            


            pictureBox1.Image = image.RenderImage(_frame);
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

            if (_grayscale)
                Text = String.Format("DICOM Image Display [wc: {0}, ww: {1}]", image.WindowCenter, image.WindowWidth);
           
           
            a.Text = String.Format("W-Center:[{0}]", image.WindowCenter);
            b.Text = String.Format("W-Width:[{0}]", image.WindowWidth);
        }


查看关于DICOM的这个问题的答案: Dicom Viewer和Dicom Image Printing [ ^ ]。



此致,



-MRB
Look into the answer to this question regarding DICOM: Dicom Viewer and Dicom Image Printing[^].

Regards,

—MRB


这篇关于如何在C#中读取DICOM(* .dcm)文件..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆