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?

2
Contributors
1
Reply
21
Views
2 Years
Discussion Span
Last Post by tdrosiadis
1

You have to notate your class as serializable.
You have to put the [Serializable] tag before your class name like this:

[Serializable()]    //Set this attribute to all the classes that want to serialize public class WhatEver : ISerializable //derive your class from ISerializable

.....
rest of your code

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.