在医疗行业工作的程序猿们,离不开DICOM协议,很多时候,都会面临图像格式转换的问题,该片文章给出一个简单的demo,如何通过C#实现PDF格式转换成DICOM格式。博主拿到这个需求的时候,在谷歌上搜索了很久,无奈是技术新人(菜+懒癌重度),没有找到pdf直接转换成dicom的方法,也有找到一些博主实现的dll,但看不到具体实现方式,博主本来是C++程序媛,后在一个可爱的写C#的姑娘的帮助下(此处有掌声),才能实现需求........(手动狗头)。
首先,引入所需要的动态链接库? ?https://download.csdn.net/download/HarshBrand/20666687
using Dicom;
using Dicom.Imaging;
using Dicom.IO.Buffer;
using O2S.Components.PDFRender4NET;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
头文件如果有缺失的话,会报错的(再次狗头)?
以下为硬干货,请吞食,嘿嘿嘿
转换图片格式的函数
private enum Definition
{
One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
}
/// <summary>
/// 将PDF文档转换为图片的方法
/// </summary>
/// <param name="pdfInputPath">PDF文件路径</param>
/// <param name="imageOutputPath">图片输出路径</param>
/// <param name="imageName">生成图片的名字</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
/// <param name="imageFormat">设置所需图片格式</param>
/// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
private static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
{
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// validate pageNum
if (startPageNum <= 0)
{
startPageNum = 1;
}
if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 100 * (int)definition);
pageImage.Save(imageOutputPath + "\\" + imageName + "." + imageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
pdfFile.Dispose();
}
本菜菜没有找到直接转换的方式,是按照pdf-bmp-dcm的路数。
public static 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++)//20210312
{
for (int x = 0; x < wide; x++)//20210312
{
var srcColor = bitmap.GetPixel(x, y);
bytes[i] = srcColor.R;
i++;
bytes[i] = srcColor.G;
i++;
bytes[i] = srcColor.B;
i++;
}
}
byte[] bytess = new byte[bitmap.Width * bitmap.Height * 3];
for (int j = 0; j < bytess.Length; j++)
{
bytess[j] = bytes[j];
}
return bytess;
}
main函数:
string pdfpath = @"C:\Users\Administrator\Desktop\test11\1.pdf";
string imagefolderpath = @"C:\Users\Administrator\Desktop\test11";
ConvertPDF2Image(pdfpath, imagefolderpath, "test", 1, 1, ImageFormat.Bmp, Definition.One);
string file = imagefolderpath + "\\test.bmp";
string SeriesUid = "1111";
Bitmap bitmap = new Bitmap(file);
byte[] pixels = GetPixels(bitmap);
MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
DicomDataset dataset = new DicomDataset();
dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);// PhotometricInterpretation.Rgb.Value
// dataset.Add(DicomTag.Rows, (ushort)(bitmap.Height / 5));
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.20181120090837121314");
// dataset.Add(DicomTag.PatientAge, 10);
dataset.Add(DicomTag.StudyInstanceUID, "111");
dataset.Add(DicomTag.StudyDate, DateTime.Now.ToString("yyyyMMdd"));
dataset.Add(DicomTag.PatientName, "MPR");
dataset.Add(DicomTag.PatientID, "1010");
dataset.Add(DicomTag.PatientPosition, "HFS");
//dataset.Add(DicomTag.PixelDataAreaOriginRelativeToFOV, DataCommon.PatientInfo.PatientPosition);
//dataset.Add(DicomTag.PhotometricInterpretation, "MONOCHROME2");
dataset.Add(DicomTag.SamplesPerPixel, "3");
dataset.Add(DicomTag.WindowCenter, "128");
dataset.Add(DicomTag.WindowWidth, "256");
dataset.Add(DicomTag.RescaleIntercept, "0");
dataset.Add(DicomTag.RescaleSlope, "1");
dataset.Add(DicomTag.RescaleType, "US");
dataset.Add(DicomTag.PixelSpacing, "1.0\\1.0");
dataset.Add(DicomTag.SeriesInstanceUID, SeriesUid);
dataset.Add(DicomTag.ProtocolName, "MPR");
dataset.Add(DicomTag.SeriesNumber, "5102");
dataset.Add(DicomTag.SeriesDescription, "0*0");
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);
string dcmFileName;
DicomFile dicomfile = new DicomFile(dataset);
bitmap.Dispose();
dicomfile.Save(file.Replace(".bmp", ".dcm"));
dcmFileName = file.Replace(".bmp", ".dcm");
以上。
|