A day with .Net

My day to day experince in .net

Archive for the ‘DICOM’ Category

Valid DICOM File checking in JavaScript

Posted by vivekcek on May 9, 2014

Today i will show you, How we can check wheather a file is a DICOM file or not through JavaScript.
One year ago i wrote about how we can achieve this through c#. Please refer this “Valid Dicom File c#

Normally the extension for a DICOM file is ‘.dcm’. But this extension never guarantee the file is a valid DICOM file.

DICOM is a binary file, in a DICOM file first 128(0-127) bytes is unused. the next 4 bytes(128-131) contain letters ‘D’,’I’,’C’,’M’.

So our logic is to ready all bytes from 128-131 using some binary reader and equate with ‘D’,’I’,’C’,’M’

128=>’D’
129=>’I’
130=>’C’
131=>’M’

header

<input type="file" id="dcmUpload" />

<script type="text/javascript">

    //binding event handler to input,This work in chrome and firefox. Use attachevent in IE
    document.getElementById("dcmUpload").addEventListener("change", handleInput, false);

    function handleInput(e) {
        if (e.target.files.length == 0) {
            return;
        }

        var file = e.target.files[0];
        var reader = new FileReader();
        reader.readAsArrayBuffer(file);

        //Fired after sucessful file read, Please read documenation for FileReader
        reader.onload = function (evt) {
            if (evt.target.readyState === FileReader.DONE) {

                var array = new Uint8Array(evt.target.result);
                var s = "";
                var start = 128, end = 132;
                for (var i = start; i < end; ++i) {
                    s += String.fromCharCode(array[i]);
                }

                if (s == "DICM") {

                    alert("DICM found not a valid dicom file")
                }

                else {

                    alert("DICM not found");
                }
            }
        };

    }

</script>

Posted in DICOM, javascript | Tagged: , , , | Leave a Comment »

Valid DICOM file checking c#

Posted by vivekcek on April 17, 2013

Normally the extension for a DICOM file is ‘.dcm’. But this extension never guarantee the file is a valid DICOM file. So in this post i will explain how to check the file is a DICOM via pure c# code.

DICOM is a binary file, in a DICOM file first 128(0-127) bytes is unused. the next 4 bytes(128-131) contain letters ‘D’,’I’,’C’,’M’.

So our logic is to ready all bytes from 128-131 using some binary reader and equate with ‘D’,’I’,’C’,’M’

128=>’D’
129=>’I’
130=>’C’
131=>’M’

header

The code is given below

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

namespace DICOMCheck
{

    public partial class Form1 : Form

    {
        public Form1()
       
        {

            InitializeComponent();

        }
 
        private void button1_Click(object sender, EventArgs e)

        {

            openFileDialog1.ShowDialog();

            string filePath = openFileDialog1.FileName;

            BinaryReader br = new BinaryReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), Encoding.ASCII);

            byte[] preamble = new byte[132];

            br.Read(preamble, 0, 132);

            if (preamble[128] != 'D' || preamble[129] != 'I' || preamble[130] != 'C' || preamble[131] != 'M')

            {

                throw new Exception("Not a valid dicom file");
              

            }

            else

                MessageBox.Show("Valid dicom file");

        }

    }

}

Posted in c#.net, DICOM | Leave a Comment »