Decompress Compress Rich Text field
<p>I have an image column in a datatable containing compressed RichText data. I suppose this was obtained from Outlook (PR_RTF_COMPRESSED property or something). I need to decompress it. Using C#, I'm trying the following, but I'm getting a System.Runtime.InteropServices.COMExceptions and can't figure it out.</p>

<p>I found a couple of old links asking this question, with no resolved answers. Here's a snippet of my code using c#.</p>

<pre><code> public Form1()
    {
        InitializeComponent();
        IStream streamOut;

        string con = "connection string";
        SqlCommand command = new SqlCommand("select top 10 columnInCompressedRichTextFormat FROM tableWithCompressedRichTextData", new SqlConnection(dbConnectionString));
        DataTable x = new DataTable();
        SqlDataAdapter a = new SqlDataAdapter(command);
        a.Fill(x);
        a.Dispose();

        foreach (DataRow r in dtResults.Rows)
        {
            byte[] arrayOfBytes = (byte[]) r["columnInCompressedRichTextFormat"];
            IStream i = CreateIStreamFromBytes(arrayOfBytes);
            WrapCompressedRTFStream(input, 0, out streamOut);
        }
    }

    public IStream CreateIStreamFromBytes(byte[] bytes)
    {
        IntPtr hglobal = Marshal.AllocHGlobal(bytes.Length);
        Marshal.Copy(bytes, 0, hglobal, bytes.Length);

        IStream stream = null;

        CreateStreamOnHGlobal(hglobal, true, out stream);

        return stream;
    }

    [DllImport("Mapi32.dll", PreserveSig = false)]
    private static extern void
        WrapCompressedRTFStream(
        [MarshalAs(UnmanagedType.Interface)] IStream lpCompressedRTFStream,
        uint ulflags,
        [MarshalAs(UnmanagedType.Interface)] out IStream lpUncompressedRTFStream
        );

    [DllImport("ole32.dll", PreserveSig = false)]
    static extern int CreateStreamOnHGlobal(IntPtr hGlobal,
        [MarshalAs(UnmanagedType.Bool)] bool fDeleteOnRelease,
        [MarshalAs(UnmanagedType.Interface)] out IStream ppstm
        );

    public const uint MAPI_MODIFY = 0x00000001;
    public const uint STORE_UNCOMPRESSED_RTF = 0x00008000;
}
</code></pre>

<p>WrapCompressedRTFStream is throwing the error. Any ideas?</p>

<p>Many Thanks.</p>