locked
How can I serialize a Chart object ? RRS feed

  • Question

  •  I use the following code try to serialize a Chart object to a file, but I get the error, How can I do? Thanks!


        protected void Button1_Click(object sender, EventArgs e)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream my = new FileStream("cw.bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(my, Chart1);
            my.Close();
        }



    Server Error in '/1' Application.
    --------------------------------------------------------------------------------

    Type 'System.Web.UI.DataVisualization.Charting.Chart' in Assembly 'System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Runtime.Serialization.SerializationException: Type 'System.Web.UI.DataVisualization.Charting.Chart' in Assembly 'System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

    Source Error:

    The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

    1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

      <%@ Page Language="C#" Debug="true" %>

    or:

    2) Add the following section to the configuration file of your application:

    <configuration>
       <system.web>
           <compilation debug="true"/>
       </system.web>
    </configuration>

    Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

    Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario. 

    Stack Trace:


    [SerializationException: Type 'System.Web.UI.DataVisualization.Charting.Chart' in Assembly 'System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.]
       System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +7731563
       System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +258
       System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +111
       System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) +161
       System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) +51
       System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +410
       System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +134
       System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) +13
       Temp.Button1_Click(Object sender, EventArgs e) +73
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565




    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053


    Free online bookmark manager http://www.hellocw.com/onlinebookmark/
    Tuesday, December 9, 2008 6:50 AM

Answers

  • It is a lot easier to serialize to a binary format:


    this.Chart1.Serializer.Format = System.Web.UI.DataVisualization.Charting.SerializationFormat.Binary;

    this.Chart1.Serializer.Save("chart.bin");

    Tuesday, December 9, 2008 7:39 AM