// Construct a new DICOM dataset and populate required attributes
DicomDataSet ds = new DicomDataSet();
IFrameSource frameSource = new MpegFrameSource("mpeg.mpg");
int frames = frameSource.CalculatedNumberOfFrames();
ds.Add(Keyword.Rows, 320);
ds.Add(Keyword.Columns, 560);
ds.Add(Keyword.BitsAllocated, 8);
ds.Add(Keyword.BitsStored, 8);
ds.Add(Keyword.HighBit, 7);
ds.Add(Keyword.SamplesPerPixel, 3);
ds.Add(Keyword.PixelRepresentation, 0);
ds.Add(Keyword.PhotometricInterpretation, "YBR_PARTIAL_420");
ds.Add(Keyword.PlanarConfiguration, 0);
ds.Add(Keyword.NumberOfFrames, frames);
ds.SetNonDicomCodec(frameSource);
// Example implementation of MpegFrameSource using Accord.Video.FFMPEG
public class MpegFrameSource : IFrameSource
{
private readonly string _videoPath;
private readonly int _frameCount;
public MpegFrameSource(string path)
{
_videoPath = path;
using (var reader = new VideoFileReader())
{
reader.Open(path);
int count = 0;
while (true)
{
using (var frame = reader.ReadVideoFrame())
{
if (frame == null)
break;
count++;
}
}
_frameCount = count;
}
}
public int CalculatedNumberOfFrames()
{
return _frameCount;
}
public void ReadFrame(int frame0, Array result, int frameCount)
{
using (var reader = new VideoFileReader())
{
reader.Open(_videoPath);
for (int i = 0; i <= frame0; i++)
{
using (var bmp = reader.ReadVideoFrame())
{
if (i == frame0)
{
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
if (!(result is byte[] buffer))
throw new ArgumentException("Result must be a byte[]");
Marshal.Copy(bmpData.Scan0, buffer, 0, bmp.Width * bmp.Height * 3);
bmp.UnlockBits(bmpData);
}
}
}
}
}
public IFrameSource Clone()
{
return new MpegFrameSource(_videoPath);
}
}