C#中:如何读取文件的部分? (DICOM) [英] c#: how to read parts of a file? (DICOM)

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

问题描述

我想读在C#中的DICOM文件。我不想做任何幻想,我只是现在想知道如何在读的元素,但首先我真的想知道的如何读取头,看是否是一个有效的DICOM文件

I would like to read a DICOM file in C#. I don't want to do anything fancy, I just for now would like to know how to read in the elements, but first I would actually like to know how to read the header to see if is a valid DICOM file.

它由二进制数据元素。的前128个字节未使用(设置为零),然后将字符串DICM'。其次是头信息,这些信息组织成组。

It consists of Binary Data Elements. The first 128 bytes are unused (set to zero), followed by the string 'DICM'. This is followed by header information, which is organized into groups.

样本DICOM头


First 128 bytes: unused DICOM format.
Followed by the characters 'D','I','C','M'
Followed by extra header information such as:

0002,0000, File Meta Elements Groups Len: 132
0002,0001, File Meta Info Version: 256
0002,0010, Transfer Syntax UID: 1.2.840.10008.1.2.1.
0008,0000, Identifying Group Length: 152
0008,0060, Modality: MR
0008,0070, Manufacturer: MRIcro

在上面的例子中,首部被组织成组。该集团0002六角是包含三个元素文件的元信息组:一是定义组的长度,一是存储文件版本和他们的存储传输语法

In the above example, the header is organized into groups. The group 0002 hex is the file meta information group which contains 3 elements: one defines the group length, one stores the file version and the their stores the transfer syntax.

< STRONG>问题


  • 我如何读取头文件并验证它是否是通过检查'D DICOM文件','我','C','128字节的前导后M'字?

  • 如何继续分析该文件中读取数据?

推荐答案

这样的事情应该读取文件,其基本的,不能处理所有情况,但它是一个起点:

Something like this should read the file, its basic and doesn't handle all cases, but it would be a starting point:


public void ReadFile(string filename)
{
    using (FileStream fs = File.OpenRead(filename))
    {
        fs.Seek(128, SeekOrigin.Begin);
        if (!(fs.ReadByte() != (byte)'D' ||
              fs.ReadByte() != (byte)'I' ||
              fs.ReadByte() != (byte)'C' ||
              fs.ReadByte() != (byte)'M'))
        {
            Console.WriteLine("Not a DCM");
            return;
        }
        BinaryReader reader = new BinaryReader(fs);

        ushort g;
        ushort e;
        do
        {
            g = reader.ReadUInt16();
            e = reader.ReadUInt16();

            string vr = new string(reader.ReadChars(2));
            long length;
            if (vr.Equals("AE") || vr.Equals("AS") || vr.Equals("AT")
                || vr.Equals("CS") || vr.Equals("DA") || vr.Equals("DS")
                || vr.Equals("DT") || vr.Equals("FL") || vr.Equals("FD")
                || vr.Equals("IS") || vr.Equals("LO") || vr.Equals("PN")
                || vr.Equals("SH") || vr.Equals("SL") || vr.Equals("SS")
                || vr.Equals("ST") || vr.Equals("TM") || vr.Equals("UI")
                || vr.Equals("UL") || vr.Equals("US"))
               length = reader.ReadUInt16();
            else
            {
                // Read the reserved byte
                reader.ReadUInt16();
                length = reader.ReadUInt32();
            }

            byte[] val = reader.ReadBytes((int) length);

        } while (g == 2);

        fs.Close();
    }

    return ;
}



中的代码实际上不尝试和考虑到的传输语法编码后的数据可以在组后更改2个元素,它也不会尝试这样做,在阅读的实际值什么。

The code does not actually try and take into account that the transfer syntax of the encoded data can change after the group 2 elements, it also doesn't try and do anything with the actual values read in.

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

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