2017-02-15 70 views
0

我怎樣才能只序列化數據點?我想將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(); 

錯誤 其他信息:類型 'System.Windows.Forms.DataVisualization.Charting.DataPoint' 在大會「System.Windows.Forms.DataVisualization,版本= 4.0.0.0,文化=中性公鑰= 31bf3856ad364e35 '未標記爲可序列化。

+0

請出示DataPoint'的'定義。此外,[MSDN](https://msdn.microsoft.com/de-de/library/system.serializableattribute(v = vs.110).aspx)提供了一個如何使用「Serializable'屬性的例子。 – Codor

+0

@Codor它是一個框架類型 - 「System.Windows.Forms.DataVisualization.Charting.DataPoint」。 –

回答

0

DataPoints包含幾個不是(直接)可序列化的屬性,如Colors,Fonts然後是一些。要麼爲這些類創建可序列化類型,要麼爲可串行化類型DataPoint類創建序列化類型,或者如果只需要一個子集,則創建一個只包含顏色的int和兩個double值的可序列化類,可能是一個字符串對於名稱或提示..

這裏是一個序列化類的有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));