Dicom文件批量转换为Bmp文件(C# / fo-dicom)

简介

有一个dicom数据的文件夹,遍历访问这个文件夹中的dicom文件,然后对dicom文件进行读取和转换为bmp文件

注:
using Dicom;
需要下载引用dicom.dll文件
链接:https://pan.baidu.com/s/1re0Wa-8fTQTvfLAaMqkPgQ
提取码:29fu

其他库文件直接在引用里找就好了

源码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using Dicom;
using Dicom.Imaging;
using Dicom.Imaging.Codec;
using System.Runtime.InteropServices;

namespace DicomToBmp
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\twj83\Desktop\image_dicom\Dicom图片数据\腿部";  //存放dicom数据的文件夹路径
            DirectoryInfo root = new DirectoryInfo(path);
            foreach (FileInfo f in root.GetFiles())//遍历文件夹下的每个文件
            {
                string filename = path+ "\\" + f.Name;     //得到单个文件的filename,自己进行相关操作
                Program pro = new Program();
                //MessageBox.Show(filename);
                pro.ShowAndDrawingDCMImage(filename, f.Name);
            }
            MessageBox.Show("转换完成");
        }

        private void ShowAndDrawingDCMImage(string dcmFileName, string filename)
        {

            try
            {
                DicomFile dcmFile = DicomFile.Open(dcmFileName);
                //Load and Show the meta info of the DCM file
                LoadAndShowDCMMetaInfo(dcmFile);    //读取dicom文件
                //Drawing the image in your DCM file to screen.
                ConvertDCMData2BitImage(dcmFile, filename);  //转换为bmp图像
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void LoadAndShowDCMMetaInfo(DicomFile dcm)
        {
            //Show the message of DCM file to the listview in the dialog.
            DicomDataset dcmDataset = dcm.Dataset;
            DicomFileMetaInformation dcmMetaInfo = dcm.FileMetaInfo;
       
            //DICOM MetaInfo
            //下列代码是获取dicom文件包含的信息,然后写入txt文档里
            //System.IO.File.WriteAllText(@"D:\fo-dicom.txt", string.Empty);         //清空txt文档
            //using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\fo-dicom.txt", true))  //打开txt文档写入信息
            //{
            //    file.WriteLine("0002 " + "0002 " + dcmMetaInfo.MediaStorageSOPClassUID.ToString());
            //    file.WriteLine("0002 " + "0003 " + dcmMetaInfo.MediaStorageSOPInstanceUID.ToString());
            //    file.WriteLine("0002 " + "0010 " + dcmMetaInfo.TransferSyntax.ToString());
            //    file.WriteLine("0002 " + "0012 " + dcmMetaInfo.ImplementationClassUID.ToString());
            //    file.WriteLine("0002 " + "0013 " + dcmMetaInfo.ImplementationVersionName.ToString());
            //    file.WriteLine("0010 " + "0010 " + dcmDataset.Get<string>(DicomTag.PatientName, ""));
            //    file.WriteLine("0010 " + "0020 " + dcmDataset.Get<string>(DicomTag.PatientID, ""));
            //    file.WriteLine("0010 " + "0030 " + dcmDataset.Get<string>(DicomTag.PatientBirthDate));
            //    file.WriteLine("0010 " + "0040 " + dcmDataset.Get<string>(DicomTag.PatientSex));
            //    file.WriteLine("0010 " + "1010 " + dcmDataset.Get<string>(DicomTag.PatientAge));
            //    file.WriteLine("0008 " + "0020 " + dcmDataset.Get<string>(DicomTag.StudyDate));
            //    file.WriteLine("0008 " + "0030 " + dcmDataset.Get<string>(DicomTag.StudyTime));
            //    file.WriteLine("0020 " + "000D " + dcmDataset.Get<string>(DicomTag.StudyInstanceUID));
            //    file.WriteLine("0020 " + "000E " + dcmDataset.Get<string>(DicomTag.SeriesInstanceUID));
            //    file.WriteLine("0020 " + "0010 " + dcmDataset.Get<string>(DicomTag.StudyID));
            //    file.WriteLine("0028 " + "0010 " + dcmDataset.Get<string>(DicomTag.Rows));
            //    file.WriteLine("0028 " + "0011 " + dcmDataset.Get<string>(DicomTag.Columns));
            //    file.WriteLine("0028 " + "0100 " + dcmDataset.Get<string>(DicomTag.BitsAllocated));
            //    file.WriteLine("0028 " + "0101 " + dcmDataset.Get<string>(DicomTag.BitsStored));
            //    file.WriteLine("0028 " + "0102 " + dcmDataset.Get<string>(DicomTag.HighBit));
            //}
        }

        private Bitmap ConvertDCMData2BitImage(DicomFile dcm, string filename)
        {
            DicomFile newDcmFile = null;
            if (dcm.FileMetaInfo.TransferSyntax.IsEncapsulated)//if the data is compressed
            {
                // System.Reflection.Assembly.LoadFrom(Path.Combine(Application.StartupPath,"Dicom.Native64.dll"));
                DicomTranscoder.LoadCodecs(Application.StartupPath, "Dicom.Native*.dll");
                newDcmFile = dcm.ChangeTransferSyntax(DicomTransferSyntax.ExplicitVRLittleEndian, new DicomJpegLsParams());
            }
            DicomImage imageDcm = null;
            if (newDcmFile != null)
                imageDcm = new DicomImage(newDcmFile.Dataset);
            else
                imageDcm = new DicomImage(dcm.Dataset);
            DicomDataset dataset = dcm.Dataset;
            byte[] fs = imageDcm.PixelData.NumberOfFrames < 2 ? imageDcm.PixelData.GetFrame(0).Data : imageDcm.PixelData.GetFrame(1).Data;
            uint size = (uint)Marshal.SizeOf(typeof(short));
            uint padding = (uint)fs.Length % size;
            uint count = (uint)fs.Length / size;
            short[] values = new short[count];
            System.Buffer.BlockCopy(fs, 0, values, 0, (int)(fs.Length - padding));

            int height = dataset.Get<int>(DicomTag.Rows);
            int width = dataset.Get<int>(DicomTag.Columns);
            int windowsWidth = (int)dataset.Get<double>(DicomTag.WindowWidth);
            int windowsCenter = (int)dataset.Get<double>(DicomTag.WindowCenter);
            //if the windowsWidth = 0, the DCM file is not standard type.
            if (windowsWidth == 0 || windowsCenter == 0)
            {
                windowsWidth = values.Max<short>() - values.Min<short>();
                windowsCenter = windowsWidth / 2;
            }
            int low = windowsCenter - windowsWidth / 2;
            int high = windowsCenter + windowsWidth / 2;
            Bitmap bitmap = new Bitmap(width, height);
            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    int r, g, b;
                    int temp = (int)values[(width - j - 1) * height + i];
                    int val = temp > high ? 255 : (temp < low ? 0 : ((temp - low) * 255 / windowsWidth));
                    r = g = b = val;
                    bitmap.SetPixel(i, width - j - 1, Color.FromArgb(r, g, b));
                }
            }
            string pathdicom = "D:\\bmp\\" + filename.Substring(0, filename.Length - 3) + "bmp";
            bitmap.Save(pathdicom, System.Drawing.Imaging.ImageFormat.Bmp);        //保存bmp文件到本地
            return bitmap;
        }
    }
}
  • 0
    点赞
  • 6
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值