DICOM 生成dcm文件

复制代码
using Dicom;
using Dicom.Imaging;
using Dicom.IO.Buffer;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperIC.Common;

namespace SuperIC.Web
{
    public class ImageToDcm
    {
        USLogger logger = USLogger.GetLogger("FileCopyService");
        public byte[] GetPixels(Bitmap bitmap)
        {
            byte[] bytes = new byte[bitmap.Width * bitmap.Height * 3];
            int wide = bitmap.Width;
            int i = 0;
            int height = bitmap.Height;
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < wide; x++)
                {
                    var srcColor = bitmap.GetPixel(x, y);
                    //bytes[i] = (byte)(srcColor.R * .299 + srcColor.G * .587 + srcColor.B * .114);
                    bytes[i] = srcColor.R;
                    i++;
                    bytes[i] = srcColor.G;
                    i++;
                    bytes[i] = srcColor.B;
                    i++;
                }
            }
            return bytes;
        }

        public void ImportImage(string file, PacsModel pacsModel, string destFilePath, ref string serGuid, ref string destPath)
        {
            logger.Info("path:" + file);

            Bitmap bitmap = new Bitmap(file);

            if (string.IsNullOrEmpty(serGuid))
                serGuid = getnum();
            var seriesUid = getnum();
            byte[] pixels = GetPixels(bitmap);

            MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
            DicomDataset dataset = new DicomDataset();

            dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
            dataset.Add(DicomTag.Rows, (ushort)bitmap.Height);
            dataset.Add(DicomTag.Columns, (ushort)bitmap.Width);
            //dataset.Add(DicomTag.SpecificCharacterSet, "GB18030");
            dataset.Add(DicomTag.BitsAllocated, (ushort)8);
            dataset.Add(DicomTag.SOPClassUID, getnum());
            dataset.Add(DicomTag.SOPInstanceUID, getnum());
            dataset.Add(DicomTag.Modality, pacsModel.EXAM_MODALITY);
            dataset.Add(DicomTag.PatientName, pacsModel.NAME);
            dataset.Add(DicomTag.StudyID, pacsModel.STUDYUID);
            dataset.Add(DicomTag.PatientID, pacsModel.PATIENTID);
            dataset.Add(DicomTag.PatientSex, pacsModel.SEX_CODE);
            dataset.Add(DicomTag.StudyInstanceUID, serGuid);
            dataset.Add(DicomTag.SeriesInstanceUID, seriesUid);
            dataset.Add(DicomTag.StudyDate, pacsModel.EXAM_DTIME);
            dataset.Add(DicomTag.PatientAge, (!string.IsNullOrEmpty(pacsModel.AGE) ? (System.Text.RegularExpressions.Regex.Replace(pacsModel.AGE, @"[^0-9]+", "").PadLeft(3, '0').PadRight(4, 'Y')) : ""));
            dataset.Add(DicomTag.ConversionType, "DI");
            dataset.Add(DicomTag.ImageType, @"ORIGINAL\PRIMARY");

            DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
            pixelData.BitsStored = 8;
            //pixelData.BitsAllocated = 8;
            pixelData.SamplesPerPixel = 3;
            pixelData.HighBit = 7;
            pixelData.PixelRepresentation = 0;
            pixelData.PlanarConfiguration = 0;
            pixelData.AddFrame(buffer);

            DicomFile dicomfile = new DicomFile(dataset);
            destPath = seriesUid + ".dcm";
            dicomfile.Save(destFilePath + seriesUid + ".dcm");
            // SaveDicomFile2(pixels, bitmap.Height, bitmap.Width);
        }

        //public void ImportImage1()
        //{
        //    Bitmap bitmap = new Bitmap(@"E:\Test\111.jpeg");

        //    //if (string.IsNullOrEmpty(serGuid))
        //    //    serGuid = getnum();
        //    var seriesUid = getnum();
        //    byte[] pixels = GetPixels(bitmap);

        //    MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
        //   // MYIOManage.SetImplementation(MYIOManage.Instance);
        //    DicomDataset dataset = new DicomDataset();

        //    dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
        //    dataset.Add(DicomTag.Rows, (ushort)bitmap.Height);
        //    dataset.Add(DicomTag.Columns, (ushort)bitmap.Width);
        //    //  dataset.Add(DicomTag.SpecificCharacterSet, "GB18030");
        //    dataset.Add(DicomTag.BitsAllocated, (ushort)8);
        //    dataset.Add(DicomTag.SOPClassUID, getnum());
        //    dataset.Add(DicomTag.SOPInstanceUID, getnum());
        //    dataset.Add(DicomTag.Modality, "US");
        //    dataset.Add(DicomTag.PatientName, "旺旺");
        //    dataset.Add(DicomTag.StudyID, "1231212");
        //    dataset.Add(DicomTag.PatientID, "1231231");
        //    dataset.Add(DicomTag.PatientSex, "F");
        //    dataset.Add(DicomTag.StudyInstanceUID, getnum());
        //    dataset.Add(DicomTag.SeriesInstanceUID, seriesUid);
        //    dataset.Add(DicomTag.StudyDate, DateTime.Now);
        //    dataset.Add(DicomTag.PatientAge, "020Y");
        //    dataset.Add(DicomTag.ConversionType, "DI");
        //    dataset.Add(DicomTag.ImageType, @"ORIGINAL\PRIMARY");

        //    DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
        //    pixelData.BitsStored = 8;
        //    //pixelData.BitsAllocated = 8;
        //    pixelData.SamplesPerPixel = 3;
        //    pixelData.HighBit = 7;
        //    pixelData.PixelRepresentation = 0;
        //    pixelData.PlanarConfiguration = 0;
        //    pixelData.AddFrame(buffer);

        //    DicomFile dicomfile = new DicomFile(dataset);
        //    dicomfile.Save(@"E:\us\" + seriesUid + ".dcm");
        //}


        public string getnum()
        {
            // var num = "1.2.156.112536.2.560.202009.1334022818108.1";
            Random rnd = new Random();

            var minValue = 0;
            var maxValue = 9;
            //1位数
            refnum(1, out minValue, out maxValue);
            var a = rnd.Next(minValue, maxValue);

            //2位数
            //refnum(2, out minValue, out maxValue);
            //var b = rnd.Next(minValue, maxValue);

            //3位数
            refnum(3, out minValue, out maxValue);
            var b = rnd.Next(minValue, maxValue);

            //6位数
            refnum(6, out minValue, out maxValue);
            var c = rnd.Next(minValue, maxValue);

            //10位数
            refnum(10, out minValue, out maxValue);
            var d = rnd.Next(minValue, maxValue);

            //1位数
            refnum(1, out minValue, out maxValue);
            var e = rnd.Next(minValue, maxValue);

            //3位数
            refnum(3, out minValue, out maxValue);
            var f = rnd.Next(minValue, maxValue);

            //10位数
            refnum(10, out minValue, out maxValue);
            var g = rnd.Next(minValue, maxValue);

            return "1." + a + "." + b + "." + c + "." + e + "." + f + "." + DateTime.Now.Date.ToString("yyyyMMdd") + d + "." + g;
        }

        public void refnum(int num, out int minValue, out int maxValue)
        {
            switch (num)
            {
                case 1:
                    minValue = 1;
                    maxValue = 9;

                    break;
                case 3:
                    minValue = 100;
                    maxValue = 900;

                    break;
                case 6:
                    minValue = 100000;
                    maxValue = 999999;

                    break;
                case 10:
                    minValue = 1000000000;
                    maxValue = int.MaxValue;
                    break;
                default:
                    minValue = 1000000000;
                    maxValue = int.MaxValue;
                    break;
            }

        }
    }
}
复制代码
为防止在生成的时候会导致姓名等信息会出现乱码的,需要建一个初始化类。
这个最好是在程序启动的时候就初始化,因为放到生成图片里面才实例化,也会出现乱码的情况,使用方式如下

  MYIOManage.SetImplementation(MYIOManage.Instance);

 

复制代码
using Dicom.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperIC.Web
{
    /// <summary>
    /// 点击(此处)折叠或打开MyIOManager.SetImplementation(MyIOManager.Instance);
    ///将fo-dicom使用的默认字符集修改一下。注意,我的类中BaseEncodingImpl取得的是Encoding.Default,意思就是取系统默认编码。这样在中文系统下就是GB2312或者GB18030了,就可以友好的支持中文了,而不 用在每次调用DicomDataSet的AddOrUpdate方法时指定编码了。是不是很方便呢。
    /// </summary>
    class MYIOManage : IOManager
    {

        public static readonly IOManager Instance = new MYIOManage();

        private MYIOManage()
        {

        }

        protected override Encoding BaseEncodingImpl
        {
            get
            {

                return Encoding.Default;
            }
        }

        protected override IPath PathImpl
        {
            get
            {
                return DesktopPath.Instance; 
            }
        }

        protected override IDirectoryReference CreateDirectoryReferenceImpl(string directoryName)
        {
            return new DesktopDirectoryReference(directoryName);
        }

        protected override IFileReference CreateFileReferenceImpl(string fileName)
        {
            return new DesktopFileReference(fileName);
        }
    }
}
复制代码

 

posted @   xiaojianjian  阅读(573)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示