0

I am working on a website which is being developed in Visual Studio 2010 with ASP.NET 4. I am trying to programmatically extract an image from a word document. The approach I am using is this:

  1. Convert WordApp.InlineShapes[1] (System._COM Object) to a byte[]
  2. Convert byte[] to an System.Drawing.Image object
  3. Save the image

But I am getting following exception in step1 itself:

Type 'System.__ComObject' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

I googled for two days but unable to understand the problem and the workaround as I am quiet new to developement in ASP.NET

The line that I am executing is this:

byte[] shapeByteArray = ObjectToByteArray(wrdDoc.InlineShapes[1]);

The method ObjectToByteArray is as follows:

    private byte[] ObjectToByteArray(Object obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();

        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }

Following line is throwing the SerializationException:

return ms.ToArray();

Can you guys please help me understand and solve this issue?

Subzero
  • 1
  • 4
  • You can't serialize a COM object which is what `BinaryFormatter` is attempting to do. Try using the clipboard like this answer: http://stackoverflow.com/questions/7937487/extract-image-from-word-file – Zer0 Sep 03 '14 at 19:51
  • @Zer0 : Thanks for your response. I checked the link you have given but I am finding no way to access clipboard. It seems to be possible in Windows Forms Application but not in ASP.NET website. I am working on an ASP.NET website. Any idea what could be done? – Subzero Sep 05 '14 at 08:45
  • You would have to find a different way to serialize that object. `BinaryFormatter` won't work here because it's not marked `Serializable`. I'm not familiar with the object you're trying to serialize so I couldn't write up a custom serializer. – Zer0 Sep 05 '14 at 09:00

0 Answers0