Exemplo n.º 1
0
        /// <summary>
        /// 图片转换dcm
        /// </summary>
        /// <param name="file"></param>
        public static void Image2dcm(Img2DcmOptions options)
        {
            if (options.images.Count <= 0)
            {
                Console.WriteLine("请传入图片路径");
                return;
            }
            // 取第一张图片作为基准图片
            Bitmap bitmap = new Bitmap(@options.images[0]);

            byte[]           pixels  = GetPixels(bitmap);
            MemoryByteBuffer buffer  = new MemoryByteBuffer(pixels);
            DicomDataset     dataset = new DicomDataset();

            dataset.Add(DicomTag.SpecificCharacterSet, "GB18030");
            // 写入tag数据
            dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
            dataset.Add(DicomTag.Rows, (ushort)bitmap.Height);
            dataset.Add(DicomTag.Columns, (ushort)bitmap.Width);
            dataset.Add(DicomTag.BitsAllocated, (ushort)8);
            dataset.Add(DicomTag.SOPClassUID, "1.2.840.10008.5.1.4.1.1.2");
            dataset.Add(DicomTag.SOPInstanceUID, "1.2.840.10008.5.1.4.1.1.2." + GetTimeStamp());
            dataset.Add(DicomTag.PatientName, Encoding.Default, string.IsNullOrEmpty(options.patientName) ? "test" : options.patientName);
            dataset.Add(DicomTag.PatientID, string.IsNullOrEmpty(options.patientID) ? Guid.NewGuid().ToString("N") : options.patientID);
            dataset.Add(DicomTag.StudyInstanceUID, "1.2.3.4.5.6.7.8.9.11." + GetTimeStamp());
            dataset.Add(DicomTag.StudyDate, DateTime.Now.ToString("yyyyMMdd"));
            dataset.Add(DicomTag.StudyTime, DateTime.Now.ToString("HHmmss"));
            dataset.Add(DicomTag.StudyID, GetTimeStamp());
            dataset.Add(DicomTag.Modality, "CT");
            dataset.Add(DicomTag.SeriesInstanceUID, "1.2.3.4.5.6.7.8.9.11." + GetTimeStamp());
            dataset.Add(DicomTag.InstanceNumber, "1000");

            DicomPixelData pixelData = DicomPixelData.Create(dataset, true);

            pixelData.BitsStored          = 8;
            pixelData.SamplesPerPixel     = 3;
            pixelData.HighBit             = 7;
            pixelData.PixelRepresentation = 0;
            pixelData.PlanarConfiguration = 0;
            pixelData.AddFrame(buffer);

            // 如果图片大于等于两张,则继续追加
            if (options.images.Count >= 2)
            {
                for (var i = 1; i < options.images.Count; i++)
                {
                    Bitmap           addBit    = new Bitmap(@options.images[i]);
                    byte[]           addPixels = GetPixels(addBit);
                    MemoryByteBuffer addBuffer = new MemoryByteBuffer(addPixels);
                    pixelData.AddFrame(addBuffer);
                }
            }

            // 保存dcm文件
            DicomFile dicomfile = new DicomFile(dataset);

            dicomfile.Save(@options.outDcm);
            Console.WriteLine("success:jpg转dcm成功");
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            // 如果未传参数,则直接返回
            if (args.Length <= 1)
            {
                Console.WriteLine("请传入参数,如:");
                Console.WriteLine("img2dcm \"{patientName:'xxx',patientID:'123',images:['1.jpg','2.jpg'],outDcm:'1.dcm'}\"");
                Console.WriteLine("dcm2jpg \"{'dcmPath':'E:/test/images/3.dcm','outJpgDir':'E:/test/images/outJpg'}\"");
                Console.WriteLine("dcmScu \"{'ip':'127.0.0.1','port':4242,'dcmPath':'E:/test/images/3.dcm'}\"");
                Console.ReadKey();
                return;
            }
            Console.WriteLine(args[1]);
            // 解析命令行参数


            // 图片转dcm
            if (args[0].Equals("img2dcm"))
            {
                Img2DcmOptions options = JsonConvert.DeserializeObject <Img2DcmOptions>(args[1]);
                Image2dcm(options);
            }

            // dcm转图片
            if (args[0].Equals("dcm2jpg"))
            {
                Dcm2JpgOptions options = JsonConvert.DeserializeObject <Dcm2JpgOptions>(args[1]);
                Dcm2Jpg(options);
            }

            // dcm转图片
            if (args[0].Equals("dcmScu"))
            {
                DcmScuOptions options = JsonConvert.DeserializeObject <DcmScuOptions>(args[1]);
                await DcmScu(options);
            }
        }