# ExtractEncapsulatedFile.cs

    /*=========================================================================

      Program: GDCM (Grassroots DICOM). A DICOM library
      Module:  $URL$

      Copyright (c) 2006-2009 Mathieu Malaterre
      All rights reserved.
      See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

         This software is distributed WITHOUT ANY WARRANTY; without even
         the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
         PURPOSE.  See the above copyright notice for more information.

    =========================================================================*/

    /*
     * This example shows how one from C# context can extract a binary blob
     * and write out as a file.
     * This example is meant for pdf encapsulated file, but can be adapted for other type
     * of binary blob.
     *
     * DICOM file is:
     * ...
     * (0042,0010) ST (no value available)                     #   0, 0 DocumentTitle
     * (0042,0011) OB 25\50\44\46\2d\31\2e\32\20\0d\25\e2\e3\cf\d3\20\0d\31\30\20\30\20... # 40718, 1 EncapsulatedDocument
     * (0042,0012) LO [application/pdf]                        #  16, 1 MIMETypeOfEncapsulatedDocument
     * ...
     *
     * Usage:
     * $ export LD_LIBRARY_PATH=$HOME/Projects/gdcm/debug-gcc/bin
     * $ mono bin/ExtractEncapsulatedFile.exe some_pdf_encapsulated.dcm
     */
    using System;
    using gdcm;

    public class ExtractEncapsulatedFile
    {
      public static int Main(string[] args)
        {
        string file = args[0];
        Reader reader = new Reader();
        reader.SetFileName( file );
        bool ret = reader.Read();
        if( !ret )
          {
          return 1;
          }

        File f = reader.GetFile();
        DataSet ds = f.GetDataSet();
        Tag tencapsulated_stream = new Tag(0x0042,0x0011); // Encapsulated Document
        if( !ds.FindDataElement( tencapsulated_stream ) )
          {
          return 1;
          }
        // else
        DataElement de = ds.GetDataElement( tencapsulated_stream );
        ByteValue bv = de.GetByteValue();
        uint len = bv.GetLength();
        byte[] encapsulated_stream = new byte[len];
        bv.GetBuffer( encapsulated_stream, len );

        // Write out the decompressed bytes
        //System.Console.WriteLine(image.toString());
        using (System.IO.Stream stream =
          System.IO.File.Open(@"/tmp/dd.pdf",
            System.IO.FileMode.Create))
          {
          System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
          writer.Write( encapsulated_stream );
          }

        return 0;
        }
    }