Click here to Skip to main content
14,869,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..
Posted
Comments
shijuse 18-Oct-11 7:28am
   
http://www.dotnetobject.com/showthread.php?tid=522 this may help you or you will get the idea

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);
        }
   
Comments
Tejas Vaishnav 7-Jun-14 1:17am
   
Maheshwarreddy first of all thank you for your answer, but now i need not to get this solution, because the question is asked in 2011, but anyway thank you for your answer.
Member 13935504 27-Nov-18 3:33am
   
pictureBox1.Image = image.RenderImage(_frame);

Error: Cannot implicitly convert type 'Dicom.Imaging.IImage' to 'System.Drawing.Image'. An explicit conversion exists(Are you missing a cast?)
Look into the answer to this question regarding DICOM: Dicom Viewer and Dicom Image Printing[^].

Regards,

—MRB
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900