c#中将dicom文件格式转换成可读图片
不多说,直接上代码
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 TestForm
{
class DicomHandler
{
string fileName = "";
Dictionary<string, string> tags = new Dictionary<string, string>();//dicom文件中的标签
BinaryReader dicomFile;//dicom文件流
//文件元信息
public Bitmap gdiImg;//转换后的gdi图像
UInt32 fileHeadLen;//文件头长度
long fileHeadOffset;//文件数据开始位置
UInt32 pixDatalen;//像素数据长度
long pixDataOffset = 0;//像素数据开始位置
bool isLitteEndian = true;//是否小端模式(小端在前 、大端在前)
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)
{
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;
}
textBox1.Clear();
tagRead();
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";
}
dicomFile.Close();
}
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