Datapoint Serialization

徘徊边缘 提交于 2021-02-16 15:39:22

问题


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

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.


回答1:


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..

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

Use it like this:

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


来源:https://stackoverflow.com/questions/42241713/datapoint-serialization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!