数据点序列化 [英] Datapoint Serialization

查看:60
本文介绍了数据点序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I only serializable DataPoints? I want save DataPoints to a file.

[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();

错误附加信息:在程序集"System.Windows.Forms.DataVisualization,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 31bf3856ad364e35"中,键入"System.Windows.Forms.DataVisualization.Charting.DataPoint"未标记为可序列化.

Error Additional information: Type 'System.Windows.Forms.DataVisualization.Charting.DataPoint' in Assembly 'System.Windows.Forms.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

推荐答案

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

DataPoints contain several properties that are not (directly) serializable, like Colors, Fonts and then some. Either you create serializable types for those and a fully serializable DataPoint class or, if you need only a subset, create a serializable class that includes only, say, an int for the color and two doubles for the x & y values, maybe a string for the name or a tooltip..

以下是可序列化类的示例,该类具有 DataPoint 属性的最小子集:

Here is an example of a serializable class with a minimal subset of a DataPoint's properties:

[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();
}

当然,反序列化向后进行相同的操作:

Of course deserializing does the same backwards:

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));

这篇关于数据点序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆