数据点序列化

时间:2017-02-15 05:57:13

标签: c# serialization

我怎样只能序列化DataPoints?我想将DataPoints保存到文件中。

[Serializable]
class CIE
{
  public List<DataPoint> CieDataPoint = new List<DataPoint>() ;
}
List<DataPoint> GetCieColorPoints(string filename, int count)
{
        CIE cie = new CIE();
        var data = new List<DataPoint>();

        float cr = (float)Math.Sqrt(count);
        using (Bitmap bmp = new Bitmap(filename))
        {
            int sx = (int)(bmp.Width / cr);
            int sy = (int)(bmp.Height / cr);

            float scx = 0.8f / bmp.Width;
            float scy = 0.9f / bmp.Height;

            for (int x = 0; x < bmp.Width; x += sx)
                for (int y = 0; y < bmp.Height; y += sy)
                {
                    Color c = bmp.GetPixel(x, y);
                    float b = c.GetBrightness();
                    if (b > 0.01f && b < 0.99f)
                    {
                        var dp = new DataPoint(x * scx, 0.9f - y * scy);
                        dp.Color = c;                        
                        dp.MarkerColor = dp.Color;
                        dp.MarkerStyle = MarkerStyle.Circle ;
                        data.Add(dp);
                    }
                }
        }  
        return data;
    }

Cie.CieDataPoint = GetCieColorPoints("E:\\CIExy1931_T2.png", 125000);;
IFormatter formatter = new BinaryFormatter();
FileStream seryalization = new FileStream("CIEdata", FileMode.Create, FileAccess.Write);
formatter.Serialize(seryalization, Cie);
seryalization.Close();

错误 附加信息:在Assembly'System.Windows.Forms.DataVisualization,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'中键入'System.Windows.Forms.DataVisualization.Charting.DataPoint'未标记为可序列化。

1 个答案:

答案 0 :(得分:1)

DataPoints包含多个(直接)可序列化的属性,例如ColorsFonts,然后是一些。您可以为这些类创建可序列化类型,也可以创建完全可序列化的DataPoint类,或者,如果您只需要一个子集,则创建一个可序列化的类,其中仅包含颜色的int和x&amp;的两个双精度类。 y值,可能是名称的字符串或工具提示..

以下是可序列化类的示例,其中包含DataPoint属性的最小子集:

[Serializable]
public class SPoint
{
    public int  PointColor { get; set; }
    public double XValue { get; set; }
    public double YValue { get; set; }
    public string Name { get; set; }

    public SPoint()        {        }

    public SPoint(int c, double xv, double yv,  string n)
    {
        PointColor = c; XValue = xv; YValue = yv; Name = n;
    }

    static public SPoint FromDataPoint(DataPoint dp)
    {
        return new SPoint(dp.Color.ToArgb(), dp.XValue, dp.YValues[0], dp.Name);
    }

    static public DataPoint FromSPoint(SPoint sp)
    {
        DataPoint dp = new DataPoint(sp.XValue, sp.YValue);
        dp.Color = Color.FromArgb(sp.PointColor);
        dp.Name = sp.Name;
        return dp;
    }
}

像这样使用:

using System.Xml.Serialization;
...
...
var points =  chart.Series[0].Points;

List<SPoint> sPoints = points.Cast<DataPoint>()
                             .Select(x => SPoint.FromDataPoint(x))
                             .ToList();

XmlSerializer xs = new XmlSerializer(sPoints.GetType());
using (TextWriter tw = new StreamWriter(@"yourfilename.xml"))
{
    xs.Serialize(tw, sPoints);
    tw.Close();
}

当然反序列化会做同样的反向:

using (TextReader tw = new StreamReader(@"yourfilename.xml"))
{
    //chart.Series[0].Points.Clear();
    //sPoints.Clear();
    sPoints = (List<SPoint>)xs.Deserialize(tw);
    tw.Close();
}
foreach (var sp in sPoints) s.Points.Add(SPoint.FromSPoint(sp));